Skip to content

LucassSantoss/k-Nearest-Neighbors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

K-Nearest Neighbors (k-NN) Classification Algorithm

This code provides an implementation of the k-Nearest Neighbors (k-NN) classification algorithm. The goal of the algorithm is to assign classes to unlabeled examples based on the classes of similar known examples.

Functions:

  1. read_examples(file_name): This function reads a file called 'iris.data.csv' that contains classified examples and returns a matrix containing these examples, excluding the header.

  2. read_unclassified(file_name): This function reads a provided file containing unlabeled examples and returns a matrix with these examples.

  3. euclidean_distance(known_example, unknown_example): This function calculates the Euclidean distance between a known example and an unlabeled example based on their numerical features.

  4. calculate_distances(classified_examples, unclassified_examples, k): This function receives matrices of classified and unclassified examples, along with a value k, and calculates the distances between the unclassified examples and the classified examples. The function returns a matrix containing the k nearest instances for each unclassified example, along with their respective classes.

  5. determine_class(nearest_instances): This function receives the matrix generated by the previous function and determines the most frequent class among the k nearest instances for each unclassified example. In case of a tie, the class is randomly selected. The function also writes the determined classes to the 'iris.data.csv' file.

  6. main(): This function is responsible for the main execution of the program. It prompts the user for a value of k and the name of a file containing the unclassified examples. It then calls the necessary functions to classify the examples and determine their classes.

The code uses the math library for mathematical calculations and the choice function from the random module to perform random selection in case of a tie in class determination.

At the end, the determined classes for the unclassified examples are printed on the screen and added to the 'iris.data.csv' file.

How to Use:

  1. Make sure you have the 'iris.data.csv' file in the same directory as the script.

  2. Execute the script.

  3. Enter the value of k when prompted.

  4. Enter the name of the file containing the unclassified examples when prompted.

  5. The determined classes for the unclassified examples will be displayed on the screen and added to the 'iris.data.csv' file.

Note: Make sure the unclassified examples file is properly formatted and contains the same number of features as the classified examples in the 'iris.data.csv' file.

Please feel free to modify the code according to your specific needs.

About

Artificial intelligence project on python.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages