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()
output_5_0.png
mean = 950
std = 50
# 生成滿足正態(tài)分布的隨機數(shù)恤煞,并繪制直方圖
sample = np.random.normal(mean, std, size=365)
plt.hist(sample, bins=30, alpha=0.7, rwidth=0.9, normed=True)
plt.show()
output_6_0.png
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()
output_8_0.png
x = np.arange(700, 1200, 1)
y = norm.cdf(x)
plt.plot(x, y)
plt.show()
output_9_0.png
#繪制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)
#設(shè)置y軸范圍
plt.ylim(0,0.0085)
plt.show()
output_10_0.png
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)
#設(shè)置y軸范圍
plt.ylim(0,0.0085)
plt.show()
output_12_0.png
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)
#設(shè)置y軸范圍
plt.ylim(0,0.0085)
plt.show()
output_15_0.png
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()
output_23_0.png
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()
output_25_0.png
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()
output_28_0.png
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()
output_35_0.png
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
#基本作業(yè)
#機票超賣現(xiàn)象
#假設(shè)某國際航班有300個座位吴超,乘客平均誤機率是2%。
#1正蛙、如果一共賣出305張機票块饺,那么登機時人數(shù)超額的概率是多少逻锐?
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
#目前表示完全看不懂了。需要有人指點了窃判,里面的知識點完全不知道钞楼,只有先嘗試。第二遍的時候再認真的學袄琳。