我通過 RandomForestClassifier 構建了一個隨機森林并繪制了決策樹。引數“值”(紅色箭頭所指)是什么意思?為什么[]中的兩個數字之和不等于“樣本”的數量?我看到了一些其他的例子,[]中兩個數字的總和等于“樣本”的數量。為什么在我的情況下,它沒有?
df = pd.read_csv("Dataset.csv")
df.drop(['Flow ID', 'Inbound'], axis=1, inplace=True)
df.replace([np.inf, -np.inf], np.nan, inplace=True)
df.dropna(inplace = True)
df.Label[df.Label == 'BENIGN'] = 0
df.Label[df.Label == 'DrDoS_LDAP'] = 1
Y = df["Label"].values
Y = Y.astype('int')
X = df.drop(labels = ["Label"], axis=1)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.5)
model = RandomForestClassifier(n_estimators = 20)
model.fit(X_train, Y_train)
Accuracy = model.score(X_test, Y_test)
for i in range(len(model.estimators_)):
fig = plt.figure(figsize=(15,15))
tree.plot_tree(model.estimators_[i], feature_names = df.columns, class_names = ['Benign', 'DDoS'])
plt.savefig('.\\TheForest\\T' str(i))

uj5u.com熱心網友回復:
不錯的收獲。
雖然沒有記錄,但這是由于默認情況下在隨機森林模型中進行引導采樣(請參閱我在
這里的結果與您報告的結果相似:對于除右下角之外的所有其他節點,sum(value)不等于samples,因為它應該是
Well, now that we have disabled bootstrap sampling, everything looks "nice": the sum of value in every node equals samples, and the base node contains indeed the whole dataset (150 samples).
So, the behavior you describe seems to be due to bootstrap sampling indeed, which, while creating samples with replacement (i.e. ending up with duplicate samples for each individual decision tree of the ensemble), these duplicate samples are not reflected in the sample values of the tree nodes, which display the number of unique samples; nevertheless, it is reflected in the node value.
The situation is completely analogous with that of a RF regression model - see own answer in sklearn RandomForestRegressor discrepancy in the displayed tree values.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/453461.html
