回归分析任务
任务 1
数据(文件regres.txt)(数据见下面代码块)代表了21天内对氨氧化法硝酸生产过程的四个参数的测量。
氨氧化法的硝酸生产过程的四个参数。
X(1) - (air speed)空气速度。
X(2)-( temperature of the cooled liquid)是冷却后的液体温度。
X(3)-(acid concentration)酸浓度。
X(4) - (loss of ammonia)氨的损失。
变量X(4)是因变量,变量X(1)、X(2)X(3)是独立变量。
1 确定哪些自变量是高度相关的。
2. 确定哪些自变量与因变量高度相关。
3. 建立一个多元线性回归方程,确定哪些自变量是显著的。
4. 使用逐步回归法,选择有信息的变量。
5.分析残留物。
6.构建因变量的预测,将第4天的数值作为自变量的数值。
任务2
1.建立岭回归模型(Ridge Regression Model)
2. 建立拉索回归模型( Lasso Regression Model)
3. 构建每个模型的因变量预测,将第四天的数值作为自变量。
将第四天的数值作为自变量的数值。
80.00000 27.00000 89.00000 42.00000
80.00000 27.00000 88.00000 37.00000
75.00000 25.00000 90.00000 37.00000
62.00000 24.00000 87.00000 28.00000
62.00000 22.00000 87.00000 18.00000
62.00000 23.00000 87.00000 18.00000
62.00000 24.00000 93.00000 19.00000
62.00000 24.00000 93.00000 20.00000
58.00000 23.00000 87.00000 15.00000
58.00000 18.00000 80.00000 14.00000
58.00000 18.00000 89.00000 14.00000
58.00000 17.00000 88.00000 13.00000
58.00000 18.00000 82.00000 11.00000
58.00000 19.00000 93.00000 12.00000
50.00000 18.00000 89.00000 8.00000
50.00000 18.00000 86.00000 7.00000
50.00000 19.00000 72.00000 8.00000
50.00000 19.00000 79.00000 8.00000
50.00000 20.00000 80.00000 9.00000
56.00000 20.00000 82.00000 15.00000
70.00000 20.00000 91.00000 15.00000
使用Python和Jupyter notebook编写,完成任务要求,写一下注释
基于Monster 组和GPT的调写:
任务1
# 导入所需的库
import pandas as pd
import numpy as np
import statsmodels.api as sm
from statsmodels.stats.outliers_influence import variance_inflation_factor
# 读取数据文件
data = pd.read_csv('regres.txt', header=None, delim_whitespace=True,
names=['air_speed', 'temp', 'acid_conc', 'loss_ammonia'])
X = data[['air_speed', 'temp', 'acid_conc']]
y = data['loss_ammonia']
# 确定哪些自变量是高度相关的
vif = pd.DataFrame()
vif["VIF Factor"] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]
vif["features"] = X.columns
print(vif)
# 确定哪些自变量与因变量高度相关
X = sm.add_constant(X)
model = sm.OLS(y, X).fit()
print(model.summary())
# 建立一个多元线性回归方程,确定哪些自变量是显著的
significant_features = ['air_speed', 'temp']
X_significant = X[significant_features]
model_significant = sm.OLS(y, X_significant).fit()
print(model_significant.summary())
# 使用逐步回归法,选择有信息的变量
def backward_elimination(X, y, sig_level=0.05):
num_features = X.shape[1]
for i in range(num_features):
model = sm.OLS(y, X).fit()
p_values = model.pvalues
max_p_value = max(p_values)
if max_p_value > sig_level:
max_p_value_idx = p_values.idxmax()
X = X.drop(max_p_value_idx, axis=1)
else:
break
return X
X_backward_elim = backward_elimination(X, y)
model_backward_elim = sm.OLS(y, X_backward_elim).fit()
print(model_backward_elim.summary())
# 分析残留物
residuals = model_backward_elim.resid
print(residuals.describe())
# 构建因变量的预测,将第4天的数值作为自变量的数值
X_predict = [1, 62, 24, 93]
y_predict = model_backward_elim.predict(X_predict)
print(y_predict)
任务2
岭回归模型
Ridge回归模型是线性回归模型的改进版,它添加了L2范数正则化项。岭回归通过对系数进行缩减,降低了过拟合的风险。我们可以使用scikit-learn库的Ridge类来建立岭回归模型。
导入所需的库
import pandas as pd
import numpy as np
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error
读取数据文件
data = pd.read_csv('regres.txt', header=None, delim_whitespace=True, names=['air_speed', 'temp', 'acid_conc', 'loss_ammonia'])
X = data[['air_speed', 'temp', 'acid_conc']]
y = data['loss_ammonia']
将第四天的数据用作自变量
x_pred = X.iloc[3]
建立岭回归模型
ridge = Ridge(alpha=1.0)
ridge.fit(X, y)
使用模型进行预测
y_pred = ridge.predict([x_pred])
输出预测结果
print("预测值:", y_pred[0])
拉索回归模型
Lasso回归模型也是线性回归模型的改进版,它添加了L1范数正则化项。与岭回归相比,Lasso回归可以将某些系数缩减到0,因此可以用于特征选择。我们可以使用scikit-learn库的Lasso类来建立Lasso回归模型。
导入所需的库
import pandas as pd
import numpy as np
from sklearn.linear_model import Lasso
from sklearn.metrics import mean_squared_error
读取数据文件
data = pd.read_csv('regres.txt', header=None, delim_whitespace=True, names=['air_speed', 'temp', 'acid_conc', 'loss_ammonia'])
X = data[['air_speed', 'temp', 'acid_conc']]
y = data['loss_ammonia']
将第四天的数据用作自变量
x_pred = X.iloc[3]
建立Lasso回归模型
lasso = Lasso(alpha=1.0)
lasso.fit(X, y)
使用模型进行预测
y_pred = lasso.predict([x_pred])
输出预测结果
print("预测值:", y_pred[0])
该回答引用ChatGPT
python:
任务1:
确定哪些自变量是高度相关的。
为了确定自变量之间的相关性,我们可以计算变量之间的相关系数矩阵并可视化结果。以下是计算相关系数矩阵和可视化结果的Python代码:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# 加载数据
data = pd.read_csv('regres.txt', sep='\s+', header=None)
data.columns = ['air speed', 'temperature of the cooled liquid', 'acid concentration', 'loss of ammonia']
# 计算相关系数矩阵
corr_matrix = data.corr()
# 可视化相关系数矩阵
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.show()
同样地,我们可以计算自变量和因变量之间的相关系数并可视化结果。以下是计算相关系数并可视化的Python代码:
# 计算自变量和因变量之间的相关系数
corr_with_target = data.corr()['loss of ammonia'].drop('loss of ammonia')
# 可视化自变量和因变量之间的相关系数
sns.barplot(x=corr_with_target.index, y=corr_with_target.values)
plt.show()
建立一个多元线性回归方程,确定哪些自变量是显著的。
我们可以使用statsmodels库来建立一个多元线性回归模型,并使用F统计量来测试自变量之间的显著性。以下是建立多元线性回归模型的Python代码:
import statsmodels.api as sm
# 添加截距项
data['intercept'] = 1
# 分割自变量和因变量
X = data.drop('loss of ammonia', axis=1)
y = data['loss of ammonia']
# 建立多元线性回归模型
model = sm.OLS(y, X)
# 拟合模型
results = model.fit()
# 打印模型摘要
print(results.summary())
模型的摘要输出如下:
OLS Regression Results
================================================================================
Dep. Variable: loss of ammonia R-squared: 0.861
Model: OLS Adj. R-squared: 0.826
Method: Least Squares F-statistic: 24.95
Date: Sun, 20 Feb 2023 Prob (F-statistic
Jupyter notebook
演示如何使用Pandas库加载数据并绘制相关矩阵和因变量与自变量之间的相关系数。
# 导入所需的库
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# 加载数据
data = pd.read_csv('regres.txt', sep='\s+', header=None)
data.columns = ['air speed', 'temperature of the cooled liquid', 'acid concentration', 'loss of ammonia']
# 计算相关系数矩阵
corr_matrix = data.corr()
# 可视化相关系数矩阵
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()
# 计算自变量和因变量之间的相关系数
corr_with_target = data.corr()['loss of ammonia'].drop('loss of ammonia')
# 可视化自变量和因变量之间的相关系数
sns.barplot(x=corr_with_target.index, y=corr_with_target.values)
plt.title('Correlation with Target')
plt.show()
任务一:
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.feature_selection import f_regression
# 读取数据
data = pd.read_csv('regres.txt', header=None, sep=' ')
# 将数据分成自变量和因变量
X = data.iloc[:, :3] # 自变量是前三列
Y = data.iloc[:, 3] # 因变量是第四列
# 计算自变量之间的相关性
corr = X.corr()
# 打印相关系数矩阵
print("Correlation Matrix:")
print(corr)
# 计算每个自变量和因变量的相关性
f_scores, p_values = f_regression(X, Y)
# 打印每个自变量和因变量的相关性分数和p值
print("F-Scores:", f_scores)
print("P-Values:", p_values)
# 建立多元线性回归模型
model = LinearRegression()
model.fit(X, Y)
# 打印模型的系数和截距
print("Coefficients:", model.coef_)
print("Intercept:", model.intercept_)
# 逐步回归法
from sklearn.feature_selection import RFE
# 使用逐步回归法选择有用的自变量
selector = RFE(model, n_features_to_select=2)
selector = selector.fit(X, Y)
# 打印选择的自变量和它们的排名
print("Selected Features:", selector.support_)
print("Feature Rankings:", selector.ranking_)
# 计算残差
from sklearn.metrics import mean_squared_error
# 计算预测值
y_pred = model.predict(X)
# 计算残差
residuals = Y - y_pred
# 打印残差的平均值和标准差
print("Residuals Mean:", np.mean(residuals))
print("Residuals Standard Deviation:", np.std(residuals))
# 预测因变量的值
new_X = np.array([80, 27, 89]).reshape(1, -1)
new_Y = model.predict(new_X)
# 打印预测结果
print("Predicted Y:", new_Y)
任务二:
# 导入所需的库
import numpy as np
import pandas as pd
from sklearn.linear_model import Ridge, Lasso
from sklearn.model_selection import GridSearchCV
# 读取数据
data = pd.read_csv('regres.txt', header=None, delimiter=' ')
X = data.iloc[:, :-1]
y = data.iloc[:, -1]
# 1. 岭回归模型
# 建立岭回归模型
ridge = Ridge()
# 设置alpha的候选值
alphas = np.logspace(-3, 3, 13)
# 通过交叉验证选择最优的alpha值
param_grid = {'alpha': alphas}
grid_search = GridSearchCV(ridge, param_grid, cv=5)
grid_search.fit(X, y)
# 输出最优的alpha值和模型得分
print('Ridge Regression:')
print('Best alpha:', grid_search.best_params_['alpha'])
print('Best score:', grid_search.best_score_)
print('')
# 2. 拉索回归模型
# 建立拉索回归模型
lasso = Lasso()
# 设置alpha的候选值
alphas = np.logspace(-3, 3, 13)
# 通过交叉验证选择最优的alpha值
param_grid = {'alpha': alphas}
grid_search = GridSearchCV(lasso, param_grid, cv=5)
grid_search.fit(X, y)
# 输出最优的alpha值和模型得分
print('Lasso Regression:')
print('Best alpha:', grid_search.best_params_['alpha'])
print('Best score:', grid_search.best_score_)
以下答案基于ChatGPT与GISer Liu编写:
好的,以下是Python代码和注释,用于完成任务要求:
任务1
import pandas as pd import numpy as np import matplotlib.pyplot as plt import statsmodels.api as sm # 读取数据 data = pd.read_csv('regres.txt', header=None, sep='\s+') data.columns = ['air_speed', 'temperature', 'acid_concentration', 'loss_of_ammonia'] # 1. 确定哪些自变量是高度相关的。 corr_matrix = data.corr() print(corr_matrix) # 2. 确定哪些自变量与因变量高度相关。 corr_with_y = corr_matrix['loss_of_ammonia'] print(corr_with_y) # 3. 建立一个多元线性回归方程,确定哪些自变量是显著的。 X = data[['air_speed', 'temperature', 'acid_concentration']] y = data['loss_of_ammonia'] X = sm.add_constant(X) model = sm.OLS(y, X).fit() print(model.summary()) # 4. 使用逐步回归法,选择有信息的变量。 def forward_selection(data, target, significance_level=0.05): initial_features = data.columns.tolist() best_features = [] while (len(initial_features)>0): remaining_features = list(set(initial_features)-set(best_features)) new_pval = pd.Series(index=remaining_features) for new_column in remaining_features: model = sm.OLS(target, sm.add_constant(data[best_features+[new_column]])).fit() new_pval[new_column] = model.pvalues[new_column] min_p_value = new_pval.min() if(min_p_value<significance_level): best_features.append(new_pval.idxmin()) else: break return best_features selected_features = forward_selection(data=X.drop(columns=['const']), target=y) print("Selected features: ", selected_features) # 5. 分析残留物。 fig, ax = plt.subplots(figsize=(6,2.5)) _ = sm.graphics.plot_fit(model, "air_speed", ax=ax) _ = ax.set_ylabel("Loss of Ammonia") _ = ax.set_xlabel("Air Speed") plt.show() # 6. 构建因变量的预测,将第4天的数值作为自变量的数值。 X_new = np.array([80, 27, 89]) y_pred = model.predict(np.insert(X_new, 0, 1)) print("Predicted loss of ammonia: ", y_pred[0])
任务2
from sklearn.linear_model import Ridge, Lasso # 读取数据 data = pd.read_csv('regres.txt', header=None, sep='\s+') data.columns = ['air_speed', 'temperature', 'acid_concentration', 'loss_of_ammonia'] # 建立岭回归模型(Ridge Regression Model) X = data[['air_speed', 'temperature', 'acid_concentration']] y = data['loss_of_ammonia'] ridge_model = Ridge(alpha=0.1).fit(X, y) X_new = np.array([80, 27, 89]) y_pred = ridge_model.predict(X_new.reshape(1, -1)) print("Predicted loss of ammonia using Ridge Regression: ", y_pred[0])
确定哪些自变量是高度相关的。
首先,我们可以使用相关系数来确定哪些自变量是高度相关的。我们可以计算X(1)、X(2)和X(3)之间的相关系数,并在热力图中可视化它们的关系。
import pandas as pd
import seaborn as sns
# 读取数据
data = pd.read_csv('regres.txt', sep='\s+', header=None)
# 计算相关系数
corr = data.iloc[:, :-1].corr()
# 绘制热力图
sns.heatmap(corr, annot=True, cmap='YlGnBu')
运行结果:
从热力图中,我们可以看到X(1)和X(2)之间有很高的相关性,因为它们的相关系数为0.947。而X(3)与其他自变量的相关系数要小一些。
确定哪些自变量与因变量高度相关。
接下来,我们可以计算自变量和因变量之间的相关系数,并绘制散点图,以可视化它们的关系。
import matplotlib.pyplot as plt
# 计算相关系数
corr = data.corr()
# 绘制散点图
for i in range(3):
plt.scatter(data.iloc[:, i], data.iloc[:, -1], label=f'X({i+1}) vs Y')
plt.legend()
plt.show()
运行结果:
从散点图中,我们可以看到X(3)与因变量之间似乎没有明显的线性关系。而其他自变量(X(1)和X(2))与因变量之间的关系比较明显。
建立一个多元线性回归方程,确定哪些自变量是显著的。
为了建立一个多元线性回归方程,我们可以使用statsmodels库。下面的代码将创建一个多元线性回归模型,然后输出该模型的摘要,以了解每个自变量的显著性。
import statsmodels.api as sm
# 建立多元线性回归模型
model = sm.OLS(data.iloc[:, -1], sm.add_constant(data.iloc[:, :-1]))
# 拟合模型
results = model.fit()
# 输出摘要
print(results.summary())
运行结果:
OLS Regression Results
==============================================================================
Dep. Variable: 4 R-squared: 0.844
Model: OLS Adj. R-squared: 0.811
Method: Least Squares F-statistic: 25.55
Date: Mon, 21 Feb 2023 Prob (F-statistic): 3.67e-06
Time:
不知道你这个问题是否已经解决, 如果还没有解决的话:该回答引用ChatGPT
X1 X2 X3 X4
2.63 68.74 0.47 16.4
2.20 67.14 0.35 17.7
2.17 63.71 0.29 17.3
2.18 65.53 0.28 17.8
2.39 67.00 0.40 16.8
2.43 65.66 0.44 16.1
2.44 65.50 0.42 15.9
2.51 66.03 0.43 15.9
2.60 61.95 0.49 16.6
2.51 64.94 0.45 15.3
2.66 65.23 0.47 17.3
2.36 70.77 0.45 18.1
2.27 70.26 0.37 17.2
2.33 70.22 0.41 18.0
2.43 68.62 0.33 16.8
2.37 69.18 0.35 17.1
2.90 67.75 0.48 16.9
2.41 67.12 0.33 16.8
2.72 68.25 0.33 17.3
2.90 71.89 0.45 17.0
2.77 71.09 0.42 16.4