如何在scikit Learn(Python)中优化随机森林模型

时间:2020-07-18 01:49:03

标签: python machine-learning scikit-learn

我有一个使用scikit Learn的随机森林模型,如下所示:

model = RandomForestClassifier(criterion='gini',n_estimators=700,min_samples_split=4,min_samples_leaf=1,max_features='auto',oob_score=True,random_state=1,n_jobs=-1)
model.fit(X_train,y_train)
y_pred_rm=model.predict(X_test)
print('--------------The Accuracy of the model---------------------------')
kfold = KFold(n_splits=10, random_state=22)
result_rm = cross_val_score(model, all_features, Targeted_feature, cv=10, scoring = 'accuracy')
print('The cross validated score for Random Forest Classifier is:',round(result_rm.mean()*100,2))
y_pred = cross_val_predict(model,all_features,Targeted_feature,cv=10)
kfold = KFold(n_splits=5, random_state=22)
result_rm1 = cross_val_score(model, all_features, Targeted_feature, cv=5, scoring='accuracy')
print('The cross validated score (5)for Random Forest Classifier is:',round(result_rm1.mean()*100,2))
sns.heatmap(confusion_matrix(Targeted_feature,y_pred),annot=True,fmt='3.0f',cmap="winter")
plt.title('Confusion_matrix', y=1.05, size=15)

我一直在尝试优化我的模型,但是还没有成功。我在测试数据集上获得的最高准确率是78%。您有什么想法或步骤可以改善模型吗?

2 个答案:

答案 0 :(得分:1)

是否尝试过使用超参数调整,如果没有尝试使用sklearn中的GridSearchCV或RandomizedSearchCV。即使这样,如果您不能提高模型得分,也可以尝试使用XGboost或进行特征工程以找到有用的特征来进行预测。

我希望您已经进行了所有必要的数据预处理,如果没有的话,这非常重要。尝试其他机器学习模型,它们也有可能表现更好。

答案 1 :(得分:0)

似乎您已经尝试了超参数调整。是什么让您认为可以达到高于78%的准确性分数?如果在尝试根据训练集进行预测时计算准确性得分,那么您获得的准确性接近100%吗?如果是这样,那么这就是过度拟合的问题,您可以尝试减少RandomForest中树的数量。

如果您没有获得很高的培训准确性,则可能您所拥有的功能不足以根据数据进行预测,因此您可能会考虑收集更多的功能。此问题称为拟合不足