具有两个特征的线性回归可以通过以下方程描述:
y = a1x1 + a2x2 + 截距
拟合多元线性回归将求解系数a1
和a2
。考虑以下代码:
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
这两个功能是Por
和Perm
。我想将 Perm
的回归系数值固定为某个固定值,并仅求解 Por
的系数。我怎样才能在Python中做到这一点?
最佳答案
假设Por
是a2
。一旦将a2
的值设置为固定值A2,那么线性回归将简化为y(a1) = a1x1 + (A2x2 +截距)
。因此,您可以简单地求解简单线性回归y(a1) = a1x1 +拦截_new
,其中intercept_new
已经考虑将Por
设置为一个常数值。
关于python - 特征具有固定系数的多元线性回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58847296/