【问题标题】:Converting Histogram Values to Int Array in Python在 Python 中将直方图值转换为 Int 数组
【发布时间】:2018-08-14 22:54:57
【问题描述】:

每个人。我使用大小为 1x1000 且其值范围为 0 到 99 的数组创建了直方图。我想将直方图值存储为 int 数组。但是,当我运行程序时,我得到了程序的以下结果(每个人的数值都是不同的,因为它是随机的):

(array([ 93., 101., 119., 91., 97., 110., 102., 111., 85., 91.]), array([ 0. , 9.9, 19.8, 29.7 , 39.6, 49.5, 59.4, 69.3, 79.2, 89.1, 99.]), )

我想得到这样的结果:

[93 101 119 91 97 110 102 111 85 91]

你能帮忙吗?结果我得到什么样的数组?它看起来不像我以前见过的任何东西

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(100, size=1000)   
y = plt.hist(x,10)  

print(y)

【问题讨论】:

  • 你得到一个元组。要访问您想要的结果,请联系我们y[0]。我看不出你想要输出没有小数点的任何充分理由。不过,您可以根据需要使用int() 转换y[0] 的每个元素

标签: python arrays histogram


【解决方案1】:

在处理随机数时,您可以使用np.random.seed(n) 来确保运行您的代码的其他人拥有相同的数字。

根据文档,您可以从 plt.hist() 获取 bin 值和 bin 边缘。

看看以下内容:

print(y[0])
 [109. 109.  82. 117.  84. 101.  80. 108. 110. 100.]

[type(i) for i in y[0]]
 [<class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>, <class 'numpy.float64'>]

您可以将y[0] 分配给您想要的任何东西,您应该一切顺利。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 2014-04-05
    • 2017-02-11
    • 1970-01-01
    • 2019-05-15
    相关资源
    最近更新 更多