Demo entry 6763544

iris

   

Submitted by anonymous on Oct 23, 2018 at 14:13
Language: Python. Code size: 2.4 kB.

# Importing libraries
import pandas as pd
import math
import numpy as np
import operator

#### Start of STEP 1
# Importing data
data = pd.read_csv("iris.csv")
#### End of STEP 1
data.head()
# Defining a function which calculate euclidean distance between two data points


def euclideandistance(data1, data2, length):
    np_c1 = np.array(data1)
    np_c2 = np.array(data2)
    distance = 0
    for x in range(length):
        distance += np.square(data1[x] - data2[x])
    return np.sqrt(distance)

# Defining our KNN model
def knn(traning, testinstance, k):
    distances = {}
    length = testinstance.shape[1]
    # Start of STEP 3
    # Calculating euclidean distance between each row of training data and test data
    for x in range(len(traning)):
        #### Start of STEP 3.1
        dist = euclideandistance (testinstance, traning.iloc[x], length)
        distances[x] = dist[0]
        #### End of STEP 3.1
    #### Start of STEP 3.2
    # Sorting them on the basis of distance
    sorted_d= sorted(distances.items(), key=operator.itemgetter(1) )
    neighbors = []
    #### Start of STEP 3.3
    # Extracting top k neighbors
    for x in range(k):
        neighbors.append(sorted_d[x][0])
    #### End of STEP 3.3
    classvotes = {}

    #### Start of STEP 3.4
    # Calculating the most freq class in the neighbors
    for x in range(len(neighbors)):
        response = traning.iloc[neighbors[x]][-1]
        if response in classvotes:
           classvotes[response] += 1
        else:
           classvotes[response] = 1
    #### End of STEP 3.4
    #### Start of STEP 3.5
    sortedVotes = sorted(classvotes.items(), key=operator.itemgetter(1),reverse=True)
    return(sortedVotes[0][0], neighbors)
    #### End of STEP 3.5
# Creating a dummy testset
testset = [[7.2, 3.6,5.1,2.5]]
test = pd.DataFrame(testset)
#### Start of Step 2
# Setting number of neighbors = 1
k = 1
#### End of STEP 2
# Running KNN model
result, neigh = knn(data,test, k)
# Predicted class
print(result)

# Nearest neighbor
print(neigh)

# Setting number of neighbors = 3
k = 3
# Running KNN model
result, neigh = knn(data, test, k)
# Predicted class
print(result)
# 3 nearest neighbors
print(neigh)
# Setting number of neighbors = 5
k = 5
# Running KNN model
result, neigh = knn(data, test, k)
# Predicted class
print(result)
# 5 nearest neighbors
print(neigh)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).