计算鸢尾花花瓣长度的最大值,平均值,中值,均方差。
import numpy as npfrom sklearn.datasets import load_iris data = load_iris()petal_length = data['data'][:,2]#鸢尾花花瓣的长度值print(petal_length)data1 = np.max(petal_length) #鸢尾花花瓣的长度最大值data2 = np.min(petal_length) #最小值data3 = np.mean(petal_length) #均值data4 = np.std(petal_length) #标准差(均方值)data5 = np.median(petal_length) #中位数print('最大值:',data1,'最小值:',data2,'均值:',data3,'均方值:',data4,'中位数:',data5)
print(np.random.normal(1,5,30)) # 用np.random.normal()产生一个正态分布的随机数组,并显示出来print(np.random.randn(5,5)) # np.random.randn()产生一个正态分布的随机数组,并显示出来
#显示鸢尾花花瓣长度的正态分布图import numpy as npimport matplotlib.pyplot as pltmu = np.mean(petal_length) # 期望值sigma = np.std(petal_length) # 标准差num = 10000 #个数为10000rand_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()
# 显示鸢尾花花瓣长度的散点图plt.scatter(np.linspace(0,160,num=150),petal_length,alpha=1,marker='x',color='green')plt.show()
print(np.arange(5)) #list(range(5))print(np.array([3,8]))print(np.arange(0,50,5))print(np.linspace(0,30)) #在指定的间隔内返回均匀间隔的数字。print(np.random.random(10)) #(0,1)以内10个随机浮点数print(np.random.randint(1,100,[5,5])) #(1,100)以内的5行5列随机整数print(np.random.rand(2,3)) #产生2行3列均匀分布随机数组print(np.random.randn(3,3)) #3行3列正态分布随机数据