Lesson 7 of 13 · How Models Learn
Your First Model
Train your first ML models with scikit-learn: linear regression to predict exam scores and k-nearest neighbours to classify fruit, with create, fit and predict.
- 5 min read
- Beginner
- Reviewed
- Use scikit-learn's create, fit, predict pattern
- Train a linear regression model and read what it learned
- Train a k-nearest neighbours classifier
- Format data the way scikit-learn expects
Meet scikit-learn
Every scikit-learn model follows the same three steps:
- Create the model:
model = LinearRegression() - Fit (train) it on features and labels:
model.fit(X, y) - Predict for new examples:
model.predict(new_X)
Learn this pattern once and you can use almost any model in the library. The code on this page runs in your browser; the first run takes a few seconds while scikit-learn loads.
A regression model
Eight students, two features each (hours studied, hours slept), and the score they got:
from sklearn.linear_model import LinearRegression
# Features (X): one row per student, one column per feature [hours studied, hours slept]
X = [[2, 8], [4, 7], [6, 8], [8, 6], [3, 5], [7, 8], [5, 6], [9, 7]]
# Labels (y): the exam score each student got
y = [56, 64, 76, 77, 54, 81, 66, 85]
model = LinearRegression() # 1. choose a model
model.fit(X, y) # 2. train it on examples
print(f"Weight for hours studied: {model.coef_[0]:.2f}")
print(f"Weight for hours slept: {model.coef_[1]:.2f}")
print(f"Starting value: {model.intercept_:.2f}")
new_students = [[5, 8], [5, 5]] # same hours studied, different sleep
for student, score in zip(new_students, model.predict(new_students)): # 3. predict
print(f"Studied {student[0]} h, slept {student[1]} h -> predicted score {score:.1f}")The model learned that each hour of study adds about 4.4 marks and each hour of sleep about 2.9. The two new students studied equally, but the one who slept 8 hours is predicted to score about 8.5 marks more (3 extra hours × 2.85). The model found that from the data: nobody told it.
A classification model
The same pattern, different model, now predicting a category:
from sklearn.neighbors import KNeighborsClassifier # Features: [weight in grams, smoothness from 1 to 10] X = [[150, 7], [170, 8], [160, 9], [120, 3], [130, 2], [115, 4]] y = ["apple", "apple", "apple", "orange", "orange", "orange"] model = KNeighborsClassifier(n_neighbors=3) # vote among the 3 most similar fruits model.fit(X, y) print(model.predict([[155, 8], [125, 3]]))
A 155 g fruit with smoothness 8 sits among the apples; a 125 g fruit with smoothness 3 among the oranges. Swapping KNeighborsClassifier for a different classifier, such as a decision tree, changes one line: the rest stays the same.
Common mistakes
Use the scikit-learn pattern: create, fit, predict.
from sklearn.linear_model import LinearRegression X = [[1], [2], [3], [4]] # years of experience y = [30, 35, 40, 45] # salary in thousands model = LinearRegression() # Train the model on X and y, then predict the salary for 6 years of experience. # Print it with one decimal place, like: 42.0
What does model.fit(X, y) do?
Summary
- scikit-learn models share one pattern: create, fit, predict.
- Linear regression predicts a number as a starting value plus weighted features; its weights show what it learned.
- k-nearest neighbours classifies by a vote among the most similar training examples.
- Pass X as a list of rows, with features always in the same order.
Next: what actually happens inside a model when it makes a prediction.
Finished reading? Tick it off to track your progress.