Welcome to Machine Learning in C, a collection of hands-on mini-projects that demonstrate core machine learning concepts using the C programming language. This repository is designed for students, enthusiasts, and anyone interested in understanding how classic ML algorithms work under the hood, without relying on high-level libraries.
This repository contains three independent mini-projects, each focusing on a different machine learning paradigm:
- K-Nearest Neighbors (KNN) – Supervised Learning (Classification)
- K-Means Clustering – Unsupervised Learning (Clustering)
- Tic-Tac-Toe with Q-Learning – Reinforcement Learning (Game AI)
Each project is self-contained, with its own code, dataset, and results. All implementations are written in C for maximum transparency and educational value.
ML_in_C/
├── K_means_ML/ # K-Means clustering implementation
│ ├── iris.data # Iris dataset
│ ├── k_means_main.c # Main C source file
│ ├── value_of_assignments_and_clusters.csv # Output results
│ └── README.md # Project-specific instructions
│
├── KNN_ML/ # K-Nearest Neighbors implementation
│ ├── iris.data # Iris dataset
│ ├── knn_algorithm_main.c # Main C source file
│ ├── results.csv # Output results
│ └── README.md # Project-specific instructions
│
├── Tic-Tac-Toe_ML/ # Tic-Tac-Toe with Q-Learning
│ ├── tic_tac_toe_main.c # Main C source file
│ ├── q_table.csv # Q-table for the agent
│ ├── a.exe # Compiled binary (example)
│ └── README.md # Project-specific instructions
│
└── README.md # (You are here)
- Type: Supervised Learning (Classification)
- Dataset: Iris Dataset
- Goal: Classify iris flowers into three species based on sepal/petal measurements.
- Features:
- Reads and normalizes the Iris dataset
- Implements the KNN algorithm from scratch
- Outputs predictions and accuracy to
results.csv
- Learning Objectives:
- Understand distance metrics and nearest neighbor search
- Learn about data normalization and supervised learning
How to Run:
- Navigate to
KNN_ML/
. - Compile:
gcc knn_algorithm_main.c -o knn
- Run:
./knn
(orknn.exe
on Windows)
- Type: Unsupervised Learning (Clustering)
- Dataset: Iris Dataset
- Goal: Cluster iris data into groups without using species labels.
- Features:
- Random centroid initialization
- Iterative assignment and centroid update
- Outputs cluster assignments to
value_of_assignments_and_clusters.csv
- Learning Objectives:
- Understand unsupervised learning and clustering
- Learn about centroid-based algorithms and convergence
How to Run:
- Navigate to
K_means_ML/
. - Compile:
gcc k_means_main.c -o kmeans
- Run:
./kmeans
(orkmeans.exe
on Windows)
- Type: Reinforcement Learning (Game AI)
- Goal: Train an AI agent to play Tic-Tac-Toe using Q-Learning.
- Features:
- Implements Q-Learning from scratch
- Agent learns by playing games and updating a Q-table
- Q-table can be saved/loaded (
q_table.csv
) - Play against the AI and watch it improve
- Learning Objectives:
- Understand reinforcement learning concepts (states, actions, rewards)
- Learn about Q-tables and epsilon-greedy policies
How to Run:
- Navigate to
Tic-Tac-Toe_ML/
. - Compile:
gcc tic_tac_toe_main.c -o ttt
- Run:
./ttt
(orttt.exe
on Windows)
- GCC or any C99-compatible compiler
- No external libraries required (standard C only)
- (Optional) Python or spreadsheet software to view CSV results
Contributions are welcome! Feel free to open issues or submit pull requests for improvements, bug fixes, or new ML mini-projects in C.
This project is open-source and available under the MIT License.
- UCI Machine Learning Repository – Iris Dataset
- Inspired by classic ML textbooks and C programming tutorials.
Explore each folder, read the code, and experiment with the algorithms. This project is a great way to demystify machine learning by building it from scratch in a low-level language.