Every line of 'sklearn knn regressor' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
24 def trainKNN(trainData,trainLabel): 25 knn = neighbors.KNeighborsClassifier(n_neighbors=3,algorithm='ball_tree') 26 #print(type(trainData)) 27 return knn.fit(trainData,trainLabel)
169 def predict_knn(X_train, X_test, y_train, y_test): 170 clf=knn(n_neighbors=3) 171 print("knn started") 172 clf.fit(X_train,y_train) 173 y_pred=clf.predict(X_test) 174 calc_accuracy("K nearest neighbours",y_test,y_pred) 175 np.savetxt('submission_surf_knn.csv', np.c_[range(1,len(y_test)+1),y_pred,y_test], delimiter=',', header = 'ImageId,Label,TrueLabel', comments = '', fmt='%d')
221 def __init__(self, X, Y): 222 ''' 223 :param X: 224 :param Y: 225 ''' 226 self.model = KNeighborsClassifier(n_neighbors=3) 227 self.X = X 228 self.Y = Y
35 def skl_knn(self, k): 36 """k: number of neighbors to use in classification 37 test_data: the data/targets used to test the classifier 38 stored_data: the data/targets used to classify the test_data 39 """ 40 fifty_x, fifty_y = self.mk_dataset(50000) 41 test_img = [self.data[i] for i in self.indx[60000:70000]] 42 test_img1 = np.array(test_img) 43 test_target = [self.target[i] for i in self.indx[60000:70000]] 44 test_target1 = np.array(test_target) 45 self.classifier.fit(fifty_x, fifty_y) 46 47 y_pred = self.classifier.predict(test_img1) 48 pickle.dump(self.classifier, open('knn.sav', 'wb')) 49 print(classification_report(test_target1, y_pred)) 50 print("KNN Classifier model saved as knn.sav!")
67 def predict(self, X=None): 68 """ 69 Predicts the labels for the given data. Model needs to be already fitted. 70 71 X : 2D-Array or Matrix, default=None 72 73 The dataset to be used. If None, the training set will be used. In this case, the prediction will be made 74 using Leave One Out (that is, the sample to predict will be taken away from the training set). 75 76 Returns 77 ------- 78 79 y : 1D-Array 80 81 The vector with the label predictions. 82 """ 83 if X is None: 84 return self.loo_pred(self.trX) 85 else: 86 X = self.dml.transform(X) 87 88 return self.knn.predict(X)