【发布时间】:2020-03-09 20:26:57
【问题描述】:
具有两个特征的线性回归可以用以下等式描述:
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 中做到这一点?
【问题讨论】:
标签: python regression linear-regression coefficients