python概率编程tmp

win10安装anaconda后, 安装pymc,命令行

conda install -c https://conda.binstar.org/pymc pymc

安装matplotlib, 命令行

conda install matplotlib

进入ipython:

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
fig=plt.figure()
ax1=fig.add_subplot(2,2,1)
ax2=fig.add_subplot(2,2,2)
ax3=fig.add_subplot(2,2,3)
ax1.plot([1,2,3,4])
ax2.plot([1,2,3,5])
#ax3.scatter(np.arange(30), np.arange(30)+3*randn(30))
ax3.plot([2,2,3,5])



fig, axes=plt.subplots(2, 2, sharex=True, sharey=True)
for i in range(2):
	for j in range(2):
		axes[i, j].hist(randn(500), bins=50, color='k', alpha=0.5)  #柱状图
plt.subplots_adjust(wspace=0, hspace=0)


x=np.linspace(-1,1,5)
y=x+1
plt.xlim((-1,5))  #x坐标的范围,从-1到5
new_ticks = np.linspace(-1,2,5)
plt.xticks(new_ticks) #x坐标上标尺只显示-1到2, 但整个x轴的范围仍是-1到5
plt.plot(x,y,'ob-')



x = np.random.randn(1000)
y1 = np.random.randn(len(x))
y2 = 1.8 + np.exp(x)
ax1 = plt.subplot(1,2,1)
ax1.scatter(x,y1,color='r',alpha=.3,edgecolors='white',label='no correl')
plt.xlabel('no correlation')
plt.grid(True)
plt.legend()
ax2 = plt.subplot(1,2,2) #显示在第二幅图上
ax2.scatter(x,y2,color='g',alpha=.3,edgecolors='gray',label='correl')  #alpha透明度 edgecolors边缘颜色 label图例(结合legend使用)
plt.xlabel('correlation')
plt.grid(True)
plt.legend() #显示图例, 图例的文字描述是上面label参数指定
plt.show()


x = np.random.randn(1000)
y1 = np.random.randn(len(x))
y2 = 1.8 + np.exp(x)
ax1 = plt.subplot()
#下面三个散点图都显示在一幅图上
ax1.scatter(x,y1,color='r',alpha=.3,edgecolors='white',label='no correl')
ax1.scatter(x,y2,color='g',alpha=.3,edgecolors='gray',label='correl')  
ax1.scatter(x,10+x,color='b',alpha=.3,edgecolors='gray',label='dong')
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.grid(True)
plt.legend() 



from pylab import *
figure(1, figsize=(6,6))
ax = axes([0.1,0.1,0.8,0.8])
labels ='spring','summer','autumn','winter'
x=[15,30,45,10]
#explode=(0.1,0.2,0.1,0.1)
explode=(0.1,0,0,0)
pie(x, explode=explode, labels=labels, autopct='%1.1f%%', startangle=67)  #饼状图
title('rainy days by season')
show()
//------------------------------------
import numpy as np
import math
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipy import stats
 
#设置显示的字体,否则会乱码
mpl.rcParams['font.sans-serif'] = [u'SimHei']  #黑体,也可以是FangSong/KaiTi等电脑上的字体
mpl.rcParams['axes.unicode_minus'] = False  #对坐标轴上的减号不进行设置,否则会乱码
 
mu = 0
sigma = 1
#取51个点,把中间的0包含进去
x = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 51)
#手动计算概率密度值
y = np.exp(-(x - mu) ** 2 / (2 * sigma ** 2)) / (math.sqrt(2 * math.pi) * sigma)
#背景白色
plt.figure(facecolor = 'w')
#'g-'表示绿色实线绘制线条,'ro'表示红色圈绘制点;linewidth和markersize分别设置线的宽度和点的大小
plt.plot(x, y, 'g-', x, y, 'ro', linewidth = 2 , markersize = 8) #在同一个图上绘制线条和点
plt.xlabel('X', fontsize = 15)
plt.ylabel('Y', fontsize = 15)
plt.title(u'高斯分布', fontsize = 18)
plt.grid(True)
plt.show()
//------------------------------------
import numpy as np
import math
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipy import stats
 
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
#产生10000个均匀分布的随机值(不一定是均匀分布?),每个xi的值都在0~1之间
x = np.random.rand(10000)
plt.subplot(121) #'121'表示共一行两列,现在绘制第一幅
#bins参数表示横坐标划分为30个区域,alpha表示透明度
plt.hist(x, bins = 30, color = 'g', alpha = 0.5, label = u'均匀分布')
plt.legend(loc = 'upper right')
plt.grid()
//--------------------------------------
#验证中心极限定理
t = 1000  #叠加1000次
a = np.zeros(10000)
for i in range(t):
    a += np.random.uniform(-5, 5, 10000)
a /= t
plt.subplot(122) #绘制第二幅图
# normed参数使得概率密度和为1,即面积
his = plt.hist(a, bins = 30, color = 'g', normed = True, alpha = 0.5, label = u'均匀分布叠加')
#his是一个tuple,当normed为True时,his[0]表示对应的30个概率密度函数值数组。his[1]表是横坐标边界数组
#print 'area: \n', sum(his[0] * np.diff(his[1]))
plt.legend(loc = 'upper right')
plt.grid()
plt.show()

猜你喜欢

转载自blog.csdn.net/william_djj/article/details/84430655
tmp