python课后练习(9.2-9.3)
1
点击查看代码
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
data = [
15.0, 15.8, 15.2, 15.1, 15.9, 14.7, 15.5, 15.6, 15.3,
15.1, 15.3, 15.0, 15.6, 15.7, 14.8, 14.5, 14.2, 14.9,
15.2, 15.0, 15.3, 15.6, 15.7, 14.9, 14.2, 14.5, 14.9,
15.0, 15.2, 15.1, 14.9, 15.1, 15.3, 15.0, 14.8, 14.6,
15.3, 15.0, 15.2, 15.5, 14.6, 15.1, 14.5, 15.0, 14.7,
14.2, 15.9, 15.1, 15.3, 14.7, 14.5, 15.0, 14.6, 15.1,
15.2, 15.8, 15.0, 15.3, 15.1, 15.6, 15.6, 15.5, 15.2,
15.8, 15.3, 14.5, 15.1, 15.0, 15.1, 15.8, 15.1
]
plt.figure(figsize=(10, 6))
sns.histplot(data, bins=10, kde=True)
plt.title('直方图与密度估计')
plt.xlabel('直径 (mm)')
plt.ylabel('频率')
plt.show()
plt.figure(figsize=(10, 6))
stats.probplot(data, dist="norm", plot=plt)
plt.title('Q-Q图')
plt.xlabel('理论分位数')
plt.ylabel('样本分位数')
plt.show()
stat, p_value = stats.shapiro(data)
print(f'Statistic: {stat}, p-value: {p_value}')
alpha = 0.05
if p_value > alpha:
print("样本可能来自正态分布 (无法拒绝零假设)")
else:
print("样本不来自正态分布 (拒绝零假设)")
print("学号后四位:3032")
点击查看代码
import numpy as np
import statsmodels.api as sm
import csv
import pandas as pd
y = np.array([1.13,1.07,4.04,4.07,4.05,4.04,4.02,4.06,4.10,4.04,
3.86,3.85,4.08,4.11,4.08,4.01,4.02,4.04,3.97,3.95,
4.00,4.02,4.01,4.01,4.04,3.99,4.03,3.97,3.98,3.98,
3.88,3.88,3.91,3.95,3.92,3.97,3.92,3.90,3.97,3.90,
4.02,3.95,4.02,3.89,3.91,4.01,3.89,3.89,3.99,4.00,
4.02,3.86,3.96,3.97,4.00,3.82,3.98,3.99,4.02,3.93,
4.00,4.02,4.03,4.04,4.10,3.81,3.91,3.96,4.05,4.06
])
x=np.hstack([np.full(10,1), np.full(10,2), np.full(10,3),
np.full(10,4), np.full(10,5), np.full(10,6), np.full(10,7)])
d = {'x':x, 'y':y}
model = sm.formula.ols("y~C(x)", d).fit()
anovat = sm.stats.anova_lm(model)
print(anovat)
print("学号后四位:3032")