Machine Learning Introduction
When your UPI app blocks a suspicious payment, when your email moves a fake "KYC update" message to spam, or when a shopping app shows you products you might like, a machine learning model is making that decision. Nobody wrote a rule for every case. The model learned from thousands of past examples.
In this lesson you will learn what machine learning is, how it relates to artificial intelligence and deep learning, and you will train your first model with scikit-learn, the most widely used Python library for classic machine learning.
What is machine learning?
In normal programming you write the rules yourself. To decide whether a student passes, you might write if marks >= 33: passed. That works when the rule is simple and known.
Many real problems have no simple rule. Which payment is fraud? Is this mango ripe from its photo? What will the price of a used car be? You could never write all the rules, but you do have examples with the correct answers. Machine learning (ML) is the practice of letting an algorithm find the rules from such examples. The result of that learning is called a model, and the learning step is called training.
| Traditional programming | Machine learning | |
|---|---|---|
| You provide | Data and rules | Data and the correct answers |
| The computer produces | Answers | Rules (a model) |
| Works best when | Rules are clear and fixed | Rules are fuzzy or change over time |
Here is a tiny example. We know the weekly study hours and attendance of ten students, and whether each one passed. Instead of writing a rule, we let a model learn one.
from sklearn.tree import DecisionTreeClassifier, export_text
# Each row is one student: [hours studied per week, attendance %]
X = [[2, 60], [3, 55], [4, 70], [5, 65], [6, 80],
[7, 75], [8, 90], [9, 85], [10, 95], [1, 40]]
# The answers we already know: 1 = passed, 0 = failed
y = [0, 0, 0, 1, 1, 1, 1, 1, 1, 0]
model = DecisionTreeClassifier(random_state=0)
model.fit(X, y) # training: learn a rule from the examples
print(export_text(model, feature_names=["hours", "attendance"]))
print("Predictions:", model.predict([[3, 90], [8, 50]]))|--- hours <= 4.50 | |--- class: 0 |--- hours > 4.50 | |--- class: 1 Predictions: [0 1]
The model found the rule "more than 4.5 hours of study means pass" on its own, then used it for two new students. With ten rows this is a toy, but the same idea scales to millions of rows and hundreds of columns, where no person could spot the pattern by eye.
AI, machine learning and deep learning
These three terms are often mixed up. They fit inside each other like nested boxes:
- Artificial intelligence (AI) is the broad goal of making computers do tasks that seem to need intelligence. It includes hand-written expert systems, game-playing search and route planning, not only learning from data.
- Machine learning is the part of AI where the computer learns from data instead of being given rules.
- Deep learning is the part of machine learning that uses neural networks with many layers. It powers face unlock, speech recognition and image classification.
- Generative AI, such as chatbots and image generators, is built on very large deep learning models that create new text, images or audio.
| Term | Example | Learns from data? |
|---|---|---|
| Rule-based AI | A chess program that searches moves using fixed scoring rules | No |
| Machine learning | A model that predicts loan default from income and history | Yes |
| Deep learning | A network that recognises crop disease from leaf photos | Yes, needs much more data |
| Generative AI | A chatbot that writes an email for you | Yes, trained on huge text collections |
The term "machine learning" was popularised by Arthur Samuel, who wrote a checkers program that improved by playing against itself in the 1950s. Deep learning became mainstream after 2012, when neural networks started winning image recognition contests, and the transformer design of 2017 led to today's chatbots. You will meet all of these later in this tutorial, starting with Types of Machine Learning.
Words you will use in every lesson
Machine learning data is usually a table. Each row is one example, called a sample: one student, one flower, one payment. Each column you use as input is a feature. The column you want to predict is the target or label. By convention, the features are stored in X (capital, because it is a table) and the target in y (small, because it is one column).
scikit-learn ships with a few small practice datasets. The famous iris dataset has measurements of 150 flowers from three species.
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target
print("Shape of X:", X.shape)
print("Features:", iris.feature_names)
print("Classes:", list(iris.target_names))
print("First flower:", X[0], "is a", iris.target_names[y[0]])Shape of X: (150, 4)
Features: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
Classes: [np.str_('setosa'), np.str_('versicolor'), np.str_('virginica')]
First flower: [5.1 3.5 1.4 0.2] is a setosaThe shape (150, 4) means 150 samples and 4 features. The target y holds the numbers 0, 1 and 2, which stand for the three species. np.str_ is how NumPy labels its own string type; you can treat these values as normal strings.
Your first scikit-learn model
A good model must work on data it has never seen, just like a student must solve new exam questions, not only the solved examples from class. So we hide some flowers from the model during training and use them afterwards to test it. This is called a train/test split.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.25, random_state=42, stratify=iris.target)
model = KNeighborsClassifier(n_neighbors=5)
model.fit(X_train, y_train) # 1. learn from the training flowers
accuracy = model.score(X_test, y_test) # 2. check on flowers it never saw
print(f"Trained on {len(X_train)} flowers, tested on {len(X_test)}")
print(f"Accuracy: {accuracy:.2f}")
new_flower = [[5.0, 3.4, 1.5, 0.2]] # 3. predict a brand-new flower
print("Prediction:", iris.target_names[model.predict(new_flower)[0]])Trained on 112 flowers, tested on 38 Accuracy: 0.97 Prediction: setosa
Every scikit-learn model follows this same pattern: create it, call fit() with training data, then call predict() or score(). Once you know it for one model, you know it for all of them. random_state=42 fixes the random shuffle so you get the same split every time you run the code. The model here, k-nearest neighbours, labels a flower by looking at the five most similar training flowers; you will study it properly in k-Nearest Neighbours.
Setting up on your own computer
Every example in this tutorial runs in your browser, so you can start without installing anything. When you want to work on your own computer, install Python and then the libraries with pip:
python -m pip install scikit-learn pandas matplotlib
python -c "import sklearn; print(sklearn.__version__)"You install the package as scikit-learn but import it as sklearn. You can check which versions the browser uses:
import numpy, pandas, sklearn
print("scikit-learn", sklearn.__version__)
print("NumPy", numpy.__version__)
print("pandas", pandas.__version__)scikit-learn 1.8.0 NumPy 2.4.6 pandas 3.0.2
When machine learning is the right tool
Machine learning is powerful, but it is not always the answer. Use it when:
- the rules are too complex to write by hand, such as spotting spam or reading handwriting;
- the patterns change over time, such as fraud tricks or customer tastes;
- you have enough good, labelled examples to learn from.
Avoid it when a clear rule already exists (GST is a fixed percentage, so calculate it), when you have very little data, or when every decision must be fully explained and a simple rule would do. A model also learns the mistakes and unfairness hidden in its data, which you will study in Responsible AI.
Common mistakes
- Running
pip install sklearn. That old placeholder package now fails on purpose. The real package name isscikit-learn. - Testing on the training data. A model that has seen the answers will look perfect. Always keep test data aside, as you will see in Train/Test Split.
- Expecting 100% accuracy. Real data is noisy. A model that is right 90% of the time can be very useful, and a "perfect" score usually means something is wrong.
- Starting with deep learning. For tables of data, classic models like the ones in this course are often faster, cheaper and just as accurate.
Key points
- Machine learning lets an algorithm learn rules (a model) from examples instead of you writing the rules.
- AI contains machine learning, which contains deep learning; generative AI is built on large deep learning models.
- Features (
X) are the inputs, the target (y) is what you predict, and each row is a sample. - Every scikit-learn model is used the same way: create it,
fit()it, thenpredict()orscore(). - Always test a model on data it did not see during training.
Exercise
scikit-learn also ships the wine dataset with load_wine(): 178 wines, 13 chemical measurements each, from three growers. Load it, split it with test_size=0.25, random_state=42 and stratify, train a KNeighborsClassifier with n_neighbors=5 and print the number of features and the test accuracy rounded to two decimals.
Show solution
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
wine = load_wine()
X_train, X_test, y_train, y_test = train_test_split(
wine.data, wine.target, test_size=0.25, random_state=42, stratify=wine.target)
model = KNeighborsClassifier(n_neighbors=5)
model.fit(X_train, y_train)
print("Features:", wine.data.shape[1])
print(f"Accuracy: {model.score(X_test, y_test):.2f}")Features: 13 Accuracy: 0.78
The code is the same as for iris, only the dataset changed. The accuracy is lower because the wine features have very different ranges (one is around 1,000, others are below 1), and nearest-neighbour models are sensitive to that. You will fix this with scaling in Scaling & Encoding Features.
Quick Quiz
1. What does a machine learning algorithm produce from data and known answers?
Training turns examples with answers into a model that can answer new cases.
2. How are AI, machine learning and deep learning related?
Deep learning is one kind of machine learning, and machine learning is one approach to AI.
3. Why do we keep a test set aside?
A model must work on new data, so we measure it on samples hidden during training.
4. Which command installs scikit-learn?
The package is named scikit-learn; you import it as sklearn.
Learn Machine Learning with a mentor
Free lessons get you started. Our Data Science & Machine Learning training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
