【问题标题】:Train/fit a Linear Regression in sklearn with only one feature/variable在 sklearn 中训练/拟合线性回归,只有一个特征/变量
【发布时间】:2020-02-11 06:15:55
【问题描述】:

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

它在文档中说

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

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

【问题讨论】:

    标签: scikit-learn regression


    【解决方案1】:
    [[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_)
    

    【讨论】:

    • 我尝试使用 1 个特征,但即使我使用的是 Lasso 回归,预测模型也只是遵循平行于 x 轴的直线。如何解决这个问题?
    猜你喜欢
    • 2020-04-09
    • 2019-10-21
    • 2019-11-10
    • 2018-05-08
    • 2017-05-27
    • 1970-01-01
    • 2019-04-07
    • 2019-02-26
    相关资源
    最近更新 更多