1.计算鸢尾花花瓣长度的最大值,平均值,中值,均方差。

#计算鸢尾花花瓣长度最大值
import numpy as np
from sklearn.datasets import load_iris
data = load_iris()

petal_length=numpy.array(list(len[2]for len in data['data']))
print(np.max(petal_length))
print(np.mean(petal_length))
print(np.std(petal_length))
print(np.median(petal_length))
 

运行结果:

 

 

2.用np.random.normal()产生一个正态分布的随机数组,并显示出来。

np.random.normal(1,5,60)

运行结果:

 

 

 

3.np.random.randn()产生一个正态分布的随机数组,并显示出来。

 

np.random.randn(3,3)#3行3列正态分布随机数组

运行结果:

 4.显示鸢尾花花瓣长度的正态分布图,曲线图,散点图。

#正态分布图

import numpy as np import matplotlib.pyplot as plt mu = 1 #期望为1 sigma = 3 #标准差为3 num = 10000 #个数为10000 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()

运行结果:

 

 

#曲线图
plt.plot(np.linspace(0,150,num=150),petal_length,'r') plt.show()

运行结果:

 

 


#散点图

import numpy as np import matplotlib.pyplot as plt plt.scatter(np.linspace(0,150,num=150),petal_length,alpha=0.5,marker='x') plt.show()

运行结果:

 

posted on 2018-10-18 11:41  duola-ling  阅读(951)  评论(0编辑  收藏  举报