numpy統計分布顯示


#加載numpy包
import numpy
#加載sklearn包
from sklearn.datasets import load_iris 
#讀出鳶尾花數據集data
data=load_iris()
print(data)

運行結果如下:

#鳶尾花花瓣長度的數據
petal_length=numpy.array(list(len[2] for len in data['data'])) 
print(petal_length)

運行結果如下:

#計算鳶尾花花瓣長度的最大值,平均值,中值,均方差
print(numpy.max(petal_length))
print(numpy.mean(petal_length))
print(numpy.median(petal_length))
print(numpy.std(petal_length))

運行結果如下:

 

#用np.random.normal()產生一個正態分布的隨機數組,並顯示出來
import numpy as np
import matplotlib.pyplot as plt
mu = 5  #期望為5
sigma = 3  #標准差為3
num = 1000  #個數為1000
normal_data = np.random.normal(mu, sigma, num)
print(normal_data)

運行結果如下:

 

#用np.random.randn()產生一個正態分布的隨機數組,並顯示出來
a=np.random.random(25)
print(a)

運行結果如下:

 

#顯示鳶尾花花瓣長度的正態分布圖,曲線圖,散點圖
import numpy as np
import matplotlib.pyplot as plt

mu=np.mean(petal_length)   
sigma =np.std(petal_length)  
num=1000

normal_data = np.random.normal(mu, sigma, num)
count, bins, ignored = plt.hist(normal_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='orange')
plt.show()

運行結果如下:

plt.plot(numpy.linspace(0,150,num=150),petal_length,color='orange')
plt.show()

運行結果如下:

plt.scatter(numpy.linspace(0,150,num=150),petal_length,color='orange')
plt.show()

運行結果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM