我试图估计我的数据的概率密度函数。在我的例子中,数据是一个形状为8200 x 8100的卫星图像。下面,我向您介绍PDF的代码(函数'is_outlier‘是由一个家伙借用的,他在这里发布了这个代码)。正如我们所看到的,PDF在图1中太密集了。我想,这是由于卫星图像由成千上万的像素组成的。这很难看。
我的问题是,我怎样才能绘制一个不太密集的PDF?例如,如图2所示。
lst = 'satellite_img.tif' #import the image
lst_flat = lst.flatten() #create 1D array
#the function below removes the outliers
def is_outlier(points, thres=3.5):
if len(points.shape) == 1:
points = points[:,None]
median = np.median(points, axis=0)
diff = np.sum((points - median)**2, axis=-1)
diff = np.sqrt(diff)
med_abs_deviation = np.median(diff)
modified_z_score = 0.6745 * diff / med_abs_deviation
return modified_z_score > thres
lst_flat = np.r_[lst_flat]
lst_flat_filtered = lst_flat[~is_outlier(lst_flat)]
fit = stats.norm.pdf(lst_flat_filtered, np.mean(lst_flat_filtered), np.std(lst_flat_filtered))
plt.plot(lst_flat_filtered, fit)
plt.hist(lst_flat_filtered, bins=30, normed=True)
plt.show()
图1
图2
发布于 2017-09-07 16:23:08
问题是PDF图中的x值没有排序,因此绘制的线在随机点之间来回移动,造成了您所看到的混乱。
有两种选择:
lst_flat_filtered
数组并绘制它之前,对其进行排序:
lst_flat = np.r_lst_flat lst_flat_filtered = np.sort(lst_flat~is_outlier(lst_flat)) #改变了该行fit = stats.norm.pdf(lst_flat_filtered,np.mean(lst_flat_filtered),np.std(lst_flat_filtered)) plt.plot(lst_flat_filtered,fit)下面是一些显示这些行为的最小示例:
重复你的问题:
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
lst_flat_filtered = np.random.normal(7, 5, 1000)
fit = stats.norm.pdf(lst_flat_filtered, np.mean(lst_flat_filtered), np.std(lst_flat_filtered))
plt.hist(lst_flat_filtered, bins=30, normed=True)
plt.plot(lst_flat_filtered, fit)
plt.show()
绘图点
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
lst_flat_filtered = np.random.normal(7, 5, 1000)
fit = stats.norm.pdf(lst_flat_filtered, np.mean(lst_flat_filtered), np.std(lst_flat_filtered))
plt.hist(lst_flat_filtered, bins=30, normed=True)
plt.plot(lst_flat_filtered, fit, 'bo')
plt.show()
数据排序
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
lst_flat_filtered = np.sort(np.random.normal(7, 5, 1000))
fit = stats.norm.pdf(lst_flat_filtered, np.mean(lst_flat_filtered), np.std(lst_flat_filtered))
plt.hist(lst_flat_filtered, bins=30, normed=True)
plt.plot(lst_flat_filtered, fit)
plt.show()
https://stackoverflow.com/questions/46100208
复制相似问题