https://github.com/mahdikh03/knn-algorithm-on-irisdataset
A machine learning project to implement the K-Nearest Neighbors (KNN) algorithm from scratch using Python for classification on the Iris dataset.
https://github.com/mahdikh03/knn-algorithm-on-irisdataset
iris-dataset knn-classification machine-learning
Last synced: about 1 year ago
JSON representation
A machine learning project to implement the K-Nearest Neighbors (KNN) algorithm from scratch using Python for classification on the Iris dataset.
- Host: GitHub
- URL: https://github.com/mahdikh03/knn-algorithm-on-irisdataset
- Owner: MahdiKh03
- Created: 2024-08-23T12:09:38.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-22T12:56:23.000Z (over 1 year ago)
- Last Synced: 2025-05-09T02:13:08.070Z (about 1 year ago)
- Topics: iris-dataset, knn-classification, machine-learning
- Language: Python
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# K-Nearest Neighbors (KNN) Classifier on Iris Dataset
This Python script implements a basic K-Nearest Neighbors (KNN) classifier using the famous Iris dataset. The script performs the following steps:
1. **Load the Dataset**: The Iris dataset is loaded using Pandas, and the data is split into training and testing sets.
2. **Data Preprocessing**: The dataset is divided randomly into training and testing sets. Each item is then numbered for easy tracking.
3. **Distance Calculation**: The Euclidean distance is calculated between the testing data and the training data for each test sample.
4. **K-Nearest Neighbors**: The script finds the nearest 10 neighbors for each test sample and performs classification based on the majority vote.
5. **Accuracy Calculation**: The accuracy of the classifier is determined by comparing the predicted labels with the actual labels from the testing set.
## Requirements:
- Python 3.x
- Pandas library
- Math and Statistics libraries (both are standard Python libraries)
## Files:
- `Iris.csv`: Contains the Iris dataset with the following columns: Sepal Length, Sepal Width, Petal Length, Petal Width, and Class (label).
## How the Script Works:
1. **Data Loading**: The dataset is read from `Iris.csv` using `pandas.read_csv()`.
2. **Training and Testing Split**: The data is split into a random 100-point training set and the rest as a testing set.
3. **Distance Calculation**: The script calculates the Euclidean distance between each test sample and all training samples.
4. **Find Nearest Neighbors**: The nearest 10 neighbors for each test sample are identified.
5. **Label Prediction**: The class label for each test sample is predicted based on the majority vote from its nearest neighbors.
6. **Accuracy**: The percentage of correctly predicted labels is printed as the final result.
## How to Run:
1. Ensure you have Python installed along with the required libraries.
2. Place the `Iris.csv` file in the same directory as the Python script.
3. Run the script by executing `python knn_classifier.py` in the terminal.
4. The script will output the accuracy of the classifier based on the K-Nearest Neighbors algorithm.