numpy数组中的min函数 [英] min function in numpy array

查看:65
本文介绍了numpy数组中的min函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一维numpy数组中找到一个最小值,该值看起来像:

I am trying to find a min value from one dimensional numpy array which which looks like:

col = array(['6.7', '0.9', '1.3', '4', '1.8'],dtype='|S7'), 

使用 col.min(),这不起作用.

我已尝试在> NumPy中获取建议/max从数值记录数组中查看功能,它无法将"S7"识别为有效字段.

I tried as suggested on NumPy: get min/max from record array of numeric values view function, it failed to recognize 'S7' as valid field.

解决此问题的最佳方法是什么?在读取值或使用min函数时是否应该指定数据类型?

What is the best way to deal with this problem? Should I have specified the data type while reading the values or while using the min function?

推荐答案

问题是您有一个字符串数组,而不是数字数组.因此,您需要先将数组转换为适当的类型:

The problem is that you have an array of strings, not an array of numbers. You therefore need to convert the array to an appropriate type first:

In [38]: col.astype(np.float64).min()
Out[38]: 0.90000000000000002

在读取值或使用min函数时是否应该指定数据类型

should I have specified the data type while reading the values or while using the min function

如果您知道输入是数字输入,则在读取数据时指定数据类型将很有意义.

If you know the input to be numeric, it would make sense to specify the data type when reading the data.

这篇关于numpy数组中的min函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆