Я новичок в анализе данных и ищу помощь. Я создаю свой алгоритм Knn с нуля, используя python. Думаю, у меня проблема с моими данными (обучение и тестирование). Я думаю, что я должен конвертировать в плавающие, но я не уверен на 100%. Я знаю, что мои функции работают, потому что я попробовал их с другим набором данных.
from scipy.io import arff
from io import StringIO
import scipy
import pandas as pd
import numpy as np
import math
data_train = scipy.io.arff.loadarff('train.arff')
train = pd.DataFrame(data_train[0])
train.head()
data_test = scipy.io.arff.loadarff('test1.arff')
print(data_test)
test = pd.DataFrame(data_test[0])
test.head()
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(train, test, test_size = 0.1, random_state=42)
print(X_train, X_test, y_train, y_test)
def distance(testpoint, trainpoint):
# distance between testpoint and trainpoint.
dist = np.sqrt(np.sum(np.power(testpoint-trainpoint, 2)))
return dis
def getNeighbors(X_train, y_train, X_test, k):
#For each point in X_test, calculate its distance from itself and each point in X_train
k_neighbors_with_labels = [] # this will be a list (for each test point) of list (contains the tuple (distance,label) of k nearest neighbors).
for testpoint in X_test:
distances_label = [] # this list carries distances between the testpoint and train point
for (trainpoint,y_train_label) in zip(X_train,y_train):
# calculate the distance and append it to a distances_label with the associated label.
distances_label.append((distance(testpoint, trainpoint), y_train_label))
k_neighbors_with_labels += [sorted(distances_label)[0:k]] # sort the distances and taken the first k neighbors
return k_neighbors_with_labels
ne = getNeighbors(X_train, y_train, X_test, k = 3)
print(ne)
TypeError Traceback (most recent call last)
<ipython-input-56-3b2868d1fd43> in <module>()
----> 1 ne = getNeighbors(X_train, y_train, X_test, k = 3)
2 print(ne)
<ipython-input-55-75b4da86d04e> in getNeighbors(X_train, y_train, X_test, k)
6 for (trainpoint,y_train_label) in zip(X_train,y_train):
7 # calculate the distance and append it to a distances_label with the associated label.
----> 8 distances_label.append((distance(testpoint, trainpoint), y_train_label))
9 k_neighbors_with_labels += [sorted(distances_label)[0:k]] # sort the distances and taken the first k neighbors
10 return k_neighbors_with_labels
<ipython-input-42-03d38977fec4> in distance(testpoint, trainpoint)
1 def distance(testpoint, trainpoint):
2 # distance between testpoint and trainpoint.
----> 3 dist = np.sqrt(np.sum(np.power(testpoint-trainpoint, 2)))
4 return distance
TypeError: unsupported operand type(s) for -: 'str' and 'str'