从回归模型中提取系数,手动计算预测



我有一个大的数据点集,我想用系数来转换。而不是对每个元素应用线性回归预测y_pred,我想提取系数并像matrix*coef1 + matrix**coef2等一样使用它们。

x=np.array([[0.25],[0.35],[0.45],[0.55],[0.65],[0.75],[0.85],[0.95]])
y=np.array([[81.2198],[77.882 ],[74.5442],[72.319],[70.6501],[67.8686],[67.3123],[65.6434]])

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.125, random_state=42)
degree = 3
# Create a pipeline with a polynomial feature transformation and linear regression
model = make_pipeline(
PolynomialFeatures(degree=degree, include_bias=False),
LinearRegression()
)
# Fit the model to the training data
model.fit(x_train, y_train)
# Make predictions on the test data
y_pred = model.predict(x_test)
# Calculate the absolute error
abs_error_array = np.array(np.abs((y_test - y_pred)))
# Get the coefficients of the linear regression model
linreg_coef = model.named_steps['linearregression'].coef_
# Print the coefficients
print(linreg_coef)

如果我使用这些系数,结果与模型不匹配。预测为新的输入值。

如何得到适合人工计算的系数?

线性回归有一个截距:

model.named_steps['linearregression'].intercept_

,你需要添加。然后结果将适合(我检查过)。

相关内容

  • 没有找到相关文章

最新更新