Machine Learning Basics Explained With a Python Example

Machine learning is a way of building software that learns patterns from examples instead of following rules a programmer wrote. You give an algorithm data with known answers, it adjusts a model until its predictions match those answers as closely as possible, and you then use the model on new data.

Oct 11, 2026 - 07:29
Updated: 
103.6k

Machine Learning in AI & Data · Explainer

Machine Learning Basics Explained

Quick answer: Machine learning is a way of building software that learns patterns from examples instead of following rules a programmer wrote. You give an algorithm data with known answers, it adjusts a model until its predictions match those answers as closely as possible, and you then use the model on new data. The basics come down to five ideas: the type of learning, features and labels, training, overfitting, and honest evaluation on data the model has never seen.

Key takeaways

  • Supervised learning learns from labelled examples; unsupervised learning finds structure without labels.
  • Classification predicts a category; regression predicts a number.
  • A model learns by reducing its error on training data, step by step.
  • Overfitting means memorising the training data instead of learning general patterns.
  • Only performance on unseen data tells you whether a model works.

Table of Contents

What Is Machine Learning?

In ordinary programming, you write the rules. In machine learning, you provide examples and the algorithm finds the rules. A fraud system, for instance, is not told that a large transaction at 3 am from a new device is suspicious; it is shown many past transactions labelled fraud or not fraud, and it learns which combinations of factors tend to go with fraud.

That makes machine learning suited to problems where the rules are too complex to write by hand, change over time, or are not fully known.

What Are the Main Types of Machine Learning?

TypeLearns fromTypical taskExample
SupervisedExamples with correct answersPredict a label or valueSpam detection, house price prediction
UnsupervisedExamples without answersFind groups or unusual itemsCustomer segments, anomaly detection
ReinforcementRewards and penalties from actionsLearn a strategyGame playing, robot control
Self-supervisedThe structure of the data itselfLearn representationsPredicting the next word, behind language models

Most business machine learning is supervised, and that is where beginners should start.

What Is the Difference Between Classification and Regression?

Both are supervised. Classification predicts a category: spam or not spam, which of five product types, whether a loan will default. Regression predicts a number: tomorrow's demand, a house price, delivery time in minutes. The choice changes which algorithms and which evaluation measures you use.

What Are Features and Labels?

Features are the inputs, such as transaction amount, time of day and account age. The label is the answer you want to predict, such as fraud or not. Much of the real work in machine learning is choosing and preparing features: cleaning missing values, encoding categories as numbers, and making sure no feature secretly contains the answer, a mistake called data leakage.

How Does a Model Actually Learn?

A model starts with parameters set to initial values and makes predictions on the training data. A loss function measures how wrong those predictions are. An optimisation method, commonly gradient descent, nudges the parameters in the direction that reduces the loss, and this repeats many times. Training stops when the error stops improving meaningfully. Different algorithms learn differently, but this loop of predict, measure error, adjust is the core of most of them.

What Does It Look Like in Python?

This program uses scikit-learn and a medical dataset that ships with it: measurements from breast tissue samples, labelled malignant or benign. It trains two decision trees, one allowed to grow without limit and one restricted to a depth of 3:

pip install scikit-learn

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.tree import DecisionTreeClassifier

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, stratify=y, random_state=0)

for depth in (None, 3):
    model = DecisionTreeClassifier(max_depth=depth, random_state=0)
    model.fit(X_train, y_train)
    print('max_depth', depth,
          'train', round(model.score(X_train, y_train), 3),
          'test', round(model.score(X_test, y_test), 3))

# five-fold cross-validation gives a steadier estimate than one split
scores = cross_val_score(DecisionTreeClassifier(max_depth=3, random_state=0), X, y, cv=5)
print('cross-validated accuracy', round(scores.mean(), 3), '+/-', round(scores.std(), 3))

Run it and look at the unlimited tree: it scores a perfect 1.0 on the training data and noticeably lower on the test data. It has memorised the training set. The depth-3 tree scores lower on training data but does better on data it has not seen, with a much smaller gap between the two. That gap is overfitting, visible in two lines of output.

What Is Overfitting and Why Does It Matter?

An overfitted model has learned the noise and quirks of its training data instead of the underlying pattern. It looks excellent in development and disappoints in use, which is one of the most common reasons machine learning projects fail.

The signs: training performance far better than test performance. The remedies: simpler models or limits such as maximum depth, more training data, regularisation, fewer or better features, and stopping training before the model starts to memorise. The opposite problem, underfitting, is a model too simple to capture the pattern, which shows as poor performance on both training and test data.

How Are Models Evaluated Honestly?

Keep a test set the model never sees during training or tuning, and report performance on that. Cross-validation goes further: it splits the data into several folds, trains on all but one and tests on the remaining one, rotating through, so every result is from unseen data and you also see how much it varies.

MeasureQuestion it answersUse when
AccuracyWhat share of predictions were right?Classes are roughly balanced
PrecisionOf the items flagged, how many were right?False alarms are costly
RecallOf the real cases, how many were found?Missing a case is costly, as in fraud or disease
F1 scoreBalance of precision and recallBoth matter and classes are uneven
Mean absolute errorHow far off are numeric predictions on average?Regression

Accuracy misleads on uneven data: if 1 percent of transactions are fraud, a model that never flags fraud is 99 percent accurate and useless.

How Is Deep Learning Different?

Deep learning uses neural networks with many layers that learn features from raw data such as images, audio and text, instead of relying on people to design features. It needs more data and computing power, and it dominates in vision, speech and language. For tables of business data, classical methods like the ones above are often as good and much easier to explain. Our comparison of AI and machine learning covers where deep learning fits.

How Should a Beginner Start?

  1. Learn enough Python to load and clean data. Our introduction to Python is a starting point.
  2. Run the program above, then change it: try another algorithm, another dataset, another test size.
  3. Learn the evaluation measures until you can say which one fits a problem and why.
  4. Work through the free Google Machine Learning Crash Course.
  5. Build two or three small projects on real data and write up what did not work.

The mathematics, mainly linear algebra, probability and calculus, becomes useful as you go deeper, but you can learn it alongside practice.

Talk to a WebAsha training advisor about machine learning courses, batches and current fees.

References

Frequently Asked Questions

Software that learns patterns from examples instead of following rules a programmer wrote, and then uses those patterns to make predictions on new data.

Supervised learning from labelled examples, unsupervised learning that finds structure without labels, reinforcement learning from rewards, and self-supervised learning used to train language models.

Classification predicts a category, such as spam or not spam. Regression predicts a number, such as a price or a delivery time.

Features are the inputs a model uses, such as transaction amount and time. The label is the answer it learns to predict, such as fraud or not fraud.

When a model memorises its training data instead of learning general patterns, so it performs very well on training data and worse on new data.

Use simpler models or limits such as maximum depth, more data, regularisation, better features and early stopping, and always check performance on held-out data.

Splitting data into several folds, training on all but one and testing on the remaining fold in rotation, so every score comes from unseen data and you see how much results vary.

On uneven data a model can score high accuracy by always predicting the common class. Precision, recall and F1 give a truer picture in cases such as fraud.

No. You can start with Python and libraries such as scikit-learn, and learn linear algebra, probability and calculus alongside as you go deeper.

scikit-learn for classical machine learning, because it has consistent interfaces, built-in datasets and good documentation, before moving to deep learning libraries.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Priyanka

I am dedicated to staying up-to-date on the most current technology trends and like to craft content that informs and inspires. Whether through detailed analysis, informative guides, or exact opinion articles, I desire to create engaging content for both technology lovers and industry experts.