Machine Learning Introduction Using Python

Learn the fundamentals of Machine Learning using Python, Scikit-learn, Pandas, and NumPy with practical real-time examples such as house-price prediction, customer classification, and sales prediction.

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.

Collect Data
→
Prepare Data
→
Split Data
→
Train Model
→
Predict
→
Evaluate

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.

1. Why Do We Use Machine Learning?

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.

2. Types of Machine Learning
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
3. Supervised Learning

In supervised learning, the training data contains both input features and known target values.

Concept
Features + Target
        ↓
Training Data
        ↓
Machine Learning Model
        ↓
Prediction for New Data

Common supervised-learning tasks are regression and classification.

4. Regression

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.

5. Classification

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.

6. Unsupervised Learning

In unsupervised learning, the data does not contain a predefined target. The algorithm searches for structure or patterns in the data.

Concept
Customer Data
      ↓
Machine Learning Algorithm
      ↓
Groups / Patterns
      ↓
Customer Segments

A common example is customer segmentation using clustering.

7. Installing Scikit-learn

Scikit-learn is one of the most commonly used Python libraries for traditional machine-learning algorithms.

Terminal
pip install scikit-learn pandas numpy matplotlib seaborn
8. Importing Machine Learning Libraries
Python
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
9. Creating a Real-Time House Price Dataset

Suppose a real-estate company wants to estimate house prices from property characteristics.

Python
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)
Real-Time Scenario: Price is measured here in lakhs for simplicity. The model will learn relationships between area, bedrooms, age, and price.
10. Understanding Features and Target

In machine learning, we separate input variables from the value we want to predict.

Python
X = df[
    ["Area", "Bedrooms", "Age"]
]

y = df["Price"]

print("Features:")
print(X)

print("\nTarget:")
print(y)
X: Features used for prediction.
y: Target value the model learns to predict.
11. Splitting Training and Testing Data

We normally keep part of the data aside for testing the model on examples it did not train on.

Python
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))
Typical Idea: Training data is used to learn the model, while testing data is reserved for evaluating how it performs on unseen data.
12. Creating a Linear Regression Model

Linear Regression is a basic supervised-learning algorithm for predicting numerical values.

Python
model = LinearRegression()

model.fit(
    X_train,
    y_train
)
13. Making a Prediction

After training, the model can predict prices for test data.

Python
predictions = model.predict(X_test)

print(predictions)
14. Predicting a New House Price

Once trained, the model can be used with a new property's feature values.

Python
new_house = pd.DataFrame({
    "Area": [1700],
    "Bedrooms": [3],
    "Age": [5]
})

predicted_price = model.predict(
    new_house
)

print(
    "Predicted Price:",
    predicted_price[0],
    "lakhs"
)
Real-Time Use: A property website could use a similar model to provide an estimated price for a newly listed property.
15. Measuring Prediction Error

Mean Absolute Error measures the average absolute difference between actual and predicted values.

Python
mae = mean_absolute_error(
    y_test,
    predictions
)

print("Mean Absolute Error:", mae)
16. Using R² Score

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.

Python
score = r2_score(
    y_test,
    predictions
)

print("R² Score:", score)
Important: A metric should be interpreted together with the dataset, business problem, baseline, and evaluation design.
17. Comparing Actual and Predicted Values
Python
comparison = pd.DataFrame({
    "Actual": y_test.values,
    "Predicted": predictions
})

print(comparison)
18. Visualizing Actual vs Predicted Prices
Python
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()
19. Classification Example

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.

Python
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.

20. Training a Classification Model
Python
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)
21. Predicting a New Customer
Python
new_customer = pd.DataFrame({
    "Age": [32],
    "Visits": [6]
})

prediction = classifier.predict(
    new_customer
)

print(
    "Predicted Purchase:",
    prediction[0]
)
Real-Time Example: An e-commerce system could use customer activity data to estimate whether a customer belongs to a particular purchase category.
22. Training vs Testing Data
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
23. Common Machine Learning Algorithms

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.

24. Real-Time Example: Sales Prediction

A retail company has historical advertising spending and sales data. It wants to investigate whether advertising spending can help predict sales.

Python
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]
)
25. Real-Time Example: Customer Churn

A subscription company wants to identify patterns associated with customer churn.

Python
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.

26. Real-Time Example: Spam Detection

Email systems can classify messages into categories such as spam and not spam using features derived from messages.

Concept
Email Message
      ↓
Text Processing
      ↓
Feature Extraction
      ↓
Classification Model
      ↓
Spam / Not Spam
Important: Real spam detection normally requires a much larger labeled dataset and appropriate text-processing techniques.
27. Real-Time Example: Customer Segmentation

A business can use unsupervised learning to group customers based on characteristics such as annual spending and purchase frequency.

Python
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)
Business Use: The resulting groups can be investigated as different customer segments. The meaning of each segment should be determined from its characteristics rather than from the cluster number itself.
28. Feature Scaling

Some algorithms are sensitive to differences in the scale of input features. Standardization is one common approach.

Python
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

print(X_scaled)
Best Practice: In train/test workflows, fit preprocessing steps on the training data and apply the learned transformation to test or future data to avoid data leakage.
29. Using a Machine Learning Pipeline

Pipelines help organize preprocessing and model training into a reproducible workflow.

Python
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)
30. Machine Learning Metrics
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
31. Overfitting

Overfitting occurs when a model learns the training data too closely and performs poorly on new data.

Concept
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.

32. Underfitting

Underfitting occurs when a model is too limited to capture important patterns in the data.

Goal: A useful model should generalize well to relevant unseen data rather than simply memorizing the training examples.
33. Model Evaluation Workflow
Dataset
→
Preprocess
→
Split
→
Train
→
Predict
→
Evaluate
34. Real-Time Example: Employee Salary Prediction

A company may have historical employee information and want to estimate salary using features such as experience and education-related variables.

Python
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]
)
Real-World Consideration: A production salary model would require carefully selected features, representative historical data, validation, fairness checks, and appropriate governance.
35. Machine Learning Workflow in a Real Project
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
36. Machine Learning vs Traditional Programming
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
37. Important Machine Learning Terms

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.

38. Complete Real-Time E-Commerce Machine Learning Example

The following example combines data preparation, training, prediction, and evaluation for an e-commerce sales problem.

Complete Python Example
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]
)
Workflow: Historical business data → Features → Training → Prediction → Evaluation → Future prediction.
39. Applications of Machine Learning

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.

40. Important Machine Learning Rules
  1. Start with a clearly defined business or technical problem.
  2. Collect data that is relevant to the problem.
  3. Perform exploratory data analysis before training.
  4. Understand features and the target variable.
  5. Clean and prepare the data carefully.
  6. Split data appropriately for evaluation.
  7. Choose an algorithm that matches the problem.
  8. Use suitable evaluation metrics.
  9. Watch for overfitting and underfitting.
  10. Prevent data leakage during preprocessing and evaluation.
  11. Compare against appropriate baselines where useful.
  12. Interpret model results in the context of the original problem.
  13. Test the model on relevant unseen data.
  14. Monitor deployed models because real-world data can change.
  15. Document assumptions, data sources, features, metrics, and limitations.
Key Idea: Machine Learning is not simply about choosing an algorithm. A successful project combines problem definition, quality data, EDA, preprocessing, model training, evaluation, deployment, and monitoring.

Python Machine Learning • Introduction • Real-Time Examples • Data Science Learning