I'm currently building my first model with sklearn to predict whether a customer will renew a subscription. I'm using a random forest because I've heard that they are robust to overfitting. The confusion matrix generated on the results of my test set (25%) seem to imply that the model is working very well, but the tree I'm generating is massive. The size of the tree alone, especially considering I'm only using 12 variables, makes it seem like the model is creating too many complex relationships and thus must be overfitting. If the model is overfitting how can I adjust my code to fix this? Here is the code I used to tune hyper parameters and fit the model.
pipe = Pipeline([('classifier', RandomForestClassifier())])
param_grid = [
{'classifier' : [RandomForestClassifier()],
'classifier__n_estimators' : list(range(200,500,10)),
'classifier__max_features' : list(range(6,32,5))
}
]
clf = GridSearchCV(pipe, param_grid = param_grid, cv = 5, verbose=True, n_jobs=-1)
predictions = clf.best_estimator_.predict(x_test)
cm = metrics.confusion_matrix(y_test, predictions)
plt.figure(figsize=(9,9))
sns.heatmap(cm, annot=True, fmt=".3f", linewidths=.5, square = True, cmap = 'Blues_r');
plt.ylabel('Actual Values')
plt.xlabel('Predicted Values')
plt.title('Renewal Model Confusion Matrix')
Here is the confusion matrix generated, as well as an image of the tree I'm generating and the code used to generate it.
from sklearn.tree import export_graphviz
import graphviz
import os
os.environ["PATH"] += os.pathsep + 'C:/Users/rich/Anaconda3/envs/32bitpy/Library/bin/graphviz/'
d = export_graphviz(clf.best_estimator_[0][1], out_file='rf.dot',
rounded = True, proportion = False,
precision = 2, filled = True)
os.environ["PATH"] += os.pathsep + 'C:/GraphViz/bin'
graph = graphviz.Source(d)
graph