【问题标题】:Histogram manipulation to remove unwanted data直方图操作以删除不需要的数据
【发布时间】:2016-07-20 09:37:35
【问题描述】:

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

想要:

谢谢。

【问题讨论】:

  • 您提供的信息太少。到目前为止,您尝试过什么?
  • 你能发布任何代码吗??
  • 您在示例中没有使用您的导入,但是您使用了 np 和 plt,我猜它们来自 numpy 和 matplotlib.pyplot。

标签: python numpy histogram


【解决方案1】:

取消np.histogram 以创建直方图。

然后使用np.where。给定一个条件,它会产生一个布尔数组,您可以使用它来索引您的直方图。

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。)

【讨论】:

  • 不,如果该 bin 的频率计数较低,我想删除这些值
  • 您的意思是将值设置为 0 或完全删除该值?例如,您对我的示例有何期望?
  • 是的,完全摆脱它们 :) 疯狂是有原因的
  • 什么是组织?在上面的示例中实现它仍然存在一些问题。
  • 好的,谢谢 - 现在我收到一个错误,提示“histog”未定义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多