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

Python 直方图操作以删除不需要的数据,python,numpy,histogram,Python,Numpy,Histogram,如何在特定频率计数下从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) p

如何在特定频率计数下从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()
给出:

我想扔掉频率小于“X”的垃圾箱(比如频率可以是100)

想要:

谢谢。

Une创建直方图

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")
然后使用。给定一个条件,它会生成一个布尔数组,您可以使用它来索引直方图

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")
将numpy导入为np
将matplotlib.pyplot作为plt导入
高斯数=np.random.randn(1000)
#获取直方图
hist,bins=np.直方图(高斯数,bins=12)
#阈值频率
频率=100
#将低值归零

hist[np.where(hist你提供的信息太少了。到目前为止你尝试了什么?你能发布任何代码吗?你在示例中不使用导入,但是你使用np和plt,我猜它们来自numpy和matplotlib.pyplot。不,如果该bin上的频率计数较低,我想去掉这些值。你的意思是将该值设置为0或完全删除该值吗例如,在我的例子中,你会期望吗?是的,完全摆脱它们:)这是一个疯狂的原因什么是histog?在上面的例子中实现它仍然有一些问题。好的,谢谢你-现在我只是得到一个错误,没有定义“histog”