# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# Objective : To solve the for the classification problem for the iris data ans regression for cars dataset using 1-NN and k-NN algorithm .

# Below codes are to import the functions and read the files.
# Cars and Iris datasets are used for the KNN prediction

# +
#Importing the functions: 
import numpy as np
import pandas as pd
import os                           
import statistics                         # import statistics for finding the mode in classification

# IMPORTING AND READING THE CARS FILE
cars=pd.read_csv("cars.csv")              #Reading the file using pandas

# IMPORTING AND READING THE IRIS FILE
iris=pd.read_csv("iris.csv")              #Reading the file using pandas


# -

# Two function are defined.  
# (A) Euclidean function is used to calculate the distance between the two points in a space.
#
# (B) The std() function is used to standarize the dataset as there are different data are been observed to be in different scales. Iris data is standardised because all the predictor values are in same scale.

# +
def euclidean(a,b):               #Function to measure the distance between the two points
    d = np.sqrt(sum((a-b)**2))    #Euclidean distance formula
    return(d)

def std(cars):                    #Function to standardize the predictors(X) and the target data(Y)
                                  # Formula used to standardize:  ( x - mean(x) / SD )
    
    cars.MPG=(cars.MPG - cars.MPG.mean())/cars.MPG.std()
    cars.Cylinders=(cars.Cylinders - cars.Cylinders.mean())/cars.Cylinders.std()
    cars.Displacement=(cars.Displacement - cars.Displacement.mean())/cars.Displacement.std()
    cars.Horsepower=(cars.Horsepower - cars.Horsepower.mean())/cars.Horsepower.std()
    cars.Weight=(cars.Weight - cars.Weight.mean())/cars.Weight.std()
    
    return


# -

# #### Function for 1-NN (Regression) for all the values in Y-test  in the cars dataset:  

# +
def one_NN_cars():

    std(cars)              # calling the function to standardize the data

#Sampling the dataset and splitting them into test and train dataset: (Train dataset = 300 values ; Test dataset =106 values)

    data=cars.sample(len(cars))                         #random sampling

    trainY_mpg= np.array(data.iloc[0:300,1])            #Target Variables - spliting dataset into test data and train data
    testdataY_mpg= np.asarray(data.iloc[300:406,1])

    traindatasX= np.array(data.iloc[0:300,[3,4,5,6]])   #Predictors -  spliting the dataset into test data and train data
    testdatasX= np.array(data.iloc[300:406,[3,4,5,6]])

    # Finding the 1st nearest neighbour for all the test data :

    nearval_xtest=[]                                   #List to store the nearest neighbour(Target variables)
    for i in range(len(testdatasX)):                   # To pick each value from the test dataset

        temp_list=[]                                   #Creating a list to append the euclidean distance values
            
#for loop: to calc euclidean distance between each test data and all other data in train data set.

        for k in range(len(traindatasX)):     
            dist=euclidean(testdatasX[i],traindatasX[k])    #Calc euclidean diatance
            temp_list.append(dist)                          #appending all distance values to the list : temp_list

        temp_list=np.array(temp_list)          #converting into numpy array to find minimum value
        value=np.min(temp_list)                #Min value is taken   
        newlist=temp_list.tolist()             #converting into list to find minimum value
        indexval=newlist.index(value)          #finding the index of min value
        
# the index value of the minimum euclidean dist value is being matched with the index value of the Y test dataset

        nearval_xtest.append(trainY_mpg[indexval])    #appending y value of the min. euclidean distance from the y train


    nearval_xtest=np.array(nearval_xtest)             # converting into numpy array

    print("Predicted values \t\t Actual Values \t\t   Difference between Prediction and Actual")
    
    for i in range (len(nearval_xtest)):              # Iteration to print all the values

        print(nearval_xtest[i],"\t\t",testdataY_mpg[i], "\t\t" ,(abs(nearval_xtest[i]-testdataY_mpg[i])))
        
    return

# -

# #### Funtion to find the K nearest neighbours (Regression) for all the Y-test values in the cars dataset:  

# +
def K_NN_cars(k):
    std(cars)            # calling the function to standardize the data

#Sampling the dataset and splitting them into test and train dataset: (Train dataset = 300 values ; Test dataset =106 values)    #Sampling the dataset and splitting into test and train dataset

    data=cars.sample(len(cars))                         #random sampling

    trainY_mpg= np.array(data.iloc[0:300,1])           #Target Variables - spliting dataset into test data and train data
    testdataY_mpg= np.asarray(data.iloc[300:406,1])

    traindatasX= np.array(data.iloc[0:300,[3,4,5,6]])   #Predictors -  spliting into test data and train data
    testdatasX= np.array(data.iloc[300:406,[3,4,5,6]])

# Finding the K nearest neighbour for all the test data :
    
    nearval_xtest=[]                        #List to store the nearest neighbour(Target variables)
    for i in range(len(testdatasX)):        # To pick each value from the test dataset

        temp_list=[]                        # Temporary list to store the distance values

        for y in range(len(traindatasX)):  #for loop the compare selected test data with all other data in train

            dist=euclidean(testdatasX[i],traindatasX[y])    #Calc euclidean diatance
            temp_list.append(dist)                          #Appending all distance values to the list : temp_list


        temp_list=np.array(temp_list)     #converting into numpy array to find minimum value

        newlist_dup= temp_list.tolist()   #saving a copy for getting the index value later
        newlist    = temp_list.tolist()
        newlist.sort()    
        newlist=newlist[0:k]              #sorting in ascending order and slicing to k values to get nearest k distances

        for v in newlist:
            val_y_4avg=[]
            indexval=newlist_dup.index(v)                 #Finding the index of the min value (Closest values)
            val_y_4avg.append(trainY_mpg[indexval])       #Appending y value of the min from the y train

        avg_pred_val=np.average(val_y_4avg)               # converting into numpy array
        nearval_xtest.append(avg_pred_val)                # appending values to a new list for better output


    print("Predicted values \t\t Actual Values \t\t   Difference between Prediction and Actual")
    for i in range (len(nearval_xtest)):                  #Iteration to print all the values

        print(nearval_xtest[i],"\t\t",testdataY_mpg[i] , "\t\t" ,(abs(nearval_xtest[i]-testdataY_mpg[i])))
    return


# -

# #### Function to find the 1st nearest neighbour for Y-test all the iris dataset :

# +
def one_NN_iris():
    
#Sampling the dataset and splitting them into test and train dataset: (Train dataset = 100 values ; Test dataset =50 values

    data=iris.sample(len(iris))    #random sampling

    trainY= np.array(data.iloc[0:100,4])           #Target Variables - spliting into test and train dataset
    testY= np.asarray(data.iloc[100:150,4])

    traindatasX= np.array(data.iloc[0:100,[0,1,2,3]])   #Predictors -  spliting into test and train dataset
    testdatasX= np.array(data.iloc[100:150,[0,1,2,3]])

# Finding the 1st nearest neighbour for all the test data :

    nearval_xtest=[]                     #List to store the nearest neighbour(Target variables)
    for i in range(len(testdatasX)):     # To pick each value from the test dataset

        temp_list=[]                     # Temporary list to store the distance values

        for k in range(len(traindatasX)):    #for loop the compare selected test data with all other data in train

            dist=euclidean(testdatasX[i],traindatasX[k])    #Calc euclidean diatance
            temp_list.append(dist)                          #adding all distance to the list


        temp_list=np.array(temp_list)   #converting to numpy array for finding the minimum value
        value=np.min(temp_list)         #Min value is taken (Closest neighbour)
        newlist=temp_list.tolist()
        indexval=newlist.index(value)    #Finding the index of min value


        nearval_xtest.append(trainY[indexval])    #Appending y value of the min distance from the y train



    nearval_xtest=np.array(nearval_xtest)        # converting into numpy array for better output

    print("Predicted values \t Actual Values")
    for i in range (len(nearval_xtest)):         #Iteration to print all the values

        print(nearval_xtest[i],"\t\t",testY[i])
        
    return


# -

# #### Function to find the K-nearest neighbour for Y-test all the iris dataset :

def knn_iris(k_val):
    
    #Sampling the datasea and splitting into test and train dataset

    data=iris.sample(len(iris))    #random sampling

    trainY= np.array(data.iloc[0:100,4])           #Target Variables - spliting into test and train dataset
    testY= np.asarray(data.iloc[100:150,4])

    traindatasX= np.array(data.iloc[0:100,[0,1,2,3]])   #Predictors -  spliting into test and train dataset
    testdatasX= np.array(data.iloc[100:150,[0,1,2,3]])

    nearval_xtest=[]                     #List to store the nearest neighbour(Target variables)
    
    for i in range(len(testdatasX)):     # To pick each value from the test dataset

        temp_list=[]

        for k in range(len(traindatasX)):    #for loop the compare selected test data with all other data in train

            dist=euclidean(testdatasX[i],traindatasX[k])    #Calc euclidean diatance
            temp_list.append(dist)                          #adding all distance values to the list


        temp_list=np.array(temp_list)     #numpy array for further calculations
        newlist_dup= temp_list.tolist()   #saving a copy of the list 
        newlist    = temp_list.tolist()   #Converting to list for the sorting of the dataset
        newlist.sort()    
        newlist=newlist[0:k_val]              #sorting in ascending order to get nearest 5 neighbours

        val_y_4avg=[]
        for i in newlist:
            indexval=newlist_dup.index(i)                 #finding the index of min value
            val_y_4avg.append(trainY[indexval])           #appending  min of y values from the y train

        avg_pred_val=statistics.mode(val_y_4avg)          # converting into numpy array for better output
        nearval_xtest.append(avg_pred_val)

    #nearval_xtest=np.array(nearval_xtest)
    print("Predicted values \t Actual Values")
    for i in range (len(nearval_xtest)):                  #Iteration to print all the values

        print(nearval_xtest[i],"\t\t",testY[i])
        
    return


# ###  A. 1-NN regression for cars dataset

# +
#Function to caculate the Predicted Y values from the cars dataset(Target values: Weight,HP,Displacement,Acceleration)

one_NN_cars() 
# -

# ### B. 1-NN classification for iris dataset 

# +
#Function to caculate the Predicted Y values from the iris dataset(Target values: Sepal-length and width,Petal-length and width)
    
one_NN_iris()    
# -

# ### C-1. K-NN regression for cars dataset
#

# +
#Function to caculate the Predicted Y values from the cars dataset(Target values: Weight,HP,Displacement,Acceleration)
#Value of K is passsed through the function

K_NN_cars(10)   # K-value is 10
# -

# ### C-2. K-NN regression for iris dataset
#

#Function to caculate the Predicted Y values from the iris dataset(Target values: Sepal-length and width,Petal-length and width)
#Value of K is 5
knn_iris(5)



StarcoderdataJupyterScriptsDedupFiltered