sklearn/predict-and-predict-proba
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| scikit-learn | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
predict computes the same probabilities as predict_proba and returns the class with the largest one, so calling both does the work twice. Call predict_proba once and take the labels from it.
forest = RandomForestClassifier(random_state=0).fit(X_train, y_train)y_pred = forest.predict(X_test)y_score = forest.predict_proba(X_test)[:, 1]proba = forest.predict_proba(X_test)y_pred = forest.classes_[proba.argmax(axis=1)]y_score = proba[:, 1]What the rule reports
Section titled “What the rule reports”The rule reports model.predict(X_test) next to model.predict_proba(X_test) when the file builds
model as a RandomForestClassifier, ExtraTreesClassifier or BaggingClassifier.
How to fix it
Section titled “How to fix it”With --unsafe-fixes, xpd reuses probabilities saved in the immediately preceding statement. Check that the model has one output per row.
The rule covers the classes whose predict takes the argmax of predict_proba. Deriving the
labels from one predict_proba was 1.83 to 2.00 times faster for the three classes at 1,000 and
10,000 rows, with bit equal labels and probabilities. The rule leaves other classes
alone, because they decide the label from a raw score, where a tie at 0.5 can give another label. Neither reader called one
of the 22 held-back reports wrong. Recall was 30 of 30.
Diagnostic message
Section titled “Diagnostic message”$M.predict($X) computes the same probabilities as $M.predict_proba($X) and returns the class with the largest one. Call predict_proba once, keep the whole matrix, and take $M.classes_[proba.argmax(axis=1)] as the labels, which gives the same labels and measured at least 1.7 times faster.See all rules in the scikit-learn pack.