【问题标题】:Multiple linear regression with fixed coefficient for a feature具有固定系数的特征的多元线性回归
【发布时间】:2020-03-09 20:26:57
【问题描述】:

具有两个特征的线性回归可以用以下等式描述:

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 中做到这一点?

【问题讨论】:

    标签: python regression linear-regression coefficients


    【解决方案1】:

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

    【讨论】:

    • 对答案的清晰性和卓越性表示赞同。
    猜你喜欢
    • 2018-01-02
    • 2019-04-07
    • 2021-04-16
    • 2021-05-14
    • 2020-05-19
    • 2019-07-18
    • 2020-05-27
    • 2017-10-24
    • 2019-05-22
    相关资源
    最近更新 更多