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

问题描述 投票:0回答:1

我有一大组数据点,我想使用系数对其进行转换。我不想为每个元素应用线性回归预测

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.predict 不匹配。

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

python prediction
1个回答
2
投票

线性回归有一个截距:

model.named_steps['linearregression'].intercept_

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

© www.soinside.com 2019 - 2024. All rights reserved.