繁体   English   中英

在 np.array 中查找最频繁的值会返回“TypeError:无法根据安全规则将数组数据从 dtype('float64) 转换为 dtype('int64')”

[英]Find most frequent value in an np.array gives back "TypeError: cannot cast array data from dtype('float64) to dtype('int64') according to safe rule"

在我的项目中,我正在创建一个 np.array,如下所示:

values_array = np.array(0.00, dtype=np.float32)

之后,我在 while 循环中附加了更多的值:

values_array = np.append(values_array, round(value, 2))

这就是我的最终数组输出的样子:

[0. 0.41 0.41 0.42 0.41 0.43]

但是当我尝试像这样在数组中找到最频繁的值时:

top_value = np.argmax(np.bincount(values_array))

我收到此错误:


TypeError: cannot cast array data from dtype('float64) to dtype('int64') according to safe rule

我怎样才能避免它?

这称为mode文档 您可以通过不同的方式执行此操作,一种使用模块scipy ,另一种使用statistics

from scipy import stats as st
st.mode(values_array)

要么

import statistics as st
print(st.mode(values_array))

无法从 dtype('

[英]Cannot cast array data from dtype('<M8[ns]') to dtype('float64') according to the rule 'safe'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM