训练和测试数据的 sklearn 随机森林准确度得分相同
sklearn Random Forest accuracy score identical for training and test data
我正在尝试为电动汽车充电事件数据构建分类模型。我想预测充电站在给定时间点是否可用。我有以下代码工作:
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
raw_data = pd.read_csv('C:/temp/sample_dataset.csv')
raw_test = pd.read_csv('C:/temp/sample_dataset_test.csv')
print ('raw data shape: ', raw_test.shape)
#choose which columns to dummify
X_vars = ['station_id', 'day_of_week', 'epoch', 'station_city',
'station_county', 'station_zip', 'port_level', 'perc_local_occupancy',
'ports_at_station', 'avg_charge_duration']
y_var = ['target_variable']
categorical_vars = ['station_id','station_city','station_county']
#split X and y in training and test
X_train = raw_data.loc[:,X_vars]
y_train = raw_data.loc[:,y_var]
X_test = raw_test.loc[:,X_vars]
y_test = raw_test.loc[:,y_var]
#make dummy variables
X_train = pd.get_dummies(X_train, columns = categorical_vars )
X_test = pd.get_dummies(X_test, columns=categorical_vars)
print('train size', X_train.shape, '\ntest size', X_test.shape)
# Train uncalibrated random forest classifier on whole train and evaluate on test data
clf = RandomForestClassifier(n_estimators=100, max_depth=2)
clf.fit(X_train, y_train.values.ravel())
print ('RF accuracy: TRAINING', clf.score(X_train,y_train))
print ('RF accuracy: TESTING', clf.score(X_test,y_test))
结果
raw data shape: (1000000, 15)
train size (1000000, 125)
test size (1000000, 125)
RF accuracy: TRAINING 0.831456
RF accuracy: TESTING 0.831456
我的问题是为什么训练和测试准确率完全一样?我已经 运行 很多次了,它总是完全一样。有任何想法吗? (我检查过确保原始数据不同)
是否有可能您在训练文件和测试文件中使用了同一组数据?
如果是相同的数据,那么最好将数据分成训练集并使用 train_test_split 模块进行测试。
http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html
好吧,您的代码中只有一个拼写错误,因为每次您的 select 所有行:
#split X and y in training and test
X_train = raw_data.loc[:,X_vars]
y_train = raw_data.loc[:,y_var]
X_test = raw_test.loc[:,X_vars]
y_test = raw_test.loc[:,y_var]
你应该通过一些索引分别索引它们,例如:X_train = raw_data.loc[:idx,X_vars]
我正在尝试为电动汽车充电事件数据构建分类模型。我想预测充电站在给定时间点是否可用。我有以下代码工作:
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
raw_data = pd.read_csv('C:/temp/sample_dataset.csv')
raw_test = pd.read_csv('C:/temp/sample_dataset_test.csv')
print ('raw data shape: ', raw_test.shape)
#choose which columns to dummify
X_vars = ['station_id', 'day_of_week', 'epoch', 'station_city',
'station_county', 'station_zip', 'port_level', 'perc_local_occupancy',
'ports_at_station', 'avg_charge_duration']
y_var = ['target_variable']
categorical_vars = ['station_id','station_city','station_county']
#split X and y in training and test
X_train = raw_data.loc[:,X_vars]
y_train = raw_data.loc[:,y_var]
X_test = raw_test.loc[:,X_vars]
y_test = raw_test.loc[:,y_var]
#make dummy variables
X_train = pd.get_dummies(X_train, columns = categorical_vars )
X_test = pd.get_dummies(X_test, columns=categorical_vars)
print('train size', X_train.shape, '\ntest size', X_test.shape)
# Train uncalibrated random forest classifier on whole train and evaluate on test data
clf = RandomForestClassifier(n_estimators=100, max_depth=2)
clf.fit(X_train, y_train.values.ravel())
print ('RF accuracy: TRAINING', clf.score(X_train,y_train))
print ('RF accuracy: TESTING', clf.score(X_test,y_test))
结果
raw data shape: (1000000, 15)
train size (1000000, 125)
test size (1000000, 125)
RF accuracy: TRAINING 0.831456
RF accuracy: TESTING 0.831456
我的问题是为什么训练和测试准确率完全一样?我已经 运行 很多次了,它总是完全一样。有任何想法吗? (我检查过确保原始数据不同)
是否有可能您在训练文件和测试文件中使用了同一组数据?
如果是相同的数据,那么最好将数据分成训练集并使用 train_test_split 模块进行测试。
http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html
好吧,您的代码中只有一个拼写错误,因为每次您的 select 所有行:
#split X and y in training and test
X_train = raw_data.loc[:,X_vars]
y_train = raw_data.loc[:,y_var]
X_test = raw_test.loc[:,X_vars]
y_test = raw_test.loc[:,y_var]
你应该通过一些索引分别索引它们,例如:X_train = raw_data.loc[:idx,X_vars]