# bahafn/from-scratch-linear-regression architecture

> Architecture diagram of the GitHub repository bahafn/from-scratch-linear-regression, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bahafn/from-scratch-linear-regression
- Repository: https://github.com/bahafn/from-scratch-linear-regression

## Overview

This is a small C linear-regression example, not a general data platform. Its main workflow reads a numeric CSV dataset, splits it into training and test sets, fits feature scaling on the training set and applies it to both sets, trains a least-squares model, predicts test targets, and reports regression metrics. The implementation also provides matrix storage and a linear-system solver beneath model training. Some implementation details, including prediction and metric internals, were not sampled; the map shows only their documented or explicit use.

Main entry points: [`src/main.c`](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/main.c) (Example runner), [`src/linear_regression.c`](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c) (CSV reader), [`src/matrix.c`](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/matrix.c) (Linear solver), [`src/linear_regression.h`](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.h) (Dataset storage)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_workflow["Example workflow"]
  node_example["Example runner<br/>[main.c]"]
  node_results["Printed results<br/>[main.c]"]
end

subgraph group_data["Dataset preparation"]
  node_csv_reader["CSV reader"]
  node_numeric_parser["Numeric parsing<br/>[string_utils.h]"]
  node_dataset["Dataset storage"]
  node_split["Train/test split"]
  node_scaler_fit["Fit min-max scaling"]
  node_scaler_transform["Transform features"]
end

subgraph group_model["Regression and math"]
  node_training["Train model"]
  node_normal_equation["Build normal equation"]
  node_matrix["Matrix operations<br/>[matrix.c]"]
  node_solver["Linear solver<br/>[matrix.c]"]
  node_predict["Batch prediction"]
  node_metrics["Regression metrics"]
end

node_input(("CSV dataset"))

node_input -->|"starts example"| node_example
node_example -->|"reads CSV"| node_csv_reader
node_csv_reader -->|"parses values"| node_numeric_parser
node_csv_reader -->|"creates dataset"| node_dataset
node_example -->|"splits dataset"| node_split
node_split -->|"creates subsets"| node_dataset
node_example -->|"fits on train"| node_scaler_fit
node_example -->|"transforms subsets"| node_scaler_transform
node_example -->|"trains on train"| node_training
node_training -->|"builds equation"| node_normal_equation
node_normal_equation -->|"uses matrix data"| node_matrix
node_training -->|"solves system"| node_solver
node_solver -->|"uses matrix operations"| node_matrix
node_example -->|"predicts test rows"| node_predict
node_example -->|"calculates metrics"| node_metrics
node_example -->|"prints model and metrics"| node_results

click node_example "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/main.c"
click node_csv_reader "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c"
click node_numeric_parser "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/string_utils.h"
click node_dataset "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.h"
click node_split "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c"
click node_scaler_fit "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c"
click node_scaler_transform "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c"
click node_training "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c"
click node_normal_equation "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c"
click node_matrix "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/matrix.c"
click node_solver "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/matrix.c"
click node_predict "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c"
click node_metrics "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c"
click node_results "https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/main.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_example,node_results toneBlue
class node_csv_reader,node_numeric_parser,node_dataset,node_split,node_scaler_fit,node_scaler_transform toneAmber
class node_training,node_normal_equation,node_matrix,node_solver,node_predict,node_metrics toneMint
class node_input toneIndigo
```

## Components

### Example workflow

- [Example runner](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/main.c): `src/main.c`
- [Printed results](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/main.c): `src/main.c`

### Dataset preparation

- [CSV reader](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c): `src/linear_regression.c`
- [Numeric parsing](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/string_utils.h): `src/string_utils.h`
- [Dataset storage](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.h): `src/linear_regression.h`
- [Train/test split](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c): `src/linear_regression.c`
- [Fit min-max scaling](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c): `src/linear_regression.c`
- [Transform features](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c): `src/linear_regression.c`

### Regression and math

- [Train model](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c): `src/linear_regression.c`
- [Build normal equation](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c): `src/linear_regression.c`
- [Matrix operations](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/matrix.c): `src/matrix.c`
- [Linear solver](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/matrix.c): `src/matrix.c`
- [Batch prediction](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c): `src/linear_regression.c`
- [Regression metrics](https://github.com/bahafn/from-scratch-linear-regression/blob/master/src/linear_regression.c): `src/linear_regression.c`

### Other components

- **CSV dataset**

## Connections

- CSV dataset → Example runner: starts example
- Example runner → CSV reader: reads CSV
- CSV reader → Numeric parsing: parses values
- CSV reader → Dataset storage: creates dataset
- Example runner → Train/test split: splits dataset
- Train/test split → Dataset storage: creates subsets
- Example runner → Fit min-max scaling: fits on train
- Example runner → Transform features: transforms subsets
- Example runner → Train model: trains on train
- Train model → Build normal equation: builds equation
- Build normal equation → Matrix operations: uses matrix data
- Train model → Linear solver: solves system
- Linear solver → Matrix operations: uses matrix operations
- Example runner → Batch prediction: predicts test rows
- Example runner → Regression metrics: calculates metrics
- Example runner → Printed results: prints model and metrics
