Your First Machine-Learning Model
Machine learning is not magic and not just for people with GPUs. At its core it is this: show a program examples, and it learns a rule that generalises to new examples. You already have everything you need — scikit-learn runs in this browser tab.
The three things every model needs
- Features (X) — the inputs you know. Here, hours studied. Always a 2-D structure: a list of rows, each row a list of numbers.
- Labels (y) — the answer you want to predict. Here, the exam score.
- A model — the algorithm that finds the rule. We start with
LinearRegression, which fits a straight liney = m·x + c.
The two verbs
Every scikit-learn model has the same two methods:
model.fit(X, y)— learn from examples.model.predict(X_new)— apply the learned rule to new inputs.
That is the whole API you will use for the rest of this track, whether the model is a line, a tree, or a forest.
Read the model
After fitting, model.coef_ holds the slope (how much score rises per extra hour) and model.intercept_ the starting point. A model you can read is a model you can trust.
Try it
- Add a 6th example
[6] -> 95and re-fit. Does the slope change? - Predict the score for 0 hours. Does the intercept match?
3 graded exercises
Write the code, click Check. We’ll tell you exactly what to fix.
- Exercise 1
Write
train(X, y)that returns a fittedLinearRegressionmodel. - Exercise 2
Write
predict_score(model, hours)that returns the model's predicted score for a singlehoursvalue (a plain number, not a list). - Exercise 3
Write
per_hour(model)that returns how much the predicted score rises per extra hour (the slope).