# 0bada1/ft_linear_regression architecture

> Architecture diagram of the GitHub repository 0bada1/ft_linear_regression, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/0bada1/ft_linear_regression
- Repository: https://github.com/0bada1/ft_linear_regression

## Overview

##### Project Type and Purpose
- This is a command-line machine-learning utility (a single-feature linear regression tool) whose goal is to train a model that predicts a car’s price from its mileage, generate visualizations of cost and parameter convergence, then offer a prediction interface and precision evaluation.

##### High-Level Components
a. Data Layer
- Raw data files (data/data.csv, data/test_data.csv)
- CSV loader (srcs/train_model/load_data.py)
b. Training Pipeline
- Cost computation (cost.py)
- Gradient descent optimizer (gradient_descent.py)
- Model definition and utility functions (linear_regression.py, plot.py, log/)
- Orchestrator script (main.py & train_model.sh)
- Output: trained parameters (model.txt) and plots (PNG or stdout)
c. Prediction Module
- CLI wrapper (predict.py)
- Loads model.txt, accepts mileage input, outputs predicted price
d. Precision Evaluation Module
- calculate_precision.py
- Loads test_data.csv and model.txt, computes prediction accuracy metrics
e. External Libraries & Tools
- Python 3.x, NumPy, Matplotlib (for plotting), shell script for orchestration

##### Relationships & Data Flow
- Data Layer → Training Pipeline: raw CSV → load_data.py → cost & gradient computation → model.txt & plots
- Training Pipeline → Prediction Module: model.txt consumed by predict.py
- Training Pipeline & Data Layer → Precision Module: test_data.csv + model.txt → calculate_precision.py
- Optionally, Training Pipeline logs intermediate theta values in log/ for plotting module

##### Architectural Patterns & Principles
- Pipeline pattern: Clear stages (Load → Train → Evaluate → Predict)
- Separation of concerns: each functionality in its own directory (train_model, predict, precision)
- Single responsibility: cost, gradient, data loading, plotting each isolated into its own module

##### Essential Elements
- Clear, concise component names (e.g., “Training Pipeline”)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Data Layer
    DataCSV["data/data.csv"]:::dataStore
    TestCSV["data/test_data.csv"]:::dataStore

    %% Training Pipeline
    subgraph "Training Pipeline"
        direction TB
        Loader["Load Data\n(srcs/train_model/load_data.py)"]:::compute
        Cost["Cost Computation\n(srcs/train_model/cost.py)"]:::compute
        Grad["Gradient Descent\n(srcs/train_model/gradient_descent.py)"]:::compute
        LinReg["Linear Regression\n(srcs/train_model/linear_regression.py)"]:::compute
        Plot["Plotting\n(srcs/train_model/plot.py)"]:::compute
        Logs["Training Logs\n(srcs/train_model/log)"]:::compute
        Main["Orchestrator\n(main.py & train_model.sh)"]:::compute
    end

    %% Model Artifact
    Model["Model Artifact\n(srcs/train_model/model.txt)"]:::artifact

    %% Prediction & Precision Modules
    Predict["Prediction CLI\n(srcs/predict/predict.py)"]:::cli
    Precision["Precision Eval\n(srcs/precision/calculate_precision.py)"]:::cli

    %% Data Flow
    DataCSV -->|data.csv| Loader
    Loader -->|training data| Cost
    Cost -->|cost values| Grad
    Grad -->|θ updates| Cost
    Grad -->|final θ₀,θ₁| LinReg
    LinReg -->|parameters| Model
    Main -->|runs stages| Loader
    Main -->|runs stages| Cost
    Main -->|runs stages| Grad
    Main -->|runs stages| LinReg
    Main -->|runs stages| Plot
    Grad -->|logs θ| Logs
    Plot -->|reads logs| Logs
    Plot -->|plots PNG| Model
    Model -->|model.txt| Predict
    Model -->|model.txt| Precision
    TestCSV -->|test_data.csv| Precision

    %% Click Events
    click DataCSV "https://github.com/0bada1/ft_linear_regression/blob/main/data/data.csv"
    click TestCSV "https://github.com/0bada1/ft_linear_regression/blob/main/data/test_data.csv"
    click Loader "https://github.com/0bada1/ft_linear_regression/blob/main/srcs/train_model/load_data.py"
    click Cost "https://github.com/0bada1/ft_linear_regression/blob/main/srcs/train_model/cost.py"
    click Grad "https://github.com/0bada1/ft_linear_regression/blob/main/srcs/train_model/gradient_descent.py"
    click LinReg "https://github.com/0bada1/ft_linear_regression/blob/main/srcs/train_model/linear_regression.py"
    click Plot "https://github.com/0bada1/ft_linear_regression/blob/main/srcs/train_model/plot.py"
    click Logs "https://github.com/0bada1/ft_linear_regression/tree/main/srcs/train_model/log"
    click Main "https://github.com/0bada1/ft_linear_regression/blob/main/srcs/train_model/main.py"
    click Main "https://github.com/0bada1/ft_linear_regression/blob/main/srcs/train_model/train_model.sh"
    click Model "https://github.com/0bada1/ft_linear_regression/blob/main/srcs/train_model/model.txt"
    click Predict "https://github.com/0bada1/ft_linear_regression/blob/main/srcs/predict/predict.py"
    click Precision "https://github.com/0bada1/ft_linear_regression/blob/main/srcs/precision/calculate_precision.py"

    %% Styles
    classDef dataStore fill:#cce5ff,stroke:#3399ff,stroke-width:2px;
    classDef compute fill:#d4edda,stroke:#28a745,stroke-width:2px;
    classDef artifact fill:#fff3cd,stroke:#ffcd39,stroke-width:2px;
    classDef cli fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
```
