def test_leave_one_out(self): correct = 0 k = 3 model = kNN.train(xs, ys, k) predictions = [1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1] for i in range(len(predictions)): model = kNN.train(xs[:i] + xs[i + 1 :], ys[:i] + ys[i + 1 :], k) prediction = kNN.classify(model, xs[i]) self.assertEqual(prediction, predictions[i]) if prediction == ys[i]: correct += 1 self.assertEqual(correct, 13)
def test_leave_one_out(self): correct = 0 k = 3 model = kNN.train(xs, ys, k) predictions = [1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1] for i in range(len(predictions)): model = kNN.train(xs[:i] + xs[i + 1:], ys[:i] + ys[i + 1:], k) prediction = kNN.classify(model, xs[i]) self.assertEqual(prediction, predictions[i]) if prediction == ys[i]: correct += 1 self.assertEqual(correct, 13)
def test_classify(self): k = 3 model = kNN.train(xs, ys, k) result = kNN.classify(model, [6, -173.143442352]) self.assertEqual(result, 1) result = kNN.classify(model, [309, -271.005880394]) self.assertEqual(result, 0)
def train_model(self, training_ffts, training_labels): """ Takes a set of training examples + corresponding true labels and returns a trained SOM and kNN. """ som_size = self.params['som_size'] som_iterations = self.params['som_iterations'] som_learning_rate = self.params['som_learning_rate'] knn_k = self.params['knn_k'] som = SelfOrganizingMap(size=som_size, n_iterations=som_iterations, learning_rate=som_learning_rate) column_vectors = np.hstack(training_ffts).T np.random.shuffle(column_vectors) som.fit(column_vectors) training_sequences = [ self.sound_fft_to_string(sound_fft, som) for sound_fft in training_ffts ] knn_model = kNN.train(training_sequences, training_labels, knn_k) return som, knn_model
def test_calculate_model(self): k = 3 model = kNN.train(xs, ys, k) self.assertEqual(model.classes, set([0, 1])) n = len(xs) for i in range(n): self.assertAlmostEqual(model.xs[i, 0], xs[i][0], 4) self.assertAlmostEqual(model.xs[i, 1], xs[i][1], 4) self.assertEqual(model.ys[i], ys[i]) self.assertEqual(model.k, k)
def test_calculate_model(self): k = 3 model = kNN.train(xs, ys, k) self.assertEqual(model.classes, set([0, 1])) n = len(xs) for i in range(n): self.assertAlmostEqual(model.xs[i, 0], xs[i][0], places=4) self.assertAlmostEqual(model.xs[i, 1], xs[i][1], places=4) self.assertEqual(model.ys[i], ys[i]) self.assertEqual(model.k, k)
def test_model_accuracy(self): correct = 0 k = 3 model = kNN.train(xs, ys, k) predictions = [1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0] for i in range(len(predictions)): prediction = kNN.classify(model, xs[i]) self.assertEqual(prediction, predictions[i]) if prediction == ys[i]: correct += 1 self.assertEqual(correct, 15)
def knn(self): xs = [[-53.0, -200.78], [117.0, -267.14], [57.0, -163.47], [16.0, -190.3], [11.0, -220.94], [85.0, -193.94], [16.0, -182.71], [15.0, -180.41], [-26.0, -181.73], [58.0, -259.87], [126.0, -414.53], [191.0, -249.57], [113.0, -265.28], [145.0, -312.99], [154.0, -213.83], [147.0, -380.85], [93.0, -291.13]] ys = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0] k = 3 model = kNN.train(xs, ys, k) return model
def test_calculate_probability(self): k = 3 model = kNN.train(xs, ys, k) weights = kNN.calculate(model, [6, -173.143442352]) self.assertAlmostEqual(weights[0], 0.0, 6) self.assertAlmostEqual(weights[1], 3.0, 6) weights = kNN.calculate(model, [309, -271.005880394]) self.assertAlmostEqual(weights[0], 3.0, 6) self.assertAlmostEqual(weights[1], 0.0, 6) weights = kNN.calculate(model, [117, -267.13999999999999]) self.assertAlmostEqual(weights[0], 2.0, 6) self.assertAlmostEqual(weights[1], 1.0, 6)
def test_calculate_probability(self): k = 3 model = kNN.train(xs, ys, k) weights = kNN.calculate(model, [6, -173.143442352]) self.assertAlmostEqual(weights[0], 0.0, places=6) self.assertAlmostEqual(weights[1], 3.0, places=6) weights = kNN.calculate(model, [309, -271.005880394]) self.assertAlmostEqual(weights[0], 3.0, places=6) self.assertAlmostEqual(weights[1], 0.0, places=6) weights = kNN.calculate(model, [117, -267.13999999999999]) self.assertAlmostEqual(weights[0], 2.0, places=6) self.assertAlmostEqual(weights[1], 1.0, places=6)
def train_model(self, training_ffts, training_labels): """ Takes a set of training examples + corresponding true labels and returns a trained SOM and kNN. """ som_size = self.params['som_size'] som_iterations = self.params['som_iterations'] som_learning_rate = self.params['som_learning_rate'] knn_k = self.params['knn_k'] som = SelfOrganizingMap(size=som_size, n_iterations=som_iterations, learning_rate=som_learning_rate) column_vectors = np.hstack(training_ffts).T np.random.shuffle(column_vectors) som.fit(column_vectors) training_sequences = [self.sound_fft_to_string(sound_fft, som) for sound_fft in training_ffts] knn_model = kNN.train(training_sequences, training_labels, knn_k) return som, knn_model
def knn(self): xs = [[-53.0, -200.78], [117.0, -267.14], [57.0, -163.47], [16.0, -190.3], [11.0, -220.94], [85.0, -193.94], [16.0, -182.71], [15.0, -180.41], [-26.0, -181.73], [58.0, -259.87], [126.0, -414.53], [191.0, -249.57], [113.0, -265.28], [145.0, -312.99], [154.0, -213.83], [147.0, -380.85], [93.0, -291.13]] ys =[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0] k=3 model = kNN.train(xs,ys,k) return model
def knn(self): xs = [[1.0, 1.0], [1.0, 2.0], [1.0, 3.0], [1.0, 4.0]] ys = [1, 1, 0, 0] k = 3 model = kNN.train(xs, ys, k) return model
features = tfidf.toarray() #将tf-idf矩阵抽取出来,元素a[i][j]表示j词在i类文本中的tf-idf权重 print(features.shape) target = [c for (d, c) in documents] train_set1 = features[:1500, :] target_train = target[:1500] test_set1 = features[1500:, :] target_test = target[1500:] from sklearn.svm import SVC svclf = SVC(kernel='linear', probability=True) svclf.fit(train_set1, target_train) pred_svc = svclf.predict(test_set1) print('SVM=', sum(pred_svc == target_test) / len(target_test)) from Bio import kNN from scipy import spatial model = kNN.train(train_set1, target_train, 7) dd = [ kNN.classify(model, t, distance_fn=spatial.distance.cosine) for t in test_set1 ] print('KNN_cos=', sum(np.array(dd) == np.array(target_test)) / len(target_test)) from sklearn.neighbors import KNeighborsClassifier knnclf = KNeighborsClassifier(n_neighbors=7) #default with k=5 knnclf.fit(train_set1, target_train) pred_knn = knnclf.predict(test_set1) print('KNN_eu=', sum(pred_knn == target_test) / len(target_test))
def knn(self): xs = [[1.0, 1.0], [1.0, 2.0], [1.0, 3.0], [1.0, 4.0]] ys =[1, 1, 0, 0] k=3 model = kNN.train(xs,ys,k) return model