1. 程式人生 > >numpy統計分析

numpy統計分析

方差 png http .data plot div eight pri ace

#計算鳶尾花的花瓣長度的最大值,均值,中值,均方差
from sklearn.datasets import load_iris
data=load_iris()
data_length=data.data[:,2]
print("最大值:",np.max(data_length),"\n","平均值:",np.mean(data_length),"\n","中值:",np.median(data_length),"\n","均方差:",np.std(data_length))

技術分享圖片

#用np.random.normal()產生一個正態分布的隨機數組,並顯示出來
np.random.normal(3,5,100)
import matplotlib.pyplot as plt mu=3 sigma=5 num=100 rand_data=np.random.normal(mu,sigma,num) print(rand_data.shape,type(rand_data)) count,bins,ignored=plt.hist(rand_data,30,normed=True) plt.plot(bins,1/(sigma*np.sqrt(2*np.pi))*np.exp(-(bins-mu)**2/(2*sigma**2)),linewidth=2,color=r) plt.show()

技術分享圖片

#用np.random.randn()產生一個正態分布的隨機數組,並顯示出來
n1=np.random.randn(4,4) mu=np.median(n1) sigma=np.std(n1) import matplotlib.pyplot as plt mu=np.median(n1) sigma=5 num=np.std(n1) print(n1.shape,type(n1)) count,bins,ignored=plt.hist(n1,15,normed=True) plt.plot(bins,1/(sigma*np.sqrt(2*np.pi))*np.exp(-(bins-mu)**2/(2*sigma**2)),linewidth=2,color=r) plt.show()
##顯示鳶尾花花瓣長度的正態分布圖
import matplotlib.pyplot as plt
mu=np.median(data_length)
sigma=np.std(data_length)
num=data_length.shape
rand_data=np.random.normal(mu,sigma,num)
print(rand_data.shape,type(rand_data))
count,bins,ignored=plt.hist(rand_data,30,normed=True)
plt.plot(bins,1/(sigma*np.sqrt(2*np.pi))*np.exp(-(bins-mu)**2/(2*sigma**2)),linewidth=2,color=r)
plt.show()

技術分享圖片

#鳶尾花花瓣的曲線圖,
import matplotlib.pyplot as plt
y=np.linspace(0,150)
x=data_length
plt.plot(np.linspace(0,150,num=150),data_length,"g")
plt.show()

技術分享圖片

#鳶尾花花瓣的散點圖,
import matplotlib.pyplot as plt
y=np.linspace(0,150)
x=data_length
plt.scatter(np.linspace(0,150,num=150),data_length,alpha=0.5,marker="+")
plt.show()

技術分享圖片

numpy統計分析