Every scikit-learn model follows the same few steps — so once you know one, you know them all.
Load data
Features
X(a 2-D table: rows = samples, columns = features) and targety(what to predict).Split
train_test_splitholds back unseen data to measure honestly.Create & fit
model = SomeModel(...)thenmodel.fit(X_train, y_train)learns from the training set.Predict
model.predict(X_test)on data it has never seen.Evaluate
Compare predictions with the truth:
accuracy_score,classification_report,mean_squared_error…
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.98Why 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.
| Metric | For | Meaning |
|---|---|---|
| Accuracy | Classification | Share of correct predictions — misleading when classes are imbalanced |
| Precision | Classification | Of those predicted positive, how many really are |
| Recall | Classification | Of the real positives, how many were found |
| F1-score | Classification | Balance of precision and recall |
| MSE / RMSE, R² | Regression | Average squared error / share of variance explained |