我花了 30 個小時來調(diào)試這個問題,但它完全沒有意義,希望你們中的一個人可以向我展示不同的觀點。問題是,我在隨機森林中使用訓(xùn)練數(shù)據(jù)幀并獲得了 98%-99% 的良好準確率,但是當我嘗試加載新樣本進行預(yù)測時。該模型總是猜測同一類。# Shuffle the data-frames records. The labels are still attacheddf = df.sample(frac=1).reset_index(drop=True)# Extract the labels and then remove them from the datay = list(df['label'])X = df.drop(['label'], axis='columns')# Split the data into training and testing setsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=TEST_SIZE)# Construct the modelmodel = RandomForestClassifier(n_estimators=N_ESTIMATORS, max_depth=MAX_DEPTH, random_state=RANDOM_STATE,oob_score=True)# Calculate the training accuracyin_sample_accuracy = model.fit(X_train, y_train).score(X_train, y_train)# Calculate the testing accuracytest_accuracy = model.score(X_test, y_test)print()print('In Sample Accuracy: {:.2f}%'.format(model.oob_score_ * 100))print('Test Accuracy: {:.2f}%'.format(test_accuracy * 100))我處理數(shù)據(jù)的方式是相同的,但是當我對 X_test 或 X_train 進行預(yù)測時,我得到了正常的 98%,當我對新數(shù)據(jù)進行預(yù)測時,它總是猜測相同的類。 # The json file is not in the correct format, this function normalizes it normalized_json = json_normalizer(json_file, "", training=False) # Turn the json into a list of dictionaries which contain the features features_dict = create_dict(normalized_json, label=None) # Convert the dictionaries into pandas dataframes df = pd.DataFrame.from_records(features_dict) print('Total amount of email samples: ', len(df)) print() df = df.fillna(-1) # One hot encodes string values df = one_hot_encode(df, noOverride=True) if 'label' in df.columns: df = df.drop(['label'], axis='columns') print(list(model.predict(df))[:100]) print(list(model.predict(X_train))[:100])上面是我的測試場景,你可以在最后兩行看到我對X_train用于訓(xùn)練模型的數(shù)據(jù)和df樣本外數(shù)據(jù)的預(yù)測,它總是猜測類別 0。一些有用的信息:數(shù)據(jù)集不平衡;0 類大約有 150,000 個樣本,而 1 類大約有 600,000 個樣本有141個功能更改 n_estimators 和 max_depth 并不能解決問題任何想法都會有幫助,如果您需要更多信息,請讓我知道我的大腦現(xiàn)在很混亂,這就是我能想到的。
1 回答

慕沐林林
TA貢獻2016條經(jīng)驗 獲得超9個贊
已修復(fù),問題是數(shù)據(jù)集不平衡,我也意識到改變深度會給我不同的結(jié)果。
例如,10 棵深度為 3 的樹 -> 似乎工作正常 10 棵深度為 6 的樹 -> 回到猜測同一類
添加回答
舉報
0/150
提交
取消