Your First Models
Expert · 18 min read · ▶ live playground · ✦ checkpoint
The vocabulary from the last lesson becomes real when a model has to make a prediction it has not seen before. Here you will train a tiny scikit-learn model with plain Python lists, split features X from labels y, call fit() and predict(), and evaluate the result on test rows.
Start with X and y
scikit-learn is a Python library for classic machine-learning workflows: table-shaped data, train/test splits, models, predictions, and evaluation tools. It is not the deep-learning stack. Tools like PyTorch and TensorFlow live in that world; here, the job is smaller and more concrete.
scikit-learn examples often name the feature table X and the label list y.
X is the feature data: the input columns a model may use. It is usually a list of rows, where each row is a list of numbers. y is the target data: the known answers for those rows. The name y comes from math notation, but you can read it as "the thing we are trying to predict."
The table below uses 1 for True and 0 for False because many models expect numeric features:
records = [
{"learner": "Mina", "practice_minutes": 25, "slept_well": 0, "passed": 0},
{"learner": "Ravi", "practice_minutes": 40, "slept_well": 1, "passed": 0},
{"learner": "Noor", "practice_minutes": 55, "slept_well": 1, "passed": 1},
]
feature_names = ["practice_minutes", "slept_well"]
X = [[record[name] for name in feature_names] for record in records]
y = [record["passed"] for record in records]
print("features X:", X)
print("labels y:", y)
print("first row means:", dict(zip(feature_names, X[0])), "->", y[0])features X: [[25, 0], [40, 1], [55, 1]]labels y: [0, 0, 1]first row means: {'practice_minutes': 25, 'slept_well': 0} -> 0
That first row says: Mina practiced 25 minutes, did not sleep well, and did not pass. The model will not receive "Mina" as a feature. A name identifies the row for us, but it would be a poor signal for future learners.
Choose a small classifier
A classifier predicts a category. In this lesson, the categories are 0 for did not pass and 1 for passed. A regressor predicts a number, such as a score, price, or delivery time. scikit-learn has both kinds of models, and their workflows feel similar: create the model, call fit(), call predict(), then evaluate.
We will use a decision tree classifier, a model that learns a small set of question-like splits. You can imagine it asking questions such as "was practice time at least this many minutes?" and then moving down a branch. Real decision trees can overfit if they grow too deep, so this example limits the tree with max_depth=2.
The two central methods are:
fit(X_train, y_train)trains the model. It sees features and correct labels together.predict(X_test)asks the trained model for labels on feature rows. It should not receive the real answers.
That separation is the point. Training is study time. Prediction is the quiz.
Try it - train, predict, evaluate
The first time you run this playground, the browser downloads scikit-learn once. After that, the package stays loaded for this page, while each Run still starts your own variables fresh.
Run the code once, then change one record or the tree's max_depth and rerun. Keep the test rows separate while you experiment.
train_test_split() shuffled the rows in a repeatable way because random_state=7 fixes the random choice. stratify=y keeps the mix of 0 and 1 labels more balanced between training and test data. With only twelve rows, every row matters.
The accuracy is the share of test predictions that matched the real labels. Here, 67% means two out of three test rows were correct. That is not a triumph. It is a measurement on a tiny test set.
The confusion matrix counts the kinds of classification results. With labels=[0, 1], the first row is actual 0, the second row is actual 1; the first column is predicted 0, the second column is predicted 1. [[1, 0], [1, 1]] means one true 0 was caught, one true 1 was caught, and one true 1 was missed as 0.
Limits matter as much as predictions
This model is a toy. It has two features, twelve rows, and a made-up target. You should not read the tree as a rule about real learners. It is a training exercise for the workflow.
The same caution applies in real projects:
- More features do not automatically mean better features.
- A high training score can mean memorization, not learning.
- A tiny test set can swing wildly from one row.
- A model can be accurate overall while making the most costly kind of mistake too often.
- A prediction is not a cause. If practice minutes predict passing, that does not prove minutes alone caused the result.
scikit-learn is excellent for this classic ML loop: prepare table-shaped features, choose a model, train it, predict, evaluate, and revise. Deep-learning frameworks solve different problems and come with different tradeoffs; you do not need them for this first local workflow.
Next, the course turns from a local model running inside Python to world-facing AI APIs. Lesson 25.3 shows the shape of sending prompts and data to an external AI service while keeping secrets, cost, errors, and safety in view.
Checkpoint
Answer all three to mark this lesson complete