What machine learning is and how it works
Machine learning is a way to get a model from data: an algorithm finds patterns in examples and predicts answers for new data where no rule has been written in advance. The main Python library for it is scikit-learn.
A model is useful where a rule is hard to state: telling spam from legitimate mail by its text, estimating a flat’s price from dozens of features, grouping customers by behaviour. The task determines what the model predicts and how it is evaluated:
| Task | What it predicts | Example | Typical metric |
|---|---|---|---|
| Classification | a category: spam or not | diagnosis from symptoms | accuracy |
| Regression | a number: a price, a temperature | the price of a flat | mean absolute error |
| Clustering | a group of similar examples | customer segments | silhouette coefficient |
| Dimensionality reduction | new features in place of the old ones | compressing 200 features to 10 | explained variance ratio |
Supervised and unsupervised learning
The difference is whether correct answers are available. In supervised learning, the answers y are known alongside the features X: spam or not spam, the price of a flat. This is how classifiers and regressors are trained. In unsupervised learning there are no answers, and the model looks for structure on its own: groups of similar customers, a compact description of the features, rare deviations. Outlier detection and density estimation belong to the same kind.
When you do not need machine learning
If the rule can be stated explicitly (“if the age is under 18, refuse”), write it as ordinary code: it is transparent, needs no data and cannot go wrong through overfitting. A model is justified when a rule is hard to extract from data. Two more reasons to avoid it: there is little data (a few dozen examples), or the cost of an error is unacceptable and every decision must be explainable.
The unified estimator API
In scikit-learn, every method is an estimator: an object that learns with fit and predicts with predict. The score method returns the quality of a trained estimator: accuracy for classifiers, the coefficient of determination for regressors. Transformers have transform instead of predict and only prepare features. This single interface lets you swap models for one another and assemble a pipeline from steps.
>>> from sklearn.pipeline import make_pipeline>>> from sklearn.preprocessing import StandardScaler>>> from sklearn.linear_model import LogisticRegression>>> from sklearn.datasets import load_iris>>> from sklearn.model_selection import train_test_split>>> from sklearn.metrics import accuracy_score>>> X, y = load_iris(return_X_y=True)>>> X_train, X_test, y_train, y_test = train_test_split(... X, y, random_state=0)>>> pipe = make_pipeline(StandardScaler(), LogisticRegression())>>> pipe.fit(X_train, y_train)>>> accuracy_score(pipe.predict(X_test), y_test)0.97...From data to prediction
Work on a task looks like a pipeline. The model is trained on one part of the data and checked on another that it has not seen; otherwise the score only shows success on memorised examples. Parameters are tuned with cross-validation inside the training part, and the test set is spent once, at the very end.
- Collect the data: a feature matrix X and, for supervised learning, the answers y.
- Split it into training and test parts with
train_test_split. - Train the model on the training part with
fit. - Evaluate on the test part:
predictand a metric from “Evaluation and metrics”. - Tune the parameters with cross-validation; see “Cross-validation and hyperparameter tuning”.
- Train the final model and apply it to new data.
How it differs from neural networks and chatbots
Classical machine learning trains a model on your tabular data and gives a verifiable prediction: a probability, a number, a label. Large language models (LLMs) generate text after training on huge corpora and solve different tasks. For where each one fits, see the LLM and RAG reference. How neural networks and large models are built, trained and fine-tuned is covered in the AI models reference.
What to know beforehand
- Python: functions, classes, working with lists and dictionaries; see the Python reference.
- Tables: DataFrames, reading CSV, selecting columns; see the pandas reference.
- Virtual environments with venv and installing packages; see the Python reference, “Installation and first run”.
Section sources
the section is written from these pages- Getting Startedscikit-learn.org/1.9/getting_started.html(opens in a new tab)
- Glossary of Common Terms and API Elementsscikit-learn.org/1.9/glossary.html(opens in a new tab)
- 3.4. Metrics and scoring: quantifying the quality of predictionsscikit-learn.org/1.9/modules/model_evaluation.html(opens in a new tab)
- 1. Supervised learningscikit-learn.org/1.9/supervised_learning.html(opens in a new tab)
- 2. Unsupervised learningscikit-learn.org/1.9/unsupervised_learning.html(opens in a new tab)