2017-05-14 142 views
0

我試圖建立一個電動汽車充電事件數據的分類模型。我想預測充電站是否可以在特定的時間點使用。我有下面的代碼工作:sklearn隨機森林準確性分數相同的訓練和測試數據

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 

我的問題是,爲什麼是訓練和測試精度完全一樣?我跑了很多次,總是一模一樣。有任何想法嗎? (我已檢查確保原始數據不同)

+0

有沒有關於'raw_data'大小的信息。你是否期望在'raw_train'和'raw_test'集合中具有完全相同的觀察數量? –

+2

您正在測試和培訓相同的數據。 'X_train == X_test'是'True'。使用scikit-learn的'test_train_split'函數或某種形式的交叉驗證迭代器。 –

回答

1

那麼根本就在你的代碼一個錯字,因爲每次你選擇的所有行:

#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]

相關問題