学号后四位3020
9.2
点击查看代码
import numpy as np
from scipy.stats import shapiro
data = [15.0, 15.8, 15.2, 15.1, 15.9, 14.7, 14.8, 15.5, 15.6, 15.3,
15.1, 15.3, 15.0, 15.6, 15.7, 14.8, 14.5, 14.2, 14.9, 14.9,
15.2, 15.0, 15.3, 15.6, 15.1, 14.9, 14.2, 14.6, 15.8, 15.2,
15.9, 15.2, 15.0, 14.9, 14.8, 14.5, 15.1, 15.5, 15.5, 15.1,
15.1, 15.0, 15.3, 14.7, 14.5, 15.5, 15.0, 14.7, 14.6, 14.2]
stat, p = shapiro(data)
print(f"Shapiro-Wilk Statistic: {stat}")
print(f"P-value: {p}")
alpha = 0.05
if p > alpha:
print("滚珠直径数据服从正态分布N(15.0780, 0.4325^2)。")
else:
print("滚珠直径数据不服从正态分布N(15.0780, 0.4325^2)。")
print("学号后两位为:20")
9.3
点击查看代码
import numpy as np
import pandas as pd
import scipy.stats as stats
import matplotlib.pyplot as plt
# 创建数据
X = np.array([
4.13, 4.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
])
# 创建因子A
A = np.repeat(np.arange(1, 8), 10)
# 创建DataFrame
contents = pd.DataFrame({'X': X, 'A': A})
# 进行ANOVA分析
aov_cont = stats.f_oneway(*[contents[contents['A'] == i]['X'] for i in range(1, 8)])
print(f"F-statistic: {aov_cont.statistic}, p-value: {aov_cont.pvalue}")
# 绘制箱线图
plt.boxplot([contents[contents['A'] == i]['X'] for i in range(1, 8)], labels=[f'Group {i}' for i in range(1, 8)])
plt.title('Boxplot of X by Group A')
plt.xlabel('Group A')
plt.ylabel('X')
plt.show()
print("20")
9.4
点击查看代码
import numpy as np
import statsmodels.api as sm
y = np.loadtxt('ti9_4.txt').flatten() #把表中的逗号替换为空格
x1=np.tile(np.arange(1,4),(12,1)).T.flatten()
x2=np.tile(np.hstack([np.ones(3),2*np.ones(3),3*np.ones(3),4*np.ones(3)]),(3,1)).flatten()
d = {'x1':x1,'x2':x2,'y':y}
md=sm.formula.ols('y~C(x1) * C(x2)',d).fit()
ano=sm.stats.anova_lm(md)
print(ano);
print('总偏差平方和:',sum(ano.sum_sq))
print("20")
9.5
点击查看代码
import pandas as pd
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
import openpyxl
column_names = ['城市1', '城市2', '城市3', '城市4']
factor_combinations = [
f"位置{i+1}广告形式{j+1}高档" for i in range(3) for j in range(2)
] + [
f"位置{i+1}广告形式{j+1}低档" for i in range(3) for j in range(2)
]
df_raw = pd.read_excel('9.5.xlsx', header=None, names=column_names, index_col=0)
df_raw.index = factor_combinations
location_factor = [int(x.split('位置')[1].split('广告')[0]) for x in df_raw.index]
ad_factor = [int(x.split('广告形式')[1].split('高档')[0]) if '高档' in x else int(x.split('广告形式')[1].split('低档')[0]) for x in df_raw.index]
decoration_factor = ['高档' if '高档' in x else '低档' for x in df_raw.index]
data = {
'销售量': df_raw.values.flatten(),
'位置': location_factor * 4,
'广告形式': ad_factor * 4,
'装横档次': decoration_factor * 4
}
df = pd.DataFrame(data)
city_factor = [f'城市{i+1}' for i in range(4) for _ in range(3 * 2)]
df['城市'] = city_factor
model = ols('销售量 ~ C(位置) + C(广告形式) + C(装横档次) + C(城市)', data=df).fit()
anova_table = anova_lm(model, typ=2)
print(anova_table)
significant_factors = anova_table[anova_table['PR(>F)'] < 0.05]['source']
print("在显著水平0.05下,以下因素对销售量有显著差异:")
print(significant_factors)
print("20")