代码之家  ›  专栏  ›  技术社区  ›  LOrD_ARaGOrN

为什么线性回归得到的分数与sklearn.metrics得到的r2\u分数不同

  •  0
  • LOrD_ARaGOrN  · 技术社区  · 3 年前

    from sklearn.datasets import california_housing
    data = california_housing.fetch_california_housing()
    data.data.shape
    data.feature_names
    data.target_names
    
    import pandas as pd
    house_data = pd.DataFrame(data.data, columns=data.feature_names)
    house_data.describe()
    house_data['Price'] = data.target
    
    
    X = house_data.iloc[:, 0:8].values
    y = house_data.iloc[:, -1].values
    
    # Splitting the dataset into the Training set and Test set
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33, random_state = 0)
    
    # Fitting Simple Linear Regression to the Training set
    from sklearn.linear_model import LinearRegression
    linear_model = LinearRegression()
    linear_model.fit(X_train, y_train)
    #Check R-square on training data
    
    
    from sklearn.metrics import mean_squared_error, r2_score
    
    y_pred = linear_model.predict(X_test)
    print(linear_model.score(X_test, y_test))
    print(r2_score(y_pred, y_test))
    

    输出

    0.5957643114594776
    0.34460597952465033
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   James Brooke    3 年前

    从文档中: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html

    sklearn.metrics.r2\u分数(y\u true,y\u pred,…)

    你传递的是真的,而你传递的是错的。如果你切换它们,你会得到正确的结果。

    print(linear_model.score(X_test, y_test))
    print(r2_score(y_test, y_pred))
    
    0.5957643114594777
    0.5957643114594777