正态分布曲线的拟合

2024-05-08 17:00:34 发布

您现在位置:Python中文网/ 问答频道 /正文

这是字典:

l= {31.2: 1,35.1: 4,39.0: 13,42.9: 33,46.8: 115,50.7: 271,54.6: 363,58.5:381,62.4:379,66.3:370,70.2:256,74.1: 47,78.0: 2}

这意味着31.2已经发生了1次,35.1已经发生了4次,以此类推。 我试过:

fig, ax = plt.subplots(1, 1)

ax.scatter(l.keys(), l.values)
ax.set_xlabel('Key')
ax.set_ylabel('Length of value')

enter image description here

我还发现了mean和std

np.mean([k for k in l.keys()])
np.std([k for k in l.keys()])

这是找到数据平均值和标准差的方法吗。我对此表示怀疑,因为它没有考虑每个数据的出现次数。我想看看这个数据的法向曲线。还有一种方法可以知道值出现的频率。例如,如果我将曲线延伸到x轴上的0,如果我想知道0的出现涉及多少数据点(也可以是概率)


Tags: 数据方法infor字典npfigplt
2条回答

以下是绘制法向高斯曲线以拟合数据的方法:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats

l = {31.2: 1, 35.1: 4, 39.0: 13, 42.9: 33, 46.8: 115, 50.7: 271, 54.6: 363, 58.5: 381, 62.4: 379, 66.3: 370, 70.2: 256, 74.1: 47, 78.0: 2}
# convert the dictionary to a list
l_list = [k for k, v in l.items() for _ in range(v)]

fig, ax = plt.subplots(1, 1)

ax.scatter(l.keys(), l.values())
ax.set_xlabel('Key')
ax.set_ylabel('Length of value')

mu = np.mean(l_list)
sigma = np.std(l_list)

u = np.linspace(mu - 4 * sigma, mu + 4 * sigma, 100)
ax2 = ax.twinx()
ax2.plot(u, stats.norm.pdf(u, mu, sigma), color='crimson')
ax2.set_ylabel('normal curve')

plt.show()

enter image description here

以下是获得平均值和标准差的方法:

l= {31.2: 1,35.1: 4,39.0: 13,42.9: 33,46.8: 115,50.7: 271,54.6: 363,58.5:381,62.4:379,66.3:370,70.2:256,74.1: 47,78.0: 2}
ll=[[i]*j for i,j in zip(l.keys(),l.values())]
flat_list = [item for sublist in ll for item in sublist]
np.mean(flat_list), np.std(flat_list)

打印(59.559194630872476, 7.528353520785996)

你可以用np.histogram(flat_list)做一个直方图来评估每次发生的频率

相关问题 更多 >

    热门问题