课堂练习--计算数组的最大值,最小值,平均值,标准差,中位数;numpy.random模块提供了产生各种分布随机数的数组;正态分布;Matplotlib

#计算数组的最大值,最小值,平均值,标准差,中位数
import numpy as np
a=np.array([1, 4, 2, 5, 3, 7, 9, 0])
print(a)

a1=np.max(a)  #最大值
print(a1)
a2=np.min(a)  #最小值
print(a2)
a3=np.mean(a) #平均值
print(a3)
a4=np.std(a)  #标准差
print(a4)
a5=np.median(a) #中位数
print(a5)

#numpy.random模块提供了产生各种分布随机数的数组

import
numpy as np a=np.arange(5) b=list(range(5)) print(a,b) c=np.array([a,b]) print(c) d=np.arange(0,60,5) .reshape(3,4) print(d) e=np.linspace(0,20) #在指定的间隔内返回均匀间隔的数字。 print(e) f=np.random.random(10) #(0,1)以内10个随机浮点数 print(f) g=np.random.randint(1,100,[5,5]) #(1,100)以内的5行5列随机整数 print(g) h=np.random.rand(2,3) #产生2行3列均匀分布随机数组 print(h) i=np.random.randn(3,3) #3行3列正态分布随机数据 print(i)

import numpy as np
import matplotlib.pyplot as plt

mu = 10  #期望为10
sigma = 30  #标准差为30
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()

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,20)
plt.plot(x,.5+x)
plt.plot(x,x**2,'r3--')
plt.show()

猜你喜欢

转载自www.cnblogs.com/a-zhuanger/p/9809011.html
今日推荐