Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessons
Probabilities and neighbours · ML-C07 · 18–25 MIN

Threshold choices depend on costs

Choose thresholds using training-only evidence and explicit requirements.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeCompute and inspect the evidence
  3. PractiseCompute and inspect the evidence
  4. TransferCompute and inspect the evidence

Understand the idea

Threshold changes trade false positives against false negatives. The default need not match a particular use case. Choose with training-only predictions, and assess uncertainty and consequences.

Choose thresholds using training-only evidence and explicit requirements.Probability for class BRow 1Row 2Row 3A threshold turns a probability into a class decision.
Choose thresholds using training-only evidence and explicit requirements.Scroll the diagram horizontally if needed.

What the syntax does

method='predict_proba'
  • Requests held-out probability vectors rather than class labels from cross_val_predict.
  • method: "predict", "predict_proba", "decision_function"
cv=folds
  • Keeps threshold investigation inside the training population.
  • cv: 5, folds, list_of_index_pairs

Supplied for this practice

These objects are prepared afresh before your editor runs.

X
Feature dataframe for the declared population, preserving row indices.
X = df[['length', 'width']]
y
Target series, aligned with X.
y = df.label
X_train / X_test / y_train / y_test
Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with X_train. Final-test targets, aligned with X_test.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
model
Already fitted on X_train, y_train. Fitted estimator or pipeline for the requested model family. Fitted on X_train, y_train; reuse without refitting.

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
CLASS180 · first 8 rows
lengthwidthlabel
236.033-0.805568A
581.2970.728558A
-1511.27-1.00866A
99.0248-0.24496A
-13.0141-0.660765A
681.1790.602475A
51.14720.873157A
362.131-0.665605A
Your task
  1. Compute OOF probabilities for the supplied class model.
answer
Hints

Think: Threshold development needs probabilities for rows not used in their own fit.

Tools: cross_val_predict, StratifiedKFold and method='predict_proba'.

Approach: Generate class-probability vectors for every training row using the specified stratified folds.

Explained solution
from sklearn.model_selection import cross_val_predict,StratifiedKFold
answer=cross_val_predict(model,X_train,y_train,cv=StratifiedKFold(5,shuffle=True,random_state=42),method='predict_proba')

OOF probabilities permit training-only threshold analysis without opening the reserved final population.

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.