首页 > 解决方案 > 在 sklearn 中训练/拟合线性回归,只有一个特征/变量

问题描述

所以我理解套索回归,我不明白为什么当它只是一个二维回归时它需要两个输入值来预测另一个值。

它在文档中说

clf.fit([[0,0], [1, 1], [2, 2]], [0, 1, 2])

我不明白。为什么是[0,0]or[1,1]而不仅仅是[0]or [1]

标签: scikit-learnregression

解决方案


[[0,0], [1, 1], [2, 2]]

意味着您有 3 个样本/观察值,每个样本都有 2 个特征/变量(二维)。

实际上,您可以让这 3 个样本只有 1 个特征/变量,并且仍然能够拟合模型。

使用 1 个功能的示例。

from sklearn import datasets
from sklearn import linear_model

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :1]  # we only take the feature
y = iris.target

clf = linear_model.Lasso(alpha=0.1)

clf.fit(X,y)

print(clf.coef_)
print(clf.intercept_)

推荐阅读