首页 > 其他 > 详细

10.18作业

时间:2018-10-21 21:19:41      阅读:230      评论:0      收藏:0      [点我收藏+]

numpy统计分布显示

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

 

import numpy as np
from sklearn.datasets import load_iris
data = load_iris()
petal_length = data[data][:,2]
print(最大值:,np.max(petal_length))
print(最小值:,np.min(petal_length))
print(均值:,np.mean(petal_length))
print(均方差:,np.std(petal_length))
print(中位数,np.median(petal_length)

 

技术分享图片

 

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

 

print(np.random.normal(1,5,50))#用np.random.normal()产生一个正态分布的随机数组,并显示出来

 

技术分享图片

 

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

 

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

 

技术分享图片

 

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

(1)鸢尾花花瓣长度的正态分布图

 

import matplotlib.pyplot as plt
mu = np.mean(petal_length)
sigma = np.std(petal_length)
num = 10000
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()

 

import matplotlib.pyplot as plt
mu = np.mean(petal_length)
sigma = np.std(petal_length)
num = 10000
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()

技术分享图片

(2)鸢尾花花瓣长度的曲线图

plt.plot(np.linspace(0,150,num=150),petal_length,b)
plt.show()

技术分享图片

(3)鸢尾花花瓣长度的散点图

plt.scatter(np.linspace(1,160,num=150),petal_length,alpha=1,marker=x,color=g)
plt.show()

技术分享图片

10.18作业

原文:https://www.cnblogs.com/yvettecheng/p/9826658.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!