#计算鸢尾花的花瓣长度的最大值,均值,中值,均方差 from sklearn.datasets import load_iris data=load_iris() data_length=data.data[:,2] print("最大值:",np.max(data_length)," ","平均值:",np.mean(data_length)," ","中值:",np.median(data_length)," ","均方差:",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()