Mathematical & Statistical Foundations for AI/ML Project
Compute descriptive statistics, execute IQR outlier detection algorithms, perform hypothesis testing, and implement a custom gradient descent optimizer loop on a retail dataset inside a VM.
Domain / Environment
Retail Analytics / Conda VM
Difficulty
Intermediate (2/5)
Course Module
Math & Stats for AI/ML
Deliverables
Jupyter Notebook / Analysis Report
1. System Architecture & Process Workflow
The diagram below displays the data science math and statistics pipeline. Input records are cleaned of statistical outliers using Interquartile Range filters. The cleaned datasets are passed to SciPy for hypothesis t-tests while a custom NumPy loop runs a gradient descent optimizer to minimize loss metrics.
2. Part 1: Step-by-Step Action Items & Key Execution Steps
STEP 1
Activate Python Virtual Environment
Point the terminal execution environment to the course conda sandbox environment.
$ conda activate ds_ai_ml
This action maps terminal interpreter paths to the course packages, making library packages importable.
STEP 2
Create Project Folders inside Linux VM
Create a dedicated folder for the project files inside your guest VM home folder directory.
$ mkdir -p ~/Projects/math_foundations && cd ~/Projects/math_foundations
This creates the project workspace folder and shifts the active directory context of the terminal into it.
STEP 3
Generate Mock Transaction Dataset
Create a CSV text database containing transactional logs, customer age records, and group tags.
$ nano transactions.csv
This opens nano editor. Paste the CSV data template from Part 2, press **Ctrl + O** and **Enter** to save, and **Ctrl + X** to exit.
STEP 4
Install SciPy Math Modules via Pip
Install SciPy scientific packages to enable hypothesis t-test calculations.
$ pip install scipy
This command downloads SciPy, which provides statistical distributions and calculations.
STEP 5
Open VS Code inside Project Folder
Launch Visual Studio Code inside your active project directory.
$ code .
This boots the editor and mounts the project directory in the left file explorer bar automatically.
STEP 6
Create verification script file in VS Code
Use VS Code GUI tools to create a new python file inside the project workspace.
Hover over "MATH_FOUNDATIONS" in VS Code left sidebar -> Click "New File" icon -> Type: verify_math.py -> Press Enter
This instantiates a new empty script document `verify_math.py` inside the active workspace directory.
STEP 7
Load Python Math Diagnostic Logic
Write the statistical functions and gradient loop code inside the empty python file.
Click on Code Editor window tab "verify_math.py" -> Copy code from Part 2 -> Paste it into the editor window -> Press Ctrl + S to save
This populates the file with script code and saves it to local disk directories.
STEP 8
Execute python script via terminal pane
Run the validation script using the python engine to verify statistical operations work.
$ python verify_math.py
This runs the calculations, outputting mean stats, IQR outliers, t-test p-values, gradient loss values, and saving a loss curve plot.
STEP 9
View Saved Graphics Output
Open and check the saved gradient descent loss chart using the default Linux desktop photo viewer.
$ xdg-open loss_minimization.png
This command loads the image viewer application on the VM desktop to show the generated plot chart.
3. Operational Pipeline Architecture
The flowchart below outlines the math analysis pipeline. It details the steps from raw database parsing and IQR outlier cleaning to calculating descriptive stats, running t-tests, executing the custom gradient loop, and saving output reports.
4. Part 2: Complete Deliverable Assets & Production Templates
To run math diagnostics, we need the raw database file and python logic script. Below is a step-by-step code breakdown, followed by the combined template files.
Step-by-Step Code Construction
Lines 1 - 5
Import Framework Modules
Include system packages, Pandas/NumPy, and SciPy stats modules in the code script.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
These imports check system paths and load mathematical computing packages, graphing tools, and statistics modules.
Lines 6 - 15
Compute Descriptive Statistics
Load the CSV dataset and print descriptive statistics like mean, median, and variance.
This runs an independent two-sample t-test, calculating p-values to evaluate if the differences in spending between the two groups are statistically significant.
Lines 33 - 48
Run Gradient Descent Loop
Implement a custom gradient descent loop inside Python to minimize a mean squared error loss function.
X_mat = df_cleaned["Age"].values
y_val = df_cleaned["Spend"].values
w = 0.0 # Initial weight
lr = 0.0001 # Learning rate
losses = []
for epoch in range(1, 101):
prediction = X_mat * w
loss = np.mean((prediction - y_val) ** 2)
gradient = 2 * np.mean(X_mat * (prediction - y_val))
w = w - lr * gradient
losses.append(loss)
This uses a custom loop to calculate predicted values, compute gradients using partial derivatives, update model weights, and track loss reduction.
Production templates
1. Sample Dataset (Save as ~/Projects/math_foundations/transactions.csv):