我是数据科学和随机森林的新手,当然,我一直在尝试在应用随机森林到一个(1239, 29)的数据集后,找出调整后的R平方和RMSE。
import matplotlib.pyplot as plt import pandas as pdimport numpy as npfrom sklearn.metrics import mean_squared_error, mean_squared_log_error, mean_absolute_errorfrom sklearn.model_selection import train_test_splitX = df.loc[:, df.columns != 'PRODUCTMONTHLYREVENUE_LINE']y = df.loc[:,['PRODUCTMONTHLYREVENUE_LINE']].valuesX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 1)
我在应用随机森林之前已经对数据集进行了训练测试划分。
from sklearn.ensemble import RandomForestRegressorfrom sklearn.metrics import roc_auc_scoremodel = RandomForestRegressor(oob_score=True)model.fit(X_train, y_train)y_predict=model.predict(X_test)
现在,当我尝试运行RMSE时,我得到了之前在OLS模型中没有遇到过的错误。
RMSE= np.sqrt(mean_squared_error(y_predict,y))RMSE
得到以下错误ValueError Traceback (most recent call last) in ()—-> 1 RMSE= np.sqrt(mean_squared_error(y_predict,y))2 RMSE
2 frames/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)210 if len(uniques) > 1:211 raise ValueError(“Found input variables with inconsistent numbers of”–> 212 ” samples: %r” % [int(l) for l in lengths])213214
ValueError: Found input variables with inconsistent numbers of samples: [248, 1239]
回答:
你需要使用下面的命令:
RMSE= np.sqrt(mean_squared_error(y_predict,y_test))RMSE
y
变量指的是整个标签数据。你以20%的比例进行了划分,你应该使用那部分测试数据的标签,而不是整个测试数据的标签