Elexa AI

What Is Machine Learning? A Beginner's Guide with Python Code

Machine learning lets computers learn patterns from data instead of following fixed rules. This guide explains how it works in plain language and walks you through your first model in Python.

EAElexa AI Team 25 November 2026 2 min read
What Is Machine Learning? A Beginner's Guide with Python Code
On this page

What is machine learning?

Machine learning is a way of building software that learns from examples instead of following rules written by hand. You show a model many examples, such as house sizes and their prices, and it finds the pattern that connects them. Then it uses that pattern to make predictions about data it has never seen.

A traditional program says: if the email contains "free money", mark it as spam. A machine learning model looks at thousands of real emails and works out for itself which words and patterns usually mean spam.

The three main types of machine learning

Supervised learning

The model learns from labelled examples, where the right answer is already known. Predicting house prices, detecting spam and recognising handwritten digits are all supervised learning.

Unsupervised learning

The data has no labels. The model looks for structure on its own, for example grouping customers with similar buying habits.

Reinforcement learning

The model learns by trial and error, earning rewards for good actions. It is used for games, robotics and recommendation systems.

Type

Has labels?

Example

Supervised

Yes

Predicting house prices

Unsupervised

No

Grouping customers

Reinforcement

Rewards

Teaching an agent to play a game

How a model learns, step by step

  1. Collect data: gather examples relevant to your problem.

  2. Prepare the data: clean it, fix missing values and turn text into numbers.

  3. Split the data: keep some examples aside to test the model fairly.

  4. Train: the model adjusts itself to fit the training examples.

  5. Evaluate: check how well it predicts the examples it has not seen.

  6. Improve: try better features, more data or a different model.

Your first model in Python

This example trains a model to recognise three types of iris flower from four measurements. It uses scikit-learn, the most popular machine learning library for Python.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# 1. Load a small, ready-made dataset
X, y = load_iris(return_X_y=True)

# 2. Keep 20% of the data aside for testing
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# 3. Train the model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# 4. Check how well it predicts unseen flowers
predictions = model.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, predictions):.2%}")

Install the library first with pip install scikit-learn, then run the script. You should see an accuracy above 90%, which means the model correctly identifies most flowers it has never seen before.

Tip: a model that scores well on training data but badly on test data is overfitting. It has memorised the examples instead of learning the pattern. Always judge a model on data it did not train on.

What to learn next

Once you are comfortable with a simple model like this, the next steps are working with real, messy datasets, choosing the right evaluation metric and comparing different algorithms. From there, deep learning and natural language processing build on the same ideas.

Frequently asked questions

Do I need to be good at maths to learn machine learning?+

No. You need basic algebra and an understanding of averages to start. Libraries like scikit-learn handle the heavy maths, and you can learn the theory gradually as you build projects.

Which programming language is best for machine learning?+

Python. It has the largest set of machine learning libraries, including scikit-learn, PyTorch and TensorFlow, and most tutorials and research code are written in it.

How long does it take to learn machine learning?+

With basic Python, you can train your first models within a few weeks of regular practice. Becoming confident with real projects usually takes a few months of building and experimenting.

Build it yourself

Step-by-step playbooks with complete source code for this topic.

Machine Learning with Python: Build Real Projects

Elexa AI Playbook

Machine Learning with Python: Build Real Projects

Learn machine learning with Python by building real projects. Clean data, train and evaluate models with…

View the playbook One-time $99
Python Programming Playbook

Elexa AI Playbook

Python Programming Playbook

Learn Python step by step by building real projects. Beginner-friendly, with full source code for every…

View the playbook One-time $19.99
The Elexa AI newsletter

Get new AI guides in your inbox

Practical AI, Python and machine learning tutorials. No spam, unsubscribe any time.