python绘制密度函数 python画密度散点图( 四 )


β分布(Beta Distribution)
β分布是一个取值在 [0, 1] 之间的连续分布 , 它由两个形态参数α和β的取值所刻画 。
β分布的形状取决于α和β的值 。贝叶斯分析中大量使用了β分布 。
当你将参数α和β都设置为1时,该分布又被称为均匀分布(uniform distribution) 。尝试不同的α和β取值,看看分布的形状是如何变化的 。
指数分布(Exponential Distribution)
指数分布是一种连续概率分布,用于表示独立随机事件发生的时间间隔 。比如旅客进入机场的时间间隔、打进客服中心电话的时间间隔、中文维基百科新条目出现的时间间隔等等 。
我将参数λ设置为0.5,并将x的取值范围设置为 $[0, 15]$。
接着,我在指数分布下模拟1000个随机变量 。scale参数表示λ的倒数 。函数np.std中,参数ddof等于标准偏差除以 $n-1$ 的值 。
结语(Conclusion)
概率分布就像盖房子的蓝图,而随机变量是对试验事件的总结 。我建议你去看看哈佛大学数据科学课程的讲座 , Joe Blitzstein教授给了一份摘要,包含了你所需要了解的关于统计模型和分布的全部 。
python制作分布图制作分布图类似密度图,在python中利用pandas来提取分布数据是比较方便的 。主要用到pandas的cut和groupby等函数 。
官方文档链接
主要参数为x和bins 。
x为数据源,数组格式的都支持,list,numpy.narray, pandas.Series 。
bins可以为int,也可以为序列 。
我们定义bins为一个序列,默认为左开右闭的区间:
对言值列按cats做groupby,然后调用get_stats统计函数,再用unstack函数将层次化的行索引“展开”为列 。
G2在之前的文章中有介绍,文章 《python结合G2绘制精美图形》。
一句话绘制出来,但具体的区间段难以区分出来 。
bokeh是python的一个优秀的绘图工具包,与pandas结合的比较好 。bokeh文档
作者原文链接: python制作分布图
python 绘制和密度图笔记import pandasas pd
import numpyas np
import seabornas sns
import matplotlib.pyplotas plt
pd.set_option('display.max_columns', 10000)
pd.set_option('display.max_rows', 10000000000)
pd.set_option('display.width', 100000)
income = pd.read_excel(r'D:\bigData\0629demo\dataSource\income.xlsx')
fill_data = https://www.04ip.com/post/income.fillna(value={'workclass': income.workclass.mode()[0], 'occupation': income.occupation.mode()[0],
'native-country': income['native-country'].mode()[0]}, inplace=True)
# print(income.apply(lambda x: np.sum(x.isnull())))
# print(income)
print(income.describe())
print(income.describe(include=['object']))
# 设置绘图风格
plt.style.use('ggplot')
# 设置多图形组合
fig, axes = plt.subplots(2, 1)
# 绘制不同收入水平下的年龄核密度图
# kind='kde', label='=50K', ax=axes[0], legend=True, linestyle='-'
# kind='kde', label='50K', ax=axes[0], legend=True, linestyle='--'
income['age'][income.income ==' =50K'].plot(kind='kde', ax=axes[0], label='=50K', legend=True, linestyle='-')
income['age'][income.income ==' 50K'].plot(kind='kde', ax=axes[0], label='50K', legend=True, linestyle='--')
# 绘制不同收入水平下的周工作小时数核密度图
# kind='kde', label='= 50K', ax=axes[1], legend=True,linestyle='-'
# kind='kde', label=' 50K', ax=axes[1], legend=True, linestyle='--'
income['hours-per-week'][income.income ==' =50K'].plot(kind='kde', label='= 50K', ax=axes[1], legend=True,
linestyle='-')
income['hours-per-week'][income.income ==' 50K'].plot(kind='kde', label=' 50K', ax=axes[1], legend=True,
linestyle='--')
plt.show()
# 构造不同收入水平下各种族人数的数据