python - 特征具有固定系数的多元线性回归

标签 python regression linear-regression coefficients

具有两个特征的线性回归可以通过以下方程描述:

y = a1x1 + a2x2 + 截距

拟合多元线性回归将求解系数a1a2。考虑以下代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

file = 'https://aegis4048.github.io/downloads/notebooks/sample_data/unconv_MV_v5.csv'
df = pd.read_csv(file)[['Por', 'Perm', 'Prod']]

features = df[['Por', 'Perm']].values.reshape(-1,2)
target = df['Prod']

ols = linear_model.LinearRegression()
model = ols.fit(features, target)
predicted = model.predict(features)

coef = model.coef_

pd.DataFrame(coef, index=['Por', 'Perm'], columns=['Regression Coef']).round(2)

>>>         Regression Coef
    Por              244.47
    Perm              97.75

这两个功能是PorPerm。我想将 Perm 的回归系数值固定为某个固定值,并仅求解 Por 的系数。我怎样才能在Python中做到这一点?

最佳答案

假设Pora2。一旦将a2的值设置为固定值A2,那么线性回归将简化为y(a1) = a1x1 + (A2x2 +截距)。因此,您可以简单地求解简单线性回归y(a1) = a1x1 +拦截_new,其中intercept_new已经考虑将Por设置为一个常数值。

关于python - 特征具有固定系数的多元线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58847296/

相关文章:

python - 属性错误: module ‘xgboost’ has no attribute ‘XGBRegressor’

r - 我如何制作这样的回归树?

python - 如何在设计 pandas DataFrame 时使索引从 1 开始

一行中的 Python 2d 列表生成器

python - 了解 TensorFlow LSTM 输入形状

python - 如何解决线性回归中的 "Exception: Data must be 1-dimensional"?

python - 使用线性回归估计价格

python - 在 pandas DataFrame 的 2 列散点图上绘制最佳拟合线

python - 使用 Python 从 Azure Key Vault 解码 JsonWebKey

python - 将具有 n*n 个元素的列表拆分为 n 个列表,每个列表中有 n 个元素