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.exeon 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.exeon 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.exeon 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.