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.
Topic: Machine Learning in AI & Data · Explainer
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?
- What Are the Main Types of Machine Learning?
- What Is the Difference Between Classification and Regression?
- What Are Features and Labels?
- How Does a Model Actually Learn?
- What Does It Look Like in Python?
- What Is Overfitting and Why Does It Matter?
- How Are Models Evaluated Honestly?
- How Is Deep Learning Different?
- How Should a Beginner Start?
- References
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?
| Type | Learns from | Typical task | Example |
|---|---|---|---|
| Supervised | Examples with correct answers | Predict a label or value | Spam detection, house price prediction |
| Unsupervised | Examples without answers | Find groups or unusual items | Customer segments, anomaly detection |
| Reinforcement | Rewards and penalties from actions | Learn a strategy | Game playing, robot control |
| Self-supervised | The structure of the data itself | Learn representations | Predicting 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.
| Measure | Question it answers | Use when |
|---|---|---|
| Accuracy | What share of predictions were right? | Classes are roughly balanced |
| Precision | Of the items flagged, how many were right? | False alarms are costly |
| Recall | Of the real cases, how many were found? | Missing a case is costly, as in fraud or disease |
| F1 score | Balance of precision and recall | Both matter and classes are uneven |
| Mean absolute error | How 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?
- Learn enough Python to load and clean data. Our introduction to Python is a starting point.
- Run the program above, then change it: try another algorithm, another dataset, another test size.
- Learn the evaluation measures until you can say which one fits a problem and why.
- Work through the free Google Machine Learning Crash Course.
- 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
- scikit-learn documentation
- scikit-learn, Cross-validation
- Google Machine Learning Crash Course
- scikit-learn source repository
Related reading
Frequently Asked Questions
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0