Statistics and Machine Learning Basics
Statistics helps you describe data honestly, and machine learning uses data to make predictions.
Measures of the Centre
What is a "typical" value? There are three common answers:
| Measure | Meaning | pandas |
|---|---|---|
| Mean | The average: add all values, divide by how many | s.mean() |
| Median | The middle value when sorted | s.median() |
| Mode | The most common value | s.mode() |
Here are the daily cups of tea sold at a small dhaba in one week. On Sunday a big group ordered 40 cups:
Example
import pandas as pd
cups = pd.Series([4, 5, 5, 6, 7, 5, 40])
print("Mean:", round(cups.mean(), 2))
print("Median:", cups.median())
print("Mode:", cups.mode().tolist())Mean: 10.29 Median: 5.0 Mode: [5]
The one unusual day (40) pulls the mean up to about 10, although on most days around 5 cups were sold. The median is not affected.
When data has outliers (values far from the rest), like salaries or house prices, the median usually describes a "typical" value better than the mean.
Measures of Spread
Two classes can have the same average but very different results. Spread tells you how far values are from each other:
- Range: largest minus smallest
- Standard deviation (std): roughly, the typical distance of values from the mean. Small std means values are close together.
Example
marks = pd.Series([35, 42, 50, 55, 63, 68, 77, 82])
print("Mean:", marks.mean())
print("Range:", marks.max() - marks.min())
print("Std:", round(marks.std(), 2))Mean: 59.0 Range: 47 Std: 16.53
Correlation
Correlation measures how strongly two numbers move together. It goes from -1 to +1:
| Value | Meaning |
|---|---|
| Close to +1 | When one goes up, the other goes up |
| Close to 0 | No clear straight-line relationship |
| Close to -1 | When one goes up, the other goes down |
Study hours and marks for eight students:
Example
df = pd.DataFrame({
"hours": [1, 2, 3, 4, 5, 6, 7, 8],
"marks": [35, 42, 50, 55, 63, 68, 77, 82],
})
print(round(df["hours"].corr(df["marks"]), 3))0.999
A very strong positive correlation. But remember: correlation is not causation. Ice cream sales and cases of sunburn rise together, but ice cream does not cause sunburn; hot weather causes both.
What is Machine Learning?
In normal programming, you write the rules. In machine learning, you give the computer examples, and it finds the rules itself.
| Type | Learns from | Example |
|---|---|---|
| Supervised: regression | Examples with a number answer | Predict marks, house price, sales |
| Supervised: classification | Examples with a category answer | Spam or not spam, pass or fail |
| Unsupervised: clustering | Examples with no answer | Group customers by shopping habits |
Your First Model: Linear Regression
Linear regression draws the best straight line through the data, then uses it to predict new values. We use scikit-learn (pip install scikit-learn):
Example
from sklearn.linear_model import LinearRegression
X = df[["hours"]] # input features (a table, so double brackets)
y = df["marks"] # the answer we want to predict
model = LinearRegression()
model.fit(X, y)
print("Slope:", round(model.coef_[0], 2))
print("Intercept:", round(model.intercept_, 2))
new_students = pd.DataFrame({"hours": [9, 2.5]})
print(model.predict(new_students).round(2))Slope: 6.74 Intercept: 28.68 [89.32 45.52]
The model learned: marks = 28.68 + 6.74 x hours. Each extra hour of study adds about 6.7 marks. A student who studies 9 hours is predicted to get about 89 marks.
Every scikit-learn model follows the same three steps:
- Create the model:
model = LinearRegression() - Train it:
model.fit(X, y) - Predict:
model.predict(new_data)
Testing a Model Fairly
A model must be tested on data it has not seen during training, just as an exam should not use the exact homework questions. Split the data first:
Example
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42
)
model = LinearRegression().fit(X_train, y_train)
print(r2_score(y_test, model.predict(X_test)))The R² score shows how well predictions match real values: 1.0 is perfect, and 0 means the model is no better than always guessing the average. With real datasets you would use hundreds or thousands of rows, not eight.
Exercise
Exercise
A dataset of monthly salaries has a few extremely high values. Which measure best shows a typical salary?
