我正在一个简单的数据集上尝试 scikit-learn LinearRegression 模型(来自 Andrew NG coursera 类(class),我并不重要,请查看图表以供引用)
这是我的脚本
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 系数,为什么我只得到一个?
最佳答案
哎呀
我没有注意到截距是模型的一个单独的属性!
print('Intercept: \n', model.intercept_)
查看文档here
intercept_ : array
Independent term in the linear model.
关于machine-learning - sklearn LinearRegression,为什么模型只返回一个系数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29703028/