【问题标题】:Finding Root Mean Squared Error with Pandas dataframe使用 Pandas 数据框查找均方根误差
【发布时间】:2016-02-01 05:38:19
【问题描述】:

我正在尝试从熊猫数据框中计算均方根误差。我已经查看了以前关于堆栈溢出的链接,例如 Root mean square error in python 和 scikit 学习文档 http://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html 我希望有人能对我做错的事情有所了解。 这是dataset。这是我的代码。

import pandas as pd
import numpy as np
sales = pd.read_csv("home_data.csv")

from sklearn.cross_validation import train_test_split
train_data,test_data = train_test_split(sales,train_size=0.8)

from sklearn.linear_model import LinearRegression
X = train_data[['sqft_living']]
y=train_data.price
#build the linear regression object
lm=LinearRegression()
# Train the model using the training sets
lm.fit(X,y)
#print the y intercept
print(lm.intercept_)
#print the coefficents
print(lm.coef_)

lm.predict(300)



from math import sqrt
from sklearn.metrics import mean_squared_error
y_true=train_data.price.loc[0:5,]
test_data=test_data[['price']].reset_index()
y_pred=test_data.price.loc[0:5,]
predicted =y_pred.as_matrix()
actual= y_true.as_matrix()
mean_squared_error(actual, predicted)

编辑

所以这对我有用。我必须将 sqft 的测试数据集值从行转换到列。

from sklearn.linear_model import LinearRegression
X = train_data[['sqft_living']]
y=train_data.price
#build the linear regression object
lm=LinearRegression()
# Train the model using the training sets
lm.fit(X,y)

新代码

test_X = test_data.sqft_living.values
print(test_X)
print(np.shape(test_X))
print(len(test_X))
test_X = np.reshape(test_X, [4323, 1])
print(test_X)
from sklearn.metrics import mean_squared_error
from sklearn.metrics import explained_variance_score
MSE = mean_squared_error(y_true = test_data.price.values, y_pred = lm.predict(test_X))
MSE
MSE**(0.5)

【问题讨论】:

  • 1. train_data 或 test_data 不再是 pandas 数据帧,它们是 numpy.mdarray 类型。
  • 您的代码没有预测任何内容:您只是将数据分成两部分,然后比较标签。因为这些部分的大小不同,mean_squared_error 无法比较它们。你能描述一下你期望这段代码做什么吗?
  • @jakevdp 我稍微编辑了我的代码。所以我根据训练数据创建了一个线性回归模型。我想看看测试数据与预测训练数据的接近程度。

标签: python pandas


【解决方案1】:

您将测试集标签与训练集标签进行比较。我相信您真正想要做的是将测试集标签与 预测 测试集标签进行比较。

例如:

import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import LinearRegression
from sklearn.cross_validation import train_test_split

sales = pd.read_csv("home_data.csv")
train_data, test_data = train_test_split(sales,train_size=0.8)

# Train the model
X = train_data[['sqft_living']]
y = train_data.price
lm = LinearRegression()
lm.fit(X, y)

# Predict on the test data
X_test = test_data[['sqft_living']]
y_test = test_data.price
y_pred = lm.predict(X_test)

# Compute the root-mean-square
rms = np.sqrt(mean_squared_error(y_test, y_pred))
print(rms)
# 260435.511036

请注意,scikit-learn 通常可以处理 Pandas DataFrames 和 Series 输入,而无需显式转换为 numpy 数组。您问题中代码 sn-p 中的错误与传递给 mean_squared_error() 的两个数组大小不同有关。

【讨论】:

  • 谢谢!我对您发布的代码进行了微调。我不得不使用 np.reshape 转换 X_test。你也知道在 pandas 中使用双括号的意义吗?我知道你用它们来选择多行。
  • df[['col']] 将返回一个 DataFrame。 df['col'] 将返回一个系列。
  • 感谢您的帮助。谢谢
猜你喜欢
  • 2018-04-25
  • 2020-03-29
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
  • 2016-06-04
相关资源
最近更新 更多