【问题标题】:Negative SKlearn linear regression score负 SKlearn 线性回归分数
【发布时间】:2020-12-24 18:27:26
【问题描述】:

我正在尝试使用 sklearn 线性回归构建房价预测模型,但我得到了负分。

请问我做错了什么?

数据集:

this is the dataset

数据集截图:

请看下面的详细信息:

数据框的形状: (23435, 190)

代码:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import ShuffleSplit
from sklearn.model_selection import cross_val_score

    properties_five = pd.read_csv('house_test.csv')
    
    X = properties_five.drop('price', axis='columns')
    y = properties_five['price']
    
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=10)
    
    lr_clf = LinearRegression()
    lr_clf.fit(X_train, y_train)
    print(lr_clf.score(X_train,y_train))
    print(lr_clf.score(X_test,y_test))
    
    cv = ShuffleSplit(n_splits=5, test_size=0.2, random_state=0)
    
    print(cross_val_score(LinearRegression(), X, y, cv=cv))

训练数据得分:0.0025884591059242013

测试数据得分:-1.6566338615525985e+24

【问题讨论】:

  • 请分享您的代码输出
  • 谢谢,我已经用这个更新了我的问题

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


【解决方案1】:

您已使用df 名称读取文件,因此您应该在下一行将properties_five 替换为df。并尝试对数据集进行标准化/规范化,希望有助于减少错误,for example here you can find details

【讨论】:

  • 我还尝试了另一个数据集并获得了积极的分数,我认为问题出在我的数据集上。我已经用数据集的链接更新了我的问题
  • 这意味着线性回归不适合您的数据。该方法试图拟合一条直线,但如果目标变量和自变量之间存在复杂的非线性关系,则需要选择非线性模型。 -ve score 意味着你的模型在那里表现得非常糟糕。如果你看到那里的分数是如何计算的数学方程式,你就会得到这背后的直觉。我会建议使用非线性模型。
【解决方案2】:

请分享您的输出。线性回归也会受到异常值的影响,因此您应该标准化数值变量。

【讨论】:

    【解决方案3】:

    您的代码看起来不错 - 除了行 df = pd.read_csv('house_test.csv') 可能应该是 properties_five = pd.read_csv('house_test.csv') 以匹配下一行。

    当我在 this data set 上运行它时,我得到以下输出:

    0.7307587542204755
    0.465770160153375
    [0.64358885 0.67211318 0.67817097 0.53631898 0.67390831]
    

    也许线性回归在您的数据集上表现不佳,或者您的数据集包含错误。负 R² 分数意味着您最好使用“恒定回归”,即让您的预测始终等于 y 的平均值。

    【讨论】:

    • 我还尝试了另一个数据集并获得了正面分数,我认为问题出在我的数据集上。我已经用数据集的链接更新了我的问题
    猜你喜欢
    • 2018-05-16
    • 2021-08-09
    • 2014-05-05
    • 2021-02-20
    • 2018-08-18
    • 2020-04-20
    • 2015-01-29
    • 1970-01-01
    • 2014-11-29
    相关资源
    最近更新 更多