[英]Multiple linear regression with fixed coefficient for a feature
Linear regression with two features can be described by the following equation:具有两个特征的线性回归可以用以下等式来描述:
y = a1x1 + a2x2 + intercept y = a1x1 + a2x2 + 截距
Fitting multiple linear regression will solve for the coefficients a1
, and a2
.拟合多元线性回归将求解系数a1
和a2
。 Consider the following code:考虑以下代码:
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
The two features are Por
and Perm
.这两个功能是Por
和Perm
。 I want to fix the values of the regression coefficient of Perm
to some fixed value, and solve only for the coefficient of Por
.我想将Perm
的回归系数的值固定为某个固定值,并且只求解Por
的系数。 How can I do this in Python?如何在 Python 中做到这一点?
Say Por
is a2
.说Por
是a2
。 Once you set the value of a2
to a fixed value A2, then your linear regression would be reduced to y(a1) = a1x1 + (A2x2 + intercept)
.将a2
的值设置为固定值 A2 后,您的线性回归将减少为y(a1) = a1x1 + (A2x2 + intercept)
。 Therefore, you can simply solve the simple linear regression y(a1) = a1x1 + intercept_new
, where intercept_new
would already take into account setting Por
to a constant value.因此,您可以简单地求解简单的线性回归y(a1) = a1x1 + intercept_new
,其中intercept_new
已经考虑将Por
设置为常数值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.