What is Machine Learning?
Machine Learning is a branch of Artificial Intelligence where computers learn patterns from data and use those patterns to make predictions, classifications, or decisions without requiring every rule to be explicitly programmed.
Traditional Programming
Rules + Data are provided to a program to produce an output.
Machine Learning
Data + Expected Results are used to learn a model that can produce predictions for new data.
Features
Input variables used by a machine-learning model, such as area, age, income, or number of bedrooms.
Target
The value the model is trying to predict, such as house price or whether a customer will purchase.
Machine Learning is useful when patterns are difficult to describe using fixed programming rules.
Prediction
Predict future values such as sales, demand, prices, or revenue.
Classification
Assign records to categories such as spam/not spam or fraud/not fraud.
Recommendation
Recommend products, movies, courses, or other items based on patterns in data.
| Type | Description | Example |
|---|---|---|
| Supervised Learning | Learn from labeled examples | House price prediction |
| Unsupervised Learning | Find patterns without target labels | Customer segmentation |
| Reinforcement Learning | Learn through actions and rewards | Game-playing systems |
In supervised learning, the training data contains both input features and known target values.
Features + Target
↓
Training Data
↓
Machine Learning Model
↓
Prediction for New Data
Common supervised-learning tasks are regression and classification.
Regression is used when the target is a numerical value.
House Price
Predict the selling price of a house.
Sales
Predict the expected sales amount.
Temperature
Predict a numerical temperature value.
Classification is used when the target represents a category.
Spam Detection
Classify an email as spam or not spam.
Customer Churn
Predict whether a customer may leave a service.
Fraud Detection
Classify transactions based on learned patterns.
In unsupervised learning, the data does not contain a predefined target. The algorithm searches for structure or patterns in the data.
Customer Data
↓
Machine Learning Algorithm
↓
Groups / Patterns
↓
Customer Segments
A common example is customer segmentation using clustering.
Scikit-learn is one of the most commonly used Python libraries for traditional machine-learning algorithms.
pip install scikit-learn pandas numpy matplotlib seaborn
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, r2_score
Suppose a real-estate company wants to estimate house prices from property characteristics.
data = {
"Area": [
800, 1000, 1200, 1500, 1800,
2000, 2200, 2500, 2800, 3000
],
"Bedrooms": [
2, 2, 3, 3, 3,
4, 4, 4, 5, 5
],
"Age": [
15, 10, 8, 12, 7,
5, 4, 6, 3, 2
],
"Price": [
45, 55, 68, 82, 95,
110, 125, 140, 158, 175
]
}
df = pd.DataFrame(data)
print(df)
In machine learning, we separate input variables from the value we want to predict.
X = df[
["Area", "Bedrooms", "Age"]
]
y = df["Price"]
print("Features:")
print(X)
print("\nTarget:")
print(y)
y: Target value the model learns to predict.
We normally keep part of the data aside for testing the model on examples it did not train on.
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
print("Training rows:", len(X_train))
print("Testing rows:", len(X_test))
Linear Regression is a basic supervised-learning algorithm for predicting numerical values.
model = LinearRegression()
model.fit(
X_train,
y_train
)
After training, the model can predict prices for test data.
predictions = model.predict(X_test)
print(predictions)
Once trained, the model can be used with a new property's feature values.
new_house = pd.DataFrame({
"Area": [1700],
"Bedrooms": [3],
"Age": [5]
})
predicted_price = model.predict(
new_house
)
print(
"Predicted Price:",
predicted_price[0],
"lakhs"
)
Mean Absolute Error measures the average absolute difference between actual and predicted values.
mae = mean_absolute_error(
y_test,
predictions
)
print("Mean Absolute Error:", mae)
R² is a common regression metric that describes how much of the variation in the target is explained by the model on the evaluated data.
score = r2_score(
y_test,
predictions
)
print("R² Score:", score)
comparison = pd.DataFrame({
"Actual": y_test.values,
"Predicted": predictions
})
print(comparison)
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 5))
plt.scatter(
y_test,
predictions
)
plt.xlabel("Actual Price")
plt.ylabel("Predicted Price")
plt.title(
"Actual vs Predicted House Prices"
)
plt.show()
Not every machine-learning problem predicts a number. Classification predicts a category.
Example: an online store wants to predict whether a customer is likely to purchase a product.
data = {
"Age": [22, 25, 28, 35, 40, 45, 50, 55],
"Visits": [2, 4, 5, 7, 8, 10, 12, 13],
"Purchased": [
0, 0, 1, 1,
1, 1, 1, 1
]
}
customers = pd.DataFrame(data)
print(customers)
Here, Purchased is the target:
0 means no purchase and
1 means purchase.
from sklearn.linear_model import LogisticRegression
X = customers[
["Age", "Visits"]
]
y = customers["Purchased"]
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.25,
random_state=42
)
classifier = LogisticRegression()
classifier.fit(
X_train,
y_train
)
predictions = classifier.predict(
X_test
)
print(predictions)
new_customer = pd.DataFrame({
"Age": [32],
"Visits": [6]
})
prediction = classifier.predict(
new_customer
)
print(
"Predicted Purchase:",
prediction[0]
)
| Dataset | Purpose |
|---|---|
| Training Data | Used by the algorithm to learn patterns |
| Testing Data | Used to evaluate predictions on unseen examples |
| Validation Data | Often used for model selection and tuning in suitable workflows |
Linear Regression
Commonly used for numerical prediction.
Logistic Regression
Commonly used for classification.
Decision Tree
Uses a sequence of decision rules to make predictions.
Random Forest
Combines predictions from multiple decision trees.
K-Nearest Neighbors
Makes predictions based on nearby training examples.
K-Means
A common clustering algorithm for unsupervised learning.
A retail company has historical advertising spending and sales data. It wants to investigate whether advertising spending can help predict sales.
data = {
"Advertising": [
10, 15, 20, 25, 30,
35, 40, 45, 50, 55
],
"Sales": [
25, 30, 35, 42, 48,
55, 61, 67, 73, 80
]
}
sales_df = pd.DataFrame(data)
X = sales_df[["Advertising"]]
y = sales_df["Sales"]
model = LinearRegression()
model.fit(X, y)
new_advertising = pd.DataFrame({
"Advertising": [60]
})
predicted_sales = model.predict(
new_advertising
)
print(
"Predicted Sales:",
predicted_sales[0]
)
A subscription company wants to identify patterns associated with customer churn.
data = {
"MonthlyUsage": [
5, 8, 12, 15, 20,
25, 30, 35
],
"SupportTickets": [
5, 4, 4, 3, 2,
2, 1, 1
],
"Churn": [
1, 1, 1, 0,
0, 0, 0, 0
]
}
churn_df = pd.DataFrame(data)
print(churn_df)
A classification model could be trained after defining appropriate features, target, preprocessing, and evaluation procedures.
Email systems can classify messages into categories such as spam and not spam using features derived from messages.
Email Message
↓
Text Processing
↓
Feature Extraction
↓
Classification Model
↓
Spam / Not Spam
A business can use unsupervised learning to group customers based on characteristics such as annual spending and purchase frequency.
from sklearn.cluster import KMeans
customers = pd.DataFrame({
"AnnualSpend": [
10000, 12000, 15000,
45000, 48000, 50000,
90000, 95000, 100000
],
"Purchases": [
2, 3, 3,
8, 9, 10,
18, 20, 22
]
})
model = KMeans(
n_clusters=3,
random_state=42,
n_init=10
)
customers["Segment"] = model.fit_predict(
customers[
["AnnualSpend", "Purchases"]
]
)
print(customers)
Some algorithms are sensitive to differences in the scale of input features. Standardization is one common approach.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
print(X_scaled)
Pipelines help organize preprocessing and model training into a reproducible workflow.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
pipeline = Pipeline([
("scaler", StandardScaler()),
("model", LogisticRegression())
])
pipeline.fit(
X_train,
y_train
)
predictions = pipeline.predict(
X_test
)
print(predictions)
| Problem | Common Metric | Purpose |
|---|---|---|
| Regression | MAE | Average absolute prediction error |
| Regression | RMSE | Penalizes larger errors more strongly |
| Regression | R² | Measures explained variation in a regression setting |
| Classification | Accuracy | Fraction of correct predictions |
| Classification | Precision | Useful when false positives have particular importance |
| Classification | Recall | Useful when missing positive cases has particular importance |
| Classification | F1 Score | Combines precision and recall using their harmonic mean |
Overfitting occurs when a model learns the training data too closely and performs poorly on new data.
Training Performance
↓
Very High
New Data Performance
↓
Much Lower
Possible Issue
↓
Overfitting
Techniques such as simpler models, regularization, cross-validation, and suitable feature selection can help address overfitting.
Underfitting occurs when a model is too limited to capture important patterns in the data.
A company may have historical employee information and want to estimate salary using features such as experience and education-related variables.
employees = pd.DataFrame({
"Experience": [
1, 2, 3, 5, 7,
9, 11, 13
],
"Salary": [
30000, 35000, 42000, 55000,
68000, 80000, 95000, 110000
]
})
X = employees[
["Experience"]
]
y = employees["Salary"]
model = LinearRegression()
model.fit(X, y)
new_employee = pd.DataFrame({
"Experience": [6]
})
predicted_salary = model.predict(
new_employee
)
print(
"Predicted Salary:",
predicted_salary[0]
)
| Step | Activity | Example |
|---|---|---|
| 1 | Define Problem | Predict house prices |
| 2 | Collect Data | Historical property records |
| 3 | Explore Data | EDA and visualization |
| 4 | Prepare Data | Missing values, encoding, scaling |
| 5 | Split Data | Training and testing sets |
| 6 | Train Model | Fit a selected algorithm |
| 7 | Evaluate | Use suitable metrics |
| 8 | Deploy | Make predictions in an application |
| 9 | Monitor | Check model performance over time |
| Traditional Programming | Machine Learning |
|---|---|
| Programmer explicitly defines rules | Model learns patterns from examples |
| Rules + Data → Output | Data + Examples → Model → Prediction |
| Works well when rules are explicit | Useful when patterns can be learned from data |
| Logic is directly coded | Model parameters are learned during training |
Feature
An input variable used by a model.
Target
The value the model is intended to predict.
Training
The process of learning model parameters from data.
Prediction
The model's output for new input data.
Model
A learned representation used to make predictions.
Generalization
How well a model performs on relevant unseen data.
The following example combines data preparation, training, prediction, and evaluation for an e-commerce sales problem.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, r2_score
# --------------------------------
# 1. Historical sales data
# --------------------------------
data = {
"Advertising": [
10, 15, 20, 25, 30,
35, 40, 45, 50, 55,
60, 65
],
"WebsiteVisits": [
100, 130, 160, 190,
220, 250, 290, 320,
350, 390, 420, 450
],
"Sales": [
20, 27, 33, 39,
45, 52, 60, 66,
73, 81, 88, 95
]
}
df = pd.DataFrame(data)
# --------------------------------
# 2. Features and target
# --------------------------------
X = df[
["Advertising", "WebsiteVisits"]
]
y = df["Sales"]
# --------------------------------
# 3. Split data
# --------------------------------
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.25,
random_state=42
)
# --------------------------------
# 4. Train model
# --------------------------------
model = LinearRegression()
model.fit(
X_train,
y_train
)
# --------------------------------
# 5. Test predictions
# --------------------------------
predictions = model.predict(
X_test
)
# --------------------------------
# 6. Evaluate
# --------------------------------
mae = mean_absolute_error(
y_test,
predictions
)
r2 = r2_score(
y_test,
predictions
)
print("MAE:", mae)
print("R²:", r2)
# --------------------------------
# 7. Predict future sales
# --------------------------------
future_data = pd.DataFrame({
"Advertising": [70],
"WebsiteVisits": [500]
})
future_prediction = model.predict(
future_data
)
print(
"Predicted Future Sales:",
future_prediction[0]
)
E-Commerce
Recommendations, demand forecasting, customer segmentation, and fraud detection.
Banking
Fraud analysis, risk modeling, and customer behavior analysis.
Healthcare
Pattern recognition, risk estimation, and decision-support applications.
Marketing
Customer segmentation, response prediction, and campaign analysis.
Manufacturing
Predictive maintenance and quality-control analysis.
Transportation
Demand forecasting, routing, and operational analysis.
- Start with a clearly defined business or technical problem.
- Collect data that is relevant to the problem.
- Perform exploratory data analysis before training.
- Understand features and the target variable.
- Clean and prepare the data carefully.
- Split data appropriately for evaluation.
- Choose an algorithm that matches the problem.
- Use suitable evaluation metrics.
- Watch for overfitting and underfitting.
- Prevent data leakage during preprocessing and evaluation.
- Compare against appropriate baselines where useful.
- Interpret model results in the context of the original problem.
- Test the model on relevant unseen data.
- Monitor deployed models because real-world data can change.
- Document assumptions, data sources, features, metrics, and limitations.
Python Machine Learning • Introduction • Real-Time Examples • Data Science Learning