第七次作业 numpy统计分布显示

import numpy as np
from sklearn.datasets import load_iris
#1.计算鸢尾花花瓣长度的最大值,平均值,中值,均方差。
data=load_iris()
print(data)
print(type(data))
print(data.keys(),data.feature_names)
iris=data.data
print(iris)
petal_length=iris[:,2]
print(petal_length)
print("最大值",np.max(petal_length))
print("平均值",np.mean(petal_length))
print("最小值",np.min(petal_length))
print("均方差",np.std(petal_length))
print("中值",np.median(petal_length))

  结果:

 

#2.用np.random.normal()产生一个正态分布的随机数组,并显示出来。
import numpy as np
import matplotlib.pyplot as plt
mu = 5  
sigma = 5  
num = 1000  
rand_data = np.random.normal(mu, sigma, num)
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()产生一个正态分布的随机数组,并显示出来。
Data=np.random.randn(30)
print(Data)

结果:

#4.显示鸢尾花花瓣长度的正态分布图
import numpy as np
import matplotlib.pyplot as plt

mu = np.mean(petal_length)  # 期望值
sigma = np.std(petal_length)  # 标准差
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,'y')
plt.show()

#散点图
plt.scatter(np.linspace(0,160,num=150),petal_length,alpha=1,marker='x',color='red')
plt.show()

 

转载于:https://www.cnblogs.com/zhanyuki/p/9824590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值