What is machine learning?#
In normal programming, you write the rules: if marks >= 40: print("Pass"). In machine learning (ML), you give the computer examples and it learns the rules by itself.
Traditional programming: data + rules → answers
Machine learning: data + answers → rules (a model)
A classic definition (Tom Mitchell): a program learns from experience E with respect to a task T and performance measure P, if its performance on T, measured by P, improves with experience E. For a spam filter: T = classify emails, E = thousands of emails labelled spam/not spam, P = percentage classified correctly.
ML is everywhere: UPI fraud detection, YouTube and Netflix recommendations, Google Maps arrival times, face unlock, crop-disease detection from leaf photos, and the large language models behind ChatGPT and Claude.
सामान्य programming में हम नियम खुद लिखते हैं। Machine Learning में हम computer को बहुत सारे उदाहरण (data + सही जवाब) देते हैं और वह खुद नियम (pattern) सीख लेता है - इसे model कहते हैं। फिर नए data पर वह model अनुमान (prediction) लगाता है।
Types of machine learning#
| Type | Data | Goal | Examples |
|---|---|---|---|
| Supervised | inputs with correct answers (labels) | predict the label for new inputs | spam detection, price prediction, disease diagnosis |
| Unsupervised | inputs without labels | find structure or groups | customer segmentation, topic grouping, anomaly detection |
| Reinforcement | an agent acting in an environment | learn actions that maximise reward | game-playing AI, robotics, ad bidding |
Supervised learning has two main kinds of problems:
- Classification - the answer is a category: spam / not spam, which flower species, pass / fail.
- Regression - the answer is a number: price, temperature, marks.
Vocabulary#
| Term | Meaning | Iris example |
|---|---|---|
| Sample (row, instance) | one example | one flower |
| Feature (X) | an input measurement | petal length |
| Label / target (y) | the answer to predict | species |
| Model | the learned function from X to y | a trained KNN classifier |
| Training | fitting the model to data | model.fit(X, y) |
| Inference / prediction | using the model on new data | model.predict(new_X) |
The machine learning workflow#
- Define the problem - what do we predict, and how will we measure success?
- Collect data - databases, CSV files, sensors, APIs, Kaggle.
- Explore and clean - missing values, wrong types, outliers.
- Prepare features - encode categories, scale numbers, create useful new features.
- Split into training and test sets.
- Train one or more models.
- Evaluate on the test set with the right metric.
- Tune and improve - better features, other algorithms, hyper-parameters.
- Deploy and monitor - put it in an app and watch it on real data.
Your first model#
The iris dataset (built into scikit-learn) has 150 flowers of 3 species with 4 measurements each. Let us look at it first:
Features: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'] Classes : ['setosa', 'versicolor', 'virginica'] Shape of X: (150, 4) First flower: [5.1 3.5 1.4 0.2] -> setosa
The first run downloads scikit-learn into your browser, so it takes a little while. Now train a classifier and test it on flowers it has never seen:
Test samples: 30 Accuracy: 1.0 First 10 predictions: [0 2 1 1 0 1 0 0 2 1] First 10 true labels: [0 2 1 1 0 1 0 0 2 1]
That is the whole scikit-learn pattern - and it is the same for almost every algorithm:
model = SomeAlgorithm(settings) # choose
model.fit(X_train, y_train) # learn
model.predict(X_test) # use
random_state=42 fixes the random shuffle so you get the same split every time - important for reproducible results. stratify=y keeps the same proportion of each class in the train and test sets.
Why not test on the training data?#
A model can simply memorise its training data. Then it looks perfect on that data but fails on new data - this is overfitting. The test set acts as an exam with unseen questions. You will study overfitting properly in the evaluation lesson.
Never let the test set influence training - not even indirectly (for example, by computing scaling statistics on all the data before splitting). That "data leakage" gives scores that are too good to be true.
The Python ML toolkit#
| Library | Used for |
|---|---|
| NumPy | fast arrays and maths |
| pandas | loading and cleaning tabular data (DataFrames) |
| Matplotlib / Seaborn | charts |
| scikit-learn | classic ML algorithms, preprocessing, evaluation |
| TensorFlow / PyTorch | deep learning (large neural networks) |
Summary#
- ML learns rules from examples instead of hand-written rules.
- Supervised (labels: classification or regression), unsupervised (no labels), reinforcement (rewards).
- Workflow: problem → data → clean → features → split → train → evaluate → tune → deploy.
- scikit-learn pattern: create,
fit,predict; always evaluate on a held-out test set.