gpt4 book ai didi

python - 直方图操作以删除不需要的数据

转载 作者:太空宇宙 更新时间:2023-11-04 00:52:59 26 4
gpt4 key购买 nike

如何在特定频率计数下从 python 中的直方图中删除数据?

假设我有 10 个箱子,第一个箱子有 4 个,第二个有 2 个,第三个有 1 个,第四个有 5 个,等等......现在我想去掉计数为 2 或更少的数据。所以第二个箱子会归零,第三个箱子也会。

例子:

import numpy as np
import matplotlib.pyplot as plt

gaussian_numbers = np.random.randn(1000)
plt.hist(gaussian_numbers, bins=12)
plt.title("Gaussian Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")

fig = plt.gcf()

给予:

enter image description here

我想去掉频率低于“X”的垃圾箱(例如频率可能为 100)。

想要:

enter image description here

谢谢。

最佳答案

联合国np.histogram创建直方图。

然后使用np.where .在给定条件的情况下,它会生成一个 bool 值数组,您可以使用它来为直方图编制索引。

import numpy as np
import matplotlib.pyplot as plt

gaussian_numbers = np.random.randn(1000)

# Get histogram
hist, bins = np.histogram(gaussian_numbers, bins=12)

# Threshold frequency
freq = 100

# Zero out low values
hist[np.where(hist <= freq)] = 0

# Plot
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.title("Gaussian Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")

(剧情部分灵感来自here。)

关于python - 直方图操作以删除不需要的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36341774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com