import scipy.stats
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%config InlineBackend.figure_format = 'retina'
np.random.seed(123)
np.random.random(5)
array([ 0.69646919, 0.28613933, 0.22685145, 0.55131477, 0.71946897])
np.random.randint(0,9,10)
array([6, 1, 0, 1, 0, 0, 3, 4, 0, 0])
num = 1000
x = np.random.random(num)
y = np.random.random(num)
pi = np.sum(x**2 + y**2 < 1) / num * 4
print('PI:', pi)
PI: 3.196
plt.figure(figsize=(5,5))
plt.scatter(x,y, alpha = 0.6)
plt.axis([0,1,0,1])
x2 = np.arange(0, 1.01, 0.01)
y2 = np.sqrt(1 - x2**2)
plt.plot(x2, y2, 'm', lw=3)
plt.show()
mean = 950
std = 50
# 生成满足正态分布的随机数,并绘制直方图
sample = np.random.normal(mean, std, size=365)
plt.hist(sample, bins=30, alpha=0.7, rwidth=0.9, normed=True)
plt.show()
mean = 950
std = 50
norm = scipy.stats.norm(mean, std)
x = np.arange(700, 1200, 1)
y = norm.pdf(x)
plt.plot(x, y)
plt.show()
x = np.arange(700, 1200, 1)
y = norm.cdf(x)
plt.plot(x, y)
plt.show()
#绘制PDF曲线
x = np.arange(700, 1200, 1)
y = norm.pdf(x)
plt.plot(x, y)
#在1000处绘制竖线
plt.vlines(1000, 0, norm.pdf(1000))
#填充颜色
x2 = np.arange(700, 1000, 1)
y2 = norm.pdf(x2)
plt.fill_between(x2, y2, color='blue', alpha=0.1)
#设置y轴范围
plt.ylim(0,0.0085)
plt.show()
norm.cdf(1000)
0.84134474606854293
#绘制PDF曲线
x = np.arange(700, 1200, 1)
y = norm.pdf(x)
plt.plot(x, y)
#绘制竖线
plt.vlines(1000, 0, norm.pdf(1000))
#填充颜色
x2 = np.arange(1000, 1200, 1)
y2 = norm.pdf(x2)
plt.fill_between(x2, y2, color='blue', alpha=0.1)
#设置y轴范围
plt.ylim(0,0.0085)
plt.show()
1 - norm.cdf(1000)
0.15865525393145707
norm.sf(1000)
0.15865525393145707
#绘制PDF曲线
x = np.arange(700, 1200, 1)
y = norm.pdf(x)
plt.plot(x, y)
#绘制竖线
plt.vlines(950, 0, norm.pdf(950))
plt.vlines(1050, 0, norm.pdf(1050))
#填充颜色
x2 = np.arange(950, 1050, 1)
y2 = norm.pdf(x2)
plt.fill_between(x2, y2, color='blue', alpha=0.1)
#设置y轴范围
plt.ylim(0,0.0085)
plt.show()
norm.cdf(1050) - norm.cdf(950)
0.47724986805182079
norm.ppf(0.9)
1014.07757827723
norm.isf(0.8)
907.91893832135429
outcome = np.random.randint(0,2,10)
outcome
array([1, 0, 0, 1, 1, 0, 1, 1, 0, 1])
np.sum(outcome)
6
sample = [np.sum(np.random.randint(0,2,10)) for i in range(10000)]
sample = pd.Series(sample)
sample.value_counts().sort_index().plot.bar()
plt.show()
n = 10
p = 0.5
binomial = scipy.stats.binom(n, p)
x = np.arange(0,11)
plt.plot(x, binomial.pmf(x), 'bo')
plt.vlines(x, 0, binomial.pmf(x), colors='b')
plt.ylim(0,0.3)
plt.show()
mean, var = binomial.stats()
print(mean)
print(var)
5.0
2.5
n = 100
p = 0.05
binom = scipy.stats.binom(n,p)
x = np.arange(0,101)
plt.plot(x, binom.pmf(x), 'bo')
plt.vlines(x, 0, binom.pmf(x), colors='b')
plt.ylim(0,0.2)
plt.show()
binom.pmf(5)
0.18001782727043672
1 - binom.cdf(4)
0.56401869931428927
binom.sf(4)
0.56401869931429105
binom.isf(0.1)
8.0
binom.ppf(1 - 0.1)
8.0
lmd = 20
poisson = scipy.stats.poisson(lmd)
x = np.arange(0,40)
plt.plot(x, poisson.pmf(x), 'bo')
plt.vlines(x, 0, poisson.pmf(x), colors='b')
plt.ylim(0,0.1)
plt.show()
mean, var = poisson.stats()
print(mean)
print(var)
20.0
20.0
poisson.pmf(20)
0.088835317392084806
poisson.cdf(15)
0.1565131346397429
poisson.sf(19)
0.5297427331607607
poisson.ppf(0.9)
26.0
#基本作业
#机票超卖现象
#假设某国际航班有300个座位,乘客平均误机率是2%。
#1、如果一共卖出305张机票,那么登机时人数超额的概率是多少?
mean = 300
std = 0.02
norm = scipy.stats.norm(mean, std)
norm.cdf(350)
1.0
#2、如果一共卖出305张机票,登机时最多只超额1人的概率是多少?
norm.cdf(306) - norm.cdf(305)
0.0
#3、一共卖几张票,可以保证不超额的概率至少是90%。
binom.isf(0.9)
2.0
#目前表示完全看不懂了。需要有人指点了,里面的知识点完全不知道,只有先尝试。第二遍的时候再认真的学。