I have a table. All signs are converted to numeric. The answers are in the state column. I implement the closest neighbors method. Here is my class
class KNearestNeighbors: def __init__(self, k): self.k = k def fit(self, pr_train, ans_train): self.pr_train = np.array(pr_train) self.ans_train = np.array(ans_train) def predict(self, pr_test): self.pr_test = np.array(pr_test) ans = np.zeros([len(pr_test), 1]) for i in range(len(self.pr_test)): dist = np.zeros([len(self.pr_train), 2]) for j in range(len(self.pr_train)): dist[j, 0] = round(sqrt(np.sum((self.pr_train[i] - pr_test[i])**2)), 5) dist[j, 1] = j dist = pd.DataFrame(dist, columns = ['dist', 'num']) kn = dist.nsmallest(self.k, 'dist', keep = 'all') ans[i] = stats.mode(kn)[0][0] I work in a flask. When calling
d = KNearestNeighbors(10) d.fit(train, train_ans) d.predict(test) produces: KeyError 0 in the line dist [j, 0] = round (sqrt (np.sum ((self.pr_train [i] - pr_test [i]) ** 2)), 5)