Skip to content
  • The scikit-learn Workflow · Data Science

The scikit-learn Workflow

LearnViaNotes @learnvianotes· 1 min read

Every scikit-learn model follows the same few steps — so once you know one, you know them all.

  1. Load data

    Features X (a 2-D table: rows = samples, columns = features) and target y (what to predict).

  2. Split

    train_test_split holds back unseen data to measure honestly.

  3. Create & fit

    model = SomeModel(...) then model.fit(X_train, y_train) learns from the training set.

  4. Predict

    model.predict(X_test) on data it has never seen.

  5. Evaluate

    Compare predictions with the truth: accuracy_score, classification_report, mean_squared_error…

Python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

X, y = load_iris(return_X_y=True)                        # 150 flowers, 4 measurements, 3 species
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
print(X.shape, X_train.shape, X_test.shape)              # (150, 4) (105, 4) (45, 4)

model = KNeighborsClassifier(n_neighbors=5)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(round(accuracy_score(y_test, y_pred), 2))          # 0.98

Why split the data?

A model scored on the data it trained on can simply memorise it. Testing on held-out data shows how it will do on new, real inputs. random_state fixes the shuffle so results are reproducible.

MetricForMeaning
AccuracyClassificationShare of correct predictions — misleading when classes are imbalanced
PrecisionClassificationOf those predicted positive, how many really are
RecallClassificationOf the real positives, how many were found
F1-scoreClassificationBalance of precision and recall
MSE / RMSE, R²RegressionAverage squared error / share of variance explained

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report