例9.1
from scipy. stats import expon, gamma
import pylab as plt
x = plt. linspace(0, 3, 100)
L=[1/3,1,2]
s1 = ['*-','.-','o-']
s2 = ['$\\alpha=1,\\beta=\\frac{1} {3}$','$\\alpha=1, \\beta=1 $', '$\\alpha=1,\\beta=2$']
s3=['$\\theta=\\frac{1}{3}$','$\\theta=1$','$\\theta=2$']
plt.rc('text', usetex=False) ;plt. rc('font', size=15)
plt.subplots_adjust(wspace=0.5)
plt. subplot(121)
for i in range(len(L)):
plt.plot(x, gamma.pdf(x, 1, scale=L[i]), s1[i], label=s2[i])
plt.xlabel('$x $');plt.ylabel('$f(x)$');plt.legend()
plt. subplot(122)
for i in range(len(L)):
plt.plot(x,expon.pdf(x,scale=L[i]), s1[i], label=s3[i])
plt.xlabel('$x $');plt.ylabel('$f(x)$')
plt. legend();plt.show()
9标准正态分布
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
x = np.linspace(-3, 3, 100)
s1 = ['*-', '.-', 'o-']
s2 = ['$\\mu=0, \\sigma=1$'] * 3
L = [1, 1, 1]
plt.rc('text', usetex=False)
plt.rc('font', size=15)
plt.subplots_adjust(wspace=0.5)
plt.subplot(121)
for i in range(len(L)):
plt.plot(x, norm.pdf(x, loc=0, scale=L[i]), s1[i], label=s2[i])
plt.xlabel('$x$')
plt.ylabel('$f(x)$')
plt.title('Standard Normal Distribution')
plt.legend()
plt.subplot(122)
for i in range(len(L)):
plt.plot(x, norm.cdf(x, loc=0, scale=L[i]), s1[i], label=s2[i])
plt.xlabel('$x$')
plt.ylabel('$F(x)$')
plt.title('Cumulative Distribution Function')
plt.legend()
plt.show()
print(3035)
习题9.2
import numpy as np
import pandas as pd
import scipy.stats as ss
import statsmodels.api as sm
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
data = np.loadtxt("C:/Users/qazws/OneDrive/python代码/python建模作业/ti9_2.txt").flatten()
mu = data.mean()
sig = data.std(ddof=1)
st, p = ss.kstest(data, 'norm', (mu, sig))
print("统计量:", st)
print("p值:", p)
print(3035)
习题9.3
import numpy as np
import pandas as pd
import scipy.stats as ss
import statsmodels.api as sm
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
df = pd.read_excel("C:/Users/qazws/OneDrive/python代码/python建模作业/ti9_3.xlsx", header=None)
fig = plt.figure()
ax = fig.add_subplot(111)
labels = ['Lab'+str(i) for i in range(1, 8)]
ax.boxplot(df, sym='xr', labels=labels)
fig.show()
y = df.values
x = np.tile(np.arange(1, 8), (10, 1))
model_dic = {'x': x.flatten(), 'y': y.flatten()}
model = sm.formula.ols('y~C(x)', model_dic).fit()
anovat = sm.stats.anova_lm(model)
print(anovat)
print(3035)
习题9.4
import numpy as np
import pandas as pd
import scipy.stats as ss
import statsmodels.api as sm
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
data = np.loadtxt("C:/Users/qazws/OneDrive/python代码/python建模作业/ti9_4.txt")
x1 = np.tile(np.arange(1, 4), (12, 1)).T
x2 = np.tile(np.array([1,1,1,2,2,2,3,3,3,4,4,4]), (3, 1))
model_dic = {'x1': x1.flatten(), 'x2': x2.flatten(), 'y': data.flatten()}
model = sm.formula.ols('y~C(x1)*C(x2)', model_dic).fit()
print(sm.stats.anova_lm(model))
print(3035)
习题9.5
import numpy as np
import pandas as pd
import scipy.stats as ss
import statsmodels.api as sm
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='stix'
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['figure.dpi'] = 200
plt.rcParams['xtick.direction']='in'
plt.rcParams['ytick.direction']='in'
data = np.loadtxt("C:/Users/qazws/OneDrive/python代码/python建模作业/ti9_5.txt")
x1 = np.vstack([np.ones((4, 4)), np.ones((4, 4))*2, np.ones((4, 4))*3])
x2 = np.tile(np.array([[1, 1, 2, 2]]).T, (3, 4))
x3 = np.tile(np.array([[1, 2]]).T, (6, 4))
model_dic = {
'x1': x1.flatten(),
'x2': x2.flatten(),
'x3': x3.flatten(),
'y': data.flatten()
}
model = sm.formula.ols('y~C(x1)+C(x2)+C(x3)', model_dic).fit()
print(sm.stats.anova_lm(model))
print(3035)
标签:plt,第九章,flatten,rcParams,np,import,习题,例题,model
From: https://www.cnblogs.com/miling/p/18627451