I am using a RandomForestClassifier to classify two outcomes, let's say circles and squares. In my data set, there are many more squares (93%) than circles (7%). The percentages are the same in the test and the train set. In total, there are about 150,000 rows of data and 24 features. For my purposes, it is much more important to find the circles and classify them as circles, even if that means to falesly classify some squares as circles.

I am using the following code:

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report

hyperF = dict(n_estimators = n_estimators, max_depth = max_depth, 
min_samples_split = min_samples_split, min_samples_leaf = min_samples_leaf)

gridF = GridSearchCV(RandomForestClassifier(class_weight='balanced'), hyperF,
verbose = 1,  scoring=['accuracy', 'recall', 'precision'], refit='precision', 
return_train_score=True, n_jobs = 18)

bestF = gridF.fit(X_train, y_train)

y_pred = bestF.predict(x_test)
print(classification_report(y_test, y_pred))

              precision    recall  f1-score   support

      square       0.93      0.99      0.96     30496
      circle       0.21      0.04      0.07      2283

    accuracy                           0.92     32779
   macro avg       0.57      0.51      0.51     32779
weighted avg       0.88      0.92      0.90     32779

As you can see, my model seems to do exactly the opposite of what I want: it learns that squares are more common and therefore tends to classify circles as squares. Is there a way to "tell the Classifier" that I want to focus on detecting the circles?

  • 1
    $\begingroup$ Off topic, but you could study the use of the parameter class_weight. Furthermore, I would advise you to first select a relevant metric to be optimized and then do exactly this. $\endgroup$
    – Michael M
    May 30 at 18:39

For every Machine Learning model you could use gridsearch to optimize your hyper parameters, see more here https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.