<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 IMPLEMENTING K_NEAREST_NEIGHBOUR
In the given data set we have to classify into which cluster a instance is going to fall
Importing required predifined methods
Step1:  reading the training data into a data frame and assigning headings
Step2:  reading the testing data into a data frame and assigning headings
Step3:  changing the strings in the train data into to numbers so that they can be used while calculating euclidean distance
Step4:  changing the strings in the test data into to numbers so that they can be used while calculating euclidean distance
Step5:  Normalizing the values in the Vacation,eCredit,Salary,Property so that all the values range in between 0 to 1.we use a formula to do  this.we are going to capture min an max for the categories which should be normalized so that we can use them for test data normalization also
Step6:  normalizing the testing data too
Step7:  converting all the data too float into make calculations accurate
Step8:  we have to shuffle data so that we can see how good our own function is classifing the instances into clusters
Step9:  developing our own k nearest algorithm with respective to our requirments, we use similarity matrix to take decision between then two parameters, and the rest we can go with euclidean distance.
Step10:  splitting the data with respective to ratio and segregating the train an test data into respective clustres
Step11:  Removing the cluster names from the instances and adding them to lists.
Step12:  Sending our slpitted test and train from the given train data in order to cross validadate
Step13:  Sending our actual testing data to find the accuracy our model
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
import numpy as np
from collections import Counter
from math import sqrt
import random
import warnings
Explanation: IMPLEMENTING K_NEAREST_NEIGHBOUR
In the given data set we have to classify into which cluster a instance is going to fall
Importing required predifined methods
End of explanation
df = pd.read_table('train.csv', sep=',', header=None, names=['Type', 'LifeStyle', 'Vacation', 'eCredit', 'Salary', 'Property', 'Label'])
df.head()
Explanation: reading the training data into a data frame and assigning headings
End of explanation
dft = pd.read_table('test.csv', sep=',', header=None, names=['Type', 'LifeStyle', 'Vacation', 'eCredit', 'Salary', 'Property', 'Label'])
dft.head()
Explanation: reading the testing data into a data frame and assigning headings
End of explanation
df['Type'] = df.Type.map({'student':1,'engineer':2,'librarian':3,'professor':4,'doctor':5 }) 
# df.head()
df['LifeStyle'] = df.LifeStyle.map({'spend<<saving':1, 'spend<saving':2, 'spend>saving':3, 'spend>>saving':4}) 
df['Label'] = df.Label.map({'C1':1, 'C2':2 ,'C3':3 ,'C4':4 ,'C5':5})
# df['Vacation']=df['Vacation']/100
df.head()
Explanation: changing the strings in the train data into to numbers so that they can be used while calculating euclidean distance
End of explanation
dft['Type'] = dft.Type.map({'student':1,'engineer':2,'librarian':3,'professor':4,'doctor':5 }) 
# df.head()
dft['LifeStyle'] = dft.LifeStyle.map({'spend<<saving':1, 'spend<saving':2, 'spend>saving':3, 'spend>>saving':4}) 
dft['Label'] = dft.Label.map({'C1':1, 'C2':2 ,'C3':3 ,'C4':4 ,'C5':5})
# df['Vacation']=df['Vacation']/100
dft.head()
Explanation: changing the strings in the test data into to numbers so that they can be used while calculating euclidean distance
End of explanation
vacmaxval=0
vacminval=0
ecrmaxval=0
ecrminval=0
salmaxval=0
salminval=0
prpminval=0
prpmaxval=0
for attribute in list(df.columns.values):
    if attribute == 'Vacation' or attribute == 'eCredit' or attribute == 'Salary' or attribute == 'Property':
        if attribute == 'Vacation':
            vacmaxval=max(df[attribute])
            vacminval=min(df[attribute])
        elif attribute == 'eCredit':
            ecrmaxval=max(df[attribute])
            ecrminval=min(df[attribute])
        elif attribute == 'Salary':
            salmaxval=max(df[attribute])
            salminval=min(df[attribute])
        elif attribute == 'Property':
            prpmaxval=max(df[attribute])
            prpminval=min(df[attribute])
        maxValue = max(df[attribute])
        minValue = min(df[attribute])
        norm = []
        for i in df[attribute]:
            normalisedValue = (i - minValue + 0.0)/(maxValue - minValue + 0.0)
            norm.append(normalisedValue)
        df[attribute] = norm
df.head()
# uncomment the below line to get a csv file of the dataframe
#df.to_csv('sanitizedData.csv', sep=',', encoding='utf-8', header=False)
Explanation: Normalizing the values in the Vacation,eCredit,Salary,Property so that all the values range in between 0 to 1.we use a formula to do  this.we are going to capture min an max for the categories which should be normalized so that we can use them for test data normalization also
End of explanation
minValue=0
maxValue=0
for attribute in list(dft.columns.values):
    if attribute == 'Vacation' or attribute == 'eCredit' or attribute == 'Salary' or attribute == 'Property':
        norm = []
        for i in dft[attribute]:
            if attribute == 'Vacation':
                minValue=vacminval
                maxValue=vacmaxval
            elif attribute == 'eCredit':
                minValue=ecrminval
                maxValue=ecrmaxval
            elif attribute == 'Salary':
                minValue=salmaxval
                maxValue=salminval
            elif attribute == 'Property':
                minValue=prpminval
                maxValue=prpmaxval
            normalisedValue = (i - minValue + 0.0)/(maxValue - minValue + 0.0)
            norm.append(normalisedValue)
        dft[attribute] = norm
dft.head()
Explanation: normalizing the testing data too
End of explanation
dataframe= df.astype(float).values.tolist()
dataframet= dft.astype(float).values.tolist()
Explanation: converting all the data too float into make calculations accurate
End of explanation
#print dataframe[:10]
random.shuffle(dataframe)
#print dataframe[:10]
Explanation: we have to shuffle data so that we can see how good our own function is classifing the instances into clusters
End of explanation
def k_nearest(data,predict,k=5):
    #if(len(data)>=k):
    #    warnings.warn('bye')
    distances =[]
    for group in data:
        for features in  data[group]:
            euclidean_distance=0
            if(predict[0]!=features[0]):
                euclidean_distance+=1
            if(predict[1]!=features[1]):
                euclidean_distance+=1
            euclidean_distance += ((predict[2]-features[2])**2 + (predict[3]-features[3])**2 +(predict[4]-features[4])**2 + (predict[5]-features[5])**2)
            #euclidean_distance = np.linalg.norm(np.array(features)-np.array(predict))
            euclidean_distance=sqrt(euclidean_distance)
            distances.append([euclidean_distance,group])
    votes = [i[1] for i in sorted(distances) [:k]]
    #print distances
    print votes
    #print (Counter(votes).most_common(1))
    vote_result = Counter(votes).most_common(1)[0][0]
    #print vote_result
    
            
    return vote_result
Explanation: developing our own k nearest algorithm with respective to our requirments, we use similarity matrix to take decision between then two parameters, and the rest we can go with euclidean distance.
End of explanation
test_size=0.2
train_set={1.0:[],2.0:[],3.0:[],4.0:[],5.0:[]}
test_set={1.0:[],2.0:[],3.0:[],4.0:[],5.0:[]}
test_setnew={1.0:[],2.0:[],3.0:[],4.0:[],5.0:[]}
train_setnew={1.0:[],2.0:[],3.0:[],4.0:[],5.0:[]}
train_data= dataframe[:-int(test_size*len(dataframe))]
test_data= dataframe[-int(test_size*len(dataframe)):]
train_datanew= dataframe[:int(1*len(dataframe))]
test_datanew=dataframet[:int(1*len(dataframet))]
#print test_set
#print test_datanew
Explanation: splitting the data with respective to ratio and segregating the train an test data into respective clustres
End of explanation
for i in train_data:
    train_set[i[-1]].append(i[:-1])
    
for i in test_data:
    test_set[i[-1]].append(i[:-1])
    
#print test_set
    
for i in test_datanew:
    test_setnew[i[-1]].append(i[:-1])
for i in train_datanew:
    train_setnew[i[-1]].append(i[:-1])
    
    
#print test_set
#print test_setnew
    
Explanation: Removing the cluster names from the instances and adding them to lists.
End of explanation
correct=0.0
total=0.0
for group in test_set:
    for data in test_set[group]:
        vote= k_nearest(train_set,data,k=5)
        #print '*****'
        #print group
        #print vote
        #print '*****'
        if group==vote:
            correct+=1
        total +=1
print correct
print total
print ('Accuracy:', correct/total)
Explanation: Sending our slpitted test and train from the given train data in order to cross validadate
End of explanation
correct =0.0
total =0.0
for group in test_setnew:
    for data in test_setnew[group]:
        vote= k_nearest(train_set,data,k=5)
        print '*****'
        print group
        print vote
        #print '*****'
        if group==vote:
            correct+=1
        total +=1
print correct
print total
print ('Accuracy:', (correct)/total)
Explanation: Sending our actual testing data to find the accuracy our model
End of explanation
<END_TASK>