numpy统计分布

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

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

运行结果:

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

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()

运行结果:

 

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

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

运行结果:

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

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,50)
y = x**2
plt.plot(x,y)

plt.show()

petal_length
mu = np.mean(petal_length)
sigma = np.std(petal_length)
print(np.mean(petal_length),np.std(petal_length),np.median(petal_length))

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.plot(np.linspace(0,150,num=150),petal_length,'r')
plt.scatter(np.linspace(0,150,num=150),petal_length,alpha=0.5,marker='x')
plt.show()





import numpy as np
import matplotlib.pyplot as plt

 
 

t = np.arange(0., 1., 0.02)
plt.plot(t,sin(t),'r--',t, t**3, 'b^', t, t**2, 'gs')
#plt.show()




猜你喜欢

转载自www.cnblogs.com/844115-l/p/9809595.html