Skip to content

This repository features three mini-projects that introduce key machine learning concepts using C. Explore K-Nearest Neighbors for classification, K-Means for clustering, and Q-Learning for a Tic Tac Toe AI. Perfect for beginners looking to practice ML while enhancing their C programming skills.

Notifications You must be signed in to change notification settings

gragi-1/ML_in_C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Machine Learning in C

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.


πŸ“š Project Overview

This repository contains three independent mini-projects, each focusing on a different machine learning paradigm:

  1. K-Nearest Neighbors (KNN) – Supervised Learning (Classification)
  2. K-Means Clustering – Unsupervised Learning (Clustering)
  3. 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.


πŸ—‚οΈ Project Structure

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)

1️⃣ K-Nearest Neighbors (KNN)

  • 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:

  1. Navigate to KNN_ML/.
  2. Compile: gcc knn_algorithm_main.c -o knn
  3. Run: ./knn (or knn.exe on Windows)

2️⃣ K-Means Clustering

  • 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:

  1. Navigate to K_means_ML/.
  2. Compile: gcc k_means_main.c -o kmeans
  3. Run: ./kmeans (or kmeans.exe on Windows)

3️⃣ Tic-Tac-Toe with Q-Learning

  • 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:

  1. Navigate to Tic-Tac-Toe_ML/.
  2. Compile: gcc tic_tac_toe_main.c -o ttt
  3. Run: ./ttt (or ttt.exe on Windows)

πŸ“¦ Requirements

  • GCC or any C99-compatible compiler
  • No external libraries required (standard C only)
  • (Optional) Python or spreadsheet software to view CSV results

πŸ§‘β€πŸ’» How to Contribute

Contributions are welcome! Feel free to open issues or submit pull requests for improvements, bug fixes, or new ML mini-projects in C.


πŸ“œ License

This project is open-source and available under the MIT License.


πŸ™ Credits


πŸš€ Start Learning ML with C!

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.

About

This repository features three mini-projects that introduce key machine learning concepts using C. Explore K-Nearest Neighbors for classification, K-Means for clustering, and Q-Learning for a Tic Tac Toe AI. Perfect for beginners looking to practice ML while enhancing their C programming skills.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages