连续数据的图拟合分布失败
plot fitted distribution of continuous data fails
我正在尝试重现 the example given in this answer 以适应我的真实数据的分布,例如 df['Note']
是我的数据框的一列,其中包含每个学生的平均笔记:
Index StudentName Note
0 Mark 3.7
1 Bryan 3.5
2 Nil 0.1
3 Amanda 2.045
4 Arthur 1.2
5 Helen 2.5
真实的数据帧包含大约 350000 行,平均值 = 2.17,如这个
所以我的代码获得了拟合分布
def fit(dataframe, path):
dataframe.set_index
size=len(dataframe.index)
x=dataframe.index
y=dataframe['Note']
plt.hist(y, bins=range(20))
dist_names = ['alpha', 'beta', 'norm', 'expon']
for dist_name in dist_names:
dist = getattr(scipy.stats, dist_name)
param = dist.fit(y)
pdf_fitted = dist.pdf(x, *param[:-2], loc=param[-2], scale=param[-1]) * size
plt.plot(pdf_fitted, label=dist_name)
plt.xlim(0, 19)
plt.legend(loc='upper left')
plt.show()
plt.savefig(path+'_fit.png', bbox_inches='tight', dpi=100)
def call_fit(pathname):
path_picture="%spicture//" %pathname
path="%data//" %pathname
path_s=sorted(os.listdir(path))
for i in path_s:
file_path=os.path.join(path, i)
picture=os.path.join(path_picture, i)
df=pd.read_csv(file_path, sep='\t')
fit(df, picture)
但是输出很奇怪。我在这里放了两个函数,因为我不确定哪个是错误的原因!不用说,分布有一个奇怪的外观,而且我的真实分布的直方图不是绘图,但每次迭代都会发生一些奇怪的事情:看看并比较第一个图和第 10 个图:
非常感谢您的帮助和建议!因为我不知道我哪里错了
分布的奇怪外观可能是由于您的数据不能很好地拟合分布(尤其是 alpha 分布)。由于我没有您的数据,因此无法进一步详细说明。你提供的样品看起来不错。
已绘制直方图,但与 1e29 相比,条形图太小,因此您看不到它们。
图例和其他绘图元素正在累积,因为您总是在同一个子图中绘图而不清除它,因此在每次迭代中您都会保留之前绘制的所有内容。
我正在尝试重现 the example given in this answer 以适应我的真实数据的分布,例如 df['Note']
是我的数据框的一列,其中包含每个学生的平均笔记:
Index StudentName Note
0 Mark 3.7
1 Bryan 3.5
2 Nil 0.1
3 Amanda 2.045
4 Arthur 1.2
5 Helen 2.5
真实的数据帧包含大约 350000 行,平均值 = 2.17,如这个
所以我的代码获得了拟合分布
def fit(dataframe, path):
dataframe.set_index
size=len(dataframe.index)
x=dataframe.index
y=dataframe['Note']
plt.hist(y, bins=range(20))
dist_names = ['alpha', 'beta', 'norm', 'expon']
for dist_name in dist_names:
dist = getattr(scipy.stats, dist_name)
param = dist.fit(y)
pdf_fitted = dist.pdf(x, *param[:-2], loc=param[-2], scale=param[-1]) * size
plt.plot(pdf_fitted, label=dist_name)
plt.xlim(0, 19)
plt.legend(loc='upper left')
plt.show()
plt.savefig(path+'_fit.png', bbox_inches='tight', dpi=100)
def call_fit(pathname):
path_picture="%spicture//" %pathname
path="%data//" %pathname
path_s=sorted(os.listdir(path))
for i in path_s:
file_path=os.path.join(path, i)
picture=os.path.join(path_picture, i)
df=pd.read_csv(file_path, sep='\t')
fit(df, picture)
但是输出很奇怪。我在这里放了两个函数,因为我不确定哪个是错误的原因!不用说,分布有一个奇怪的外观,而且我的真实分布的直方图不是绘图,但每次迭代都会发生一些奇怪的事情:看看并比较第一个图和第 10 个图:
非常感谢您的帮助和建议!因为我不知道我哪里错了
分布的奇怪外观可能是由于您的数据不能很好地拟合分布(尤其是 alpha 分布)。由于我没有您的数据,因此无法进一步详细说明。你提供的样品看起来不错。
已绘制直方图,但与 1e29 相比,条形图太小,因此您看不到它们。
图例和其他绘图元素正在累积,因为您总是在同一个子图中绘图而不清除它,因此在每次迭代中您都会保留之前绘制的所有内容。