具有固定系数的特征的多元线性回归
Multiple linear regression with fixed coefficient for a feature
具有两个特征的线性回归可以用以下等式描述:
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 + intercept)
。因此,您可以简单地求解简单线性回归 y(a1) = a1x1 + intercept_new
,其中 intercept_new
已经考虑将 Por
设置为常数值。
具有两个特征的线性回归可以用以下等式描述:
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 + intercept)
。因此,您可以简单地求解简单线性回归 y(a1) = a1x1 + intercept_new
,其中 intercept_new
已经考虑将 Por
设置为常数值。