亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

隨機森林在訓練和測試中獲得 98% 的準確率,但在其他情況下總是預測同一類

隨機森林在訓練和測試中獲得 98% 的準確率,但在其他情況下總是預測同一類

繁星coding 2023-08-22 14:58:28
我花了 30 個小時來調試這個問題,但它完全沒有意義,希望你們中的一個人可以向我展示不同的觀點。問題是,我在隨機森林中使用訓練數據幀并獲得了 98%-99% 的良好準確率,但是當我嘗試加載新樣本進行預測時。該模型總是猜測同一類。#  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))我處理數據的方式是相同的,但是當我對 X_test 或 X_train 進行預測時,我得到了正常的 98%,當我對新數據進行預測時,它總是猜測相同的類。    #  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用于訓練模型的數據和df樣本外數據的預測,它總是猜測類別 0。一些有用的信息:數據集不平衡;0 類大約有 150,000 個樣本,而 1 類大約有 600,000 個樣本有141個功能更改 n_estimators 和 max_depth 并不能解決問題任何想法都會有幫助,如果您需要更多信息,請讓我知道我的大腦現在很混亂,這就是我能想到的。
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

已修復,問題是數據集不平衡,我也意識到改變深度會給我不同的結果。

例如,10 棵深度為 3 的樹 -> 似乎工作正常 10 棵深度為 6 的樹 -> 回到猜測同一類


查看完整回答
反對 回復 2023-08-22
  • 1 回答
  • 0 關注
  • 1758 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號