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

q8l4jmvw  于 2023-04-04  发布在  Python
关注(0)|答案(1)|浏览(161)

我有一个很大的数据点集,我想用系数来转换。我不想对每个元素应用线性回归预测y_pred,我想提取系数并使用它们,比如矩阵 *coef1 +矩阵**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 不匹配。

如何获取适合手工计算的系数?

jaql4c8m

jaql4c8m1#

线性回归具有截距:

model.named_steps['linearregression'].intercept_

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

相关问题