Histogram Manipulation To Remove Unwanted Data
How do I remove data from a histogram in python under a certain frequency count? Say I have 10 bins, the first bin has a count of 4, the second has 2, the third has 1, fourth has
Solution 1:
Une np.histogram
to create the histogram.
Then use np.where
. Given a condition, it yields an array of booleans you can use to index your histogram.
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")
(Plot part inspired from here.)
Post a Comment for "Histogram Manipulation To Remove Unwanted Data"