1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2)
t_num = list(range(0,200,10))
t_num = [i+10 for i in t_num]
t_num = list([1]+t_num)
for t in t_num:
trees.append(t)
rfClassifier = RandomForestClassifier(max_depth=3,max_leaf_nodes=8,criterion="gini",n_estimators=t)
rfClassifier.fit(x_train,y_train)
y_test_predict = rfClassifier.predict(x_test)
acc = accuracy_score(y_true=y_test,y_pred=y_test_predict)
accs.append(acc)
err = 1-acc
errs.append(err)
df = pd.DataFrame({
"trees":trees,
"errs":errs,
"accs":accs
})
df.plot(x="trees",y="errs",figsize=(10,5))
|