Reputation: 1
i have not been using python for some time and having trouble finding the solution to this problem. I tried to change the equation for euclidean distance function but it did not do any good. Maybe i am blind to see the solution to my problem.
Here is my code:
from math import sqrt
import csv
from random import shuffle
import numpy as np
import numpy
import matplotlib.pyplot as plt
import operator
import math
iris = datasets.load_iris()
X = iris.data
y = iris.target
def euclideanDistance(id1, id2):
for x in range(len(id1)-1):
dist = np.sqrt(np.sum((int(id2[x]) - int(id1[x]))**2))
return dist
data1 = [2, 2, 2, 'a']
data2 = [4, 4, 4, 'b']
distance = euclideanDistance(data1, data2)
print(distance)
def mykNN(X, y, x_):
distance = []
neighbour = []
for i in range(len(X)):
d = euclideanDistance(X[i], x_ )
distance.append((X[i], d))
distance.sort(key=operator.itemgetter(1))
for r in range(k):
options.append(distance[r][0])
options = neighbour
return neighbour
k=3
y_ = mykNN(X, y,k)
print(y_)
no matter what how i change my function it comes up with this error.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-85-2a5bfc4a796d> in <module>
42 k=3
43
---> 44 y_ = mykNN(X, y,k)
45 print(y_)
46
<ipython-input-85-2a5bfc4a796d> in mykNN(X, y, x_)
31
32 for i in range(len(X)):
---> 33 d = euclideanDistance(X[i], x_ )
34 distance.append((X[i], d))
35 distance.sort(key=operator.itemgetter(1))
<ipython-input-85-2a5bfc4a796d> in euclideanDistance(id1, id2)
18 def euclideanDistance(id1, id2):
19 for x in range(len(id1)-1):
---> 20 dist = np.sqrt(np.sum((int(id2[x]) - int(id1[x]))**2))
21 return dist
22
TypeError: 'int' object is not subscriptable
I would appreciate your response as this has been bothering me.
Thank you.
Upvotes: 0
Views: 313
Reputation: 7860
Well, the error is telling you that in euclideanDistance()
, either id1
or id2
(or both) is an integer, since those are the two identifiers you're indexing on that line. To follow it through:
k = 3
mykNN(X, y, k)
, which means that in mykNN()
, x_ == 3
.euclideanDistance(X[i], x_)
, which means that in euclideanDistance()
, id2 == 3
.id2
on the indicated line. Integers can't be indexed, thus the exception.So that's what caused your error. As I'm not sure exactly what your code is doing, I can't directly recommend a fix.
Upvotes: 1