- class kNN():
- def __init__(self):
- pass
- def fit(self, X, y):
- self.data = X
- self.targets = y
- def euclidean_distance(self, X):
- '''
- Computes the euclidean distance between the training data and
- a new input example or matrix of input examples X
- '''
- # input: single data point
- if X.ndim == 1:
- l2 = np.sqrt(np.sum((self.data - X)**2, axis=1))
- # input: matrix of data points
- if X.ndim == 2:
- n_samples, _ = X.shape
- l2 = [np.sqrt(np.sum((self.data - X[i])**2, axis=1)) for i in range(n_samples)]
- return np.array(l2)
- defpredict(self, X, k=1):
- '''Predicts the classification for an input example or matrix of input examples X'''
- # step 1: compute distance between input and training data
- dists = self.euclidean_distance(X)
- # step 2: find the k nearest neighbors and their classifications
- if X.ndim == 1:
- if k == 1:
- nn = np.argmin(dists)
- return self.targets[nn]
- else:
- knn = np.argsort(dists)[:k]
- y_knn = self.targets[knn]
- max_vote = max(y_knn, key=list(y_knn).count)
- return max_vote
- if X.ndim == 2:
- knn = np.argsort(dists)[:, :k]
- y_knn = self.targets[knn]
- if k == 1:
- return y_knn.T
- else:
- n_samples, _ = X.shape
- max_votes = [max(y_knn[i], key=list(y_knn[i]).count) for i in range(n_samples)]
- return max_votes
来源: http://www.92to.com/bangong/2018/04-05/33520924.html