I have a problem with a random forest algorithm, I'm firstly explaining the situation and then I'll ask questions.
I have a dataset of 10000 raws x 40 columns, 39 of them are features and 1 contains the labels. The 10000 raws represent different people and the label column shows that 7570 people satisfy a certain proprierty "0" and 2430 satisfy a proprerty "1". I have written a random forest algorithm and my goal is to train the model in order to recognize, considering the 39 features I've already mentioned, if a specific person satisfies property "0" or "1". The code is the following:
model = RandomForestClassifier(max_depth=1, n_estimators=250, random_state=0)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
It returns the value
Accuracy = 0.91
Then I computed the precision matrix:
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
The result obtained has been the following:
Confusion matrix:
[[754 0]
[90 156]]
I have also plotted the learning curves writing the following code block:
plt.figure()
plt.plot(train_sizes, train_scores_mean, 'o-', color='r', label='Training score')
plt.plot(train_sizes, test_scores_mean, 'o-', color='g', label='Cross-validation score')
plt.fill_between(train_sizes, train_scores_mean - train_scores_std, train_scores_mean + train_scores_std, alpha=0.1, color='r')
plt.fill_between(train_sizes, test_scores_mean - test_scores_std, test_scores_mean + test_scores_std, alpha=0.1, color='g')
plt.xlabel('Training examples')
plt.ylabel('Score')
plt.title('Learning Curves for Random Forest')
plt.legend(loc='best')
plt.grid(True)
plt.show()
In the end I have considered the features importance writing the following code block:
importances = model.feature_importances_
indices = np.argsort(importances)[::-1]
sorted_titles = [titles[i] for i in indices]
plt.figure()
plt.title("Features relevance")
plt.bar(range(X.shape[1]), importances[indices], align="center")
plt.xticks(range(X.shape[1]), sorted_titles, rotation=90)
plt.xlim([-1, X.shape[1]])
plt.xlabel("Feature")
plt.ylabel("Relevance")
plt.show()
After explaining everything I've done it's time for questions. The first thing I don't understand is why if I consider a value max_deph > 1 the accuracy becomes greater as the value of max_depth increases, the accuracy reaches 100% for values of max_depth greater than 10. Such a high value for the accuracy clearly is telling me something is not working correctly, but I'm unable to understand what it is. Can please someone help me? Is there any overfitting problem? Is there a problem with the dataset? Am I considering too many features? I'm looking for anyone who can help me, even if you cannot, thanks for having read my question! ;)
PS: If something is not clear, please tell me and I'll clarify it
--- UPDATES ---
As suggested in an answer, due to the fact the dataset is unbalanced, I set class_weight = 'balanced' in model definition:
model = RandomForestClassifier(max_depth=1, n_estimators=250, random_state=0, class_weight=balanced)
and now the Confusion matrix is like:
Confusion matrix:
[[1464 44]
[ 40 452]]
Then, I printed the classification_report obtaining:
And finally I printed the ROC curve obtaining the following graph:
What do you think? Meanwhile I'll try other models like XGBoost or a DNN and see what happens.