An open API service indexing awesome lists of open source software.

https://github.com/neuraladitya/polynomial_regression_c

A high-performance polynomial regression implementation in pure C with gradient descent optimization and visualization support.
https://github.com/neuraladitya/polynomial_regression_c

algorithm-implementation c-programming csv-processing data-science data-visualization high-performance-computing machine-learning numerical-computing polynomial-regression regression-analysis

Last synced: about 2 months ago
JSON representation

A high-performance polynomial regression implementation in pure C with gradient descent optimization and visualization support.

Awesome Lists containing this project

README

        

# Polynomial Regression from Scratch in C

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![C Standard](https://img.shields.io/badge/C-99-blue)
![Build Status](https://img.shields.io/badge/build-passing-brightgreen)

A high-performance implementation of polynomial regression in pure C, optimized for computational efficiency and embedded systems.

![Regression Visualization](docs/regression_plot.png)

## Features

- **Fast Computation ๐Ÿš€** - Highly optimized for speed and low memory usage
- **Zero Dependencies ๐Ÿงน** - Uses only the C99 standard library
- **Polynomial Regression ๐Ÿ“ˆ** - Supports any degree polynomial fitting
- **Visualization Support ๐Ÿ“Š** - Python-based visualization of regression results
- **Educational & Modular ๐ŸŽ“** - Clear implementation for easy learning and extension

## Benchmarks

| Metric | This Implementation | Python (sklearn) |
|---------------------|---------------------|------------------|
| 100K samples (ms) | 18 | 470 |
| Memory Usage (MB) | 3.5 | 50.2 |
| Binary Size (KB) | 52 | N/A |

*Tested on Intel i7-1185G7 @ 3.00GHz*

## Installation

### Requirements

- GCC or Clang compiler
- Python 3.8+ (for visualization only)

### Build Instructions

```bash
# Clone repository
git clone https://github.com/NeuralAditya/Polynomial_Regression_C.git
cd Polynomial_Regression_C

# Compile with optimizations
gcc src/polynomial_regression.c -o polyreg -Wall -Wextra -lm
./polyreg

# Generate sample data (optional)
python scripts/generate_data.py
```

## Usage

### Basic Training

```bash
./lr data/synthetic.csv
```

### Visualization

```bash
python scripts/plot_results.py
```

### Command Line Options

| Flag | Description | Default |
|--------------|------------------------------|---------|
| `-d` | Polynomial Degree | 1000 |
| `-e` | Number of epochs | 1000 |
| `-l` | Learning rate | 0.01 |
| `-o` | Output predictions file | predictions.csv |

## Project Structure

```
Polynomial_Regression_C/
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ polynomial_regression.c # Core algorithm
โ”‚ โ”œโ”€โ”€ data_loader.c # CSV parser
โ”‚ โ””โ”€โ”€ polynomial_regression.h # Interface
โ”œโ”€โ”€ scripts/
โ”‚ โ”œโ”€โ”€ plot_results.py # Plotting
โ”‚ โ”œโ”€โ”€ generate_data.py # Data generation
โ”œโ”€โ”€ data/
โ”‚ โ”œโ”€โ”€ synthetic.csv # Sample dataset
โ”‚ โ”œโ”€โ”€ predictions.csv # Model predictions
โ”œโ”€โ”€ docs/
โ”‚ โ””โ”€โ”€ regression_plot.png # Visualization output
โ””โ”€โ”€ tests/ # Unit tests (future)
```

## Algorithm Details

### Gradient Descent for Polynomial Coefficients

```c
void train(Model *model, Dataset *data, Hyperparams *params) {
for (int epoch = 0; epoch < params->epochs; epoch++) {
double gradients[MAX_DEGREE] = {0};

for (int i = 0; i < data->n_samples; i++) {
double prediction = predict(model, data->X[i]);
double error = prediction - data->y[i];

for (int j = 0; j <= model->degree; j++) {
gradients[j] += error * pow(data->X[i], j);
}
}

for (int j = 0; j <= model->degree; j++) {
model->theta[j] -= params->lr * (gradients[j] / data->n_samples);
}
}
}
```

### Key Optimizations

1. **Batch Processing** - Efficient computation for large datasets
2. **Matrix Formulations** - Utilizes matrix operations for normal equations
3. **Floating-Point Stability** - Reduces precision errors in higher-degree polynomials

## Applications

- Predicting trends in time-series data
- Stock market or financial forecasting
- Sensor data modeling
- Educational ML implementations

## Roadmap

- [x] Polynomial regression with gradient descent
- [x] CSV data loading and preprocessing
- [x] Prediction and model evaluation
- [x] Data visualization using Python
- [ ] Multi-threaded training for performance boost
- [ ] GPU acceleration with CUDA/OpenCL
- [ ] Support for higher-degree polynomials dynamically
- [ ] Model serialization and checkpointing
- [ ] Unit test framework for robustness

## Contributing

1. Fork the repository
2. Create your feature branch
3. Commit your changes
4. Push to the branch
5. Open a Pull Request

## License

Distributed under the MIT License. See `LICENSE` for more information.

## Contact

Aditya Arora - [email protected]

Project Link: [https://github.com/NeuralAditya/Polynomial_Regression_C](https://github.com/NeuralAditya/Polynomial_Regression_C)