【问题标题】:sklearn LinearRegression, why only one coefficient returned by the model?sklearn LinearRegression,为什么模型只返回一个系数?
【发布时间】:2015-06-24 12:44:11
【问题描述】:

我正在一个简单的数据集上尝试 scikit-learn LinearRegression 模型(来自 Andrew NG coursera 课程,我并不重要,看图以供参考)

这是我的脚本

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

dataset = np.loadtxt('../mlclass-ex1-008/mlclass-ex1/ex1data1.txt', delimiter=',')
X = dataset[:, 0]
Y = dataset[:, 1]


plt.figure()
plt.ylabel('Profit in $10,000s')
plt.xlabel('Population of City in 10,000s')
plt.grid()
plt.plot(X, Y, 'rx')

model = LinearRegression()
model.fit(X[:, np.newaxis], Y)

plt.plot(X, model.predict(X[:, np.newaxis]), color='blue', linewidth=3)

print('Coefficients: \n', model.coef_)

plt.show()

我的问题是: 我希望这个线性模型有 2 个系数:截距项和 x 系数,我怎么只得到一个?

【问题讨论】:

    标签: machine-learning scikit-learn linear-regression


    【解决方案1】:

    哎呀

    我没有注意到intercept是模型的一个单独的属性!

    print('Intercept: \n', model.intercept_)
    

    查看文档here

    intercept_ : 数组

    线性模型中的独立项。

    【讨论】:

      猜你喜欢
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      相关资源
      最近更新 更多