Python Scikit-Learn Basics

In the last lesson, Introduction to Machine Learning, you learned the idea: give the computer examples, and it learns the rules. So you get the concept. Now you need the tool that actually does it. Writing the math for a model by hand would take pages of code and deep knowledge. scikit-learn does it for you in a few lines. This lesson introduces the library, shows the one pattern that every model follows, and gets you ready to build a real model next.

πŸ€” Why scikit-learn?

Machine learning involves a lot of heavy math. If you had to write all of it yourself, you would need months of study just to train one simple model. That is a huge wall for anyone starting out.

scikit-learn (written sklearn in code) tears that wall down. It is a free Python library full of ready-made machine learning models. You do not write the math. You pick a model, hand it your data, and it learns. A few reasons it is the standard starting point:

  • It is simple. Every model uses the same two steps, so once you learn one, you know them all.
  • It comes with everything: models, tools to split data, and ways to measure how well a model did.
  • It works hand in hand with NumPy and Pandas, the tools you already know.

So scikit-learn is where almost everyone begins machine learning in Python. Deep learning tools like TensorFlow come much later, and only for special cases.

πŸ“¦ Installing scikit-learn

scikit-learn is third-party, so you install it once with pip. Notice the install name is scikit-learn, but the import name is sklearn. Run this in your terminal:

Terminal window
pip install scikit-learn

Then you import the parts you need. You do not import the whole thing at once; you pull out the specific model or tool you want:

from sklearn.linear_model import LinearRegression
import sklearn
print(sklearn.__version__)

Output

1.5.1

Your version number may differ, and that is fine. The import working is all that matters.

Caution

A common trip-up: you install with pip install scikit-learn, but in code you write import sklearn. The install name and the import name are different. Writing pip install sklearn may even install the wrong thing, so always install scikit-learn.

πŸ”‘ The fit and predict pattern

Here is the most important thing in this whole module. Every model in scikit-learn, no matter how simple or fancy, follows the same three-step pattern. Learn it once and every model feels familiar.

  • Create the model. You make a model object, like LinearRegression().
  • Fit the model. You call .fit(X, y) to teach it from your data. This is the learning step.
  • Predict with the model. You call .predict(new_X) to get answers for new data.

In scikit-learn, the features are called X (capital, because it is a table) and the labels are called y (small). So model.fit(X, y) reads as β€œlearn how X leads to y”.

This is the whole pattern in code, with tiny made-up data:

from sklearn.linear_model import LinearRegression
# X is the features (house sizes), y is the labels (prices)
X = [[50], [70], [90], [110]]
y = [150, 200, 260, 310]
model = LinearRegression() # 1. create
model.fit(X, y) # 2. fit (learn from the data)
prediction = model.predict([[100]]) # 3. predict for a new house
print(prediction)

Output

[284.16666667]

Read it step by step:

  • LinearRegression() creates an empty model that has not learned anything yet.
  • model.fit(X, y) shows it the four houses and their prices, so it learns the size-to-price pattern.
  • model.predict([[100]]) asks β€œwhat about a 100 square meter house?” and the model answers about 284.

That is a real, trained machine learning model in five lines. The same create, fit, predict steps work for every other model in the library.

Note

Notice X is a list of lists, like [[50], [70]], not a flat list. scikit-learn always wants the features as a 2D table: one row per example, one column per feature. Even with a single feature, each example sits in its own little list.

πŸ—ƒοΈ Built-in datasets for practice

To learn machine learning, you need data. scikit-learn ships with small, clean datasets built right in, so you can practice without hunting for files.

This loads a classic dataset of flower measurements:

from sklearn.datasets import load_iris
data = load_iris()
print(data.data.shape) # the features: rows and columns
print(data.target.shape) # the labels
print(data.target_names) # the flower types

Output

(150, 4)
(150,)
['setosa' 'versicolor' 'virginica']

What you are seeing:

  • data.data is the features X: 150 flowers, each with 4 measurements. So its shape is (150, 4).
  • data.target is the labels y: the type of each flower, one per row, so 150 values.
  • data.target_names lists the three flower types the labels stand for.

These built-in datasets are perfect for learning because they are already clean and the right shape. You will use one to train and test a real model in the next lessons.

🌍 What scikit-learn gives you

scikit-learn is a whole toolbox, not just models. Here are the parts you will reach for.

Tool What it is for
Models Ready-made learners like LinearRegression, all using fit and predict
train_test_split Splits your data into a training set and a test set
Metrics Measures how good the predictions are, like accuracy
Datasets Small built-in datasets to practice on

⚠️ Common Mistakes

A few things that trip people up early:

  • Confusing the install name and the import name. Install scikit-learn, but import sklearn. They are not the same word.
  • Passing features as a flat list. scikit-learn wants X as a 2D table, so [[50], [70]], not [50, 70].
# ❌ A flat list of features causes an error
model.fit([50, 70, 90], y)
# βœ… Each example in its own list, so it is a 2D table
model.fit([[50], [70], [90]], y)
  • Calling .predict before .fit. The model has not learned anything yet, so it cannot predict. Always fit first, then predict.

βœ… Best Practices

Habits that make scikit-learn smooth:

  • Remember the create, fit, predict rhythm. It is the same for every model, so lean on it.
  • Keep features in X and labels in y. Sticking to that naming makes your code match every example online.
  • Practice on the built-in datasets first. They are clean, so you can focus on the model instead of fixing data.
  • Import only the model you need, like from sklearn.linear_model import LinearRegression. It keeps your code clear.

🧩 What You’ve Learned

βœ… scikit-learn (sklearn in code) is Python’s main library of ready-made machine learning models.

βœ… You install it with pip install scikit-learn, but you import it as sklearn.

βœ… Every model follows the same pattern: create the model, .fit(X, y) to learn, then .predict(new_X) to get answers.

βœ… Features go in X as a 2D table, and labels go in y.

βœ… scikit-learn includes built-in datasets, a data splitter, and metrics, so you have everything to practice.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    You run pip install sklearn and it behaves oddly. What is the correct install command?

    Why: The install name is scikit-learn even though you import it as sklearn. The two names are different, and you must install scikit-learn.

  2. 2

    What are the three steps every scikit-learn model follows?

    Why: You create the model object, call .fit(X, y) to learn from the data, then call .predict(new_X) to get answers for new inputs.

  3. 3

    In scikit-learn, what do X and y stand for?

    Why: By convention X holds the features as a 2D table and y holds the labels. So model.fit(X, y) means learn how X leads to y.

  4. 4

    Why does scikit-learn want features like [[50], [70]] instead of [50, 70]?

    Why: scikit-learn always expects X as a 2D table. Each example gets its own list, so even a single feature is wrapped, like [[50], [70]].

πŸš€ What’s Next?

You know the library and the create, fit, predict pattern. Now let’s use it for real. Next we load a dataset, split it into training and test sets, and train a complete model from start to finish.

Training a Model

Share & Connect