def kNN_model(): # Init kNN's weights X, Y = [], [] cset = [str(i) for i in range(10)] for c in cset[:]: image_paths = glob.glob('./train/{}/*.*'.format(c)) # cells_list = [ path2cells(p) for p in image_paths ] for p in image_paths: x = path2cells(p) X.append(x) Y.append(int(c)) X = np.array(X) Y = np.array(Y) # np.save('X.npy', X) # np.save('Y.npy', Y) model = kNNClassifier(1) model.fit(X, Y) # %% # Test kNN model X_test = [] Y_test = [] for root_dir, sub_dir, files in os.walk('./test'): for i, f in enumerate(files): fpath = os.path.join(root_dir, f) image = cv2.imread(fpath, cv2.IMREAD_GRAYSCALE) image = 255 -image cells = calc_cells(image,thresh=10 ) X_test.append(cells) Y_test.append(int(root_dir[-1].lower())) X_test = np.array(X_test) Y_test = np.array(Y_test) pred = model.predict(X_test) acc = np.sum( Y_test == pred ) / Y_test.shape[0] print(f"Accuracy: {acc} on {n_test} images")
# -*- coding: utf-8 -*- """ @Project = KNN @File = main.py @Author = FengQi @Mail = [email protected] @Time = 2019/11/7 10:21 上午 @Software: PyCharm """ from kNN import kNNClassifier import numpy as np raw_data_X = [[3.393533211, 2.331273381], [3.110073483, 1.781539638], [1.343853454, 3.368312451], [3.582294121, 4.679917921], [2.280362211, 2.866990212], [7.423436752, 4.685324231], [5.745231231, 3.532131321], [9.172112222, 2.511113104], [7.927841231, 3.421455345], [7.939831414, 0.791631213]] raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] # 设置训练组 X_train = np.array(raw_data_X) y_train = np.array(raw_data_y) x = np.array([[8.90933607318, 3.365731514]]) knn_clf = kNNClassifier(k=6) knn_clf.fit(X_train, y_train) X_predict = x.reshape(1, -1) y_predict = knn_clf.predict(X_predict) print(y_predict)
#导入iris数据 iris = datasets.load_iris() X = iris.data y = iris.target standardScaler = StandardScaler() standardScaler.fit #将数据集拆分为train、test数据 X_train, X_test, y_train, y_test = train_test_split(X, y, 0.2) #数据的归一化(训练数据集、测试数据集的归一化) standardScaler = StandardScaler() standardScaler.fit(X_train) X_train = standardScaler.transform(X_train) X_test = standardScaler.transform(X_test) print(X_test) #使用上述数据进行训练和预测 kcf = kNNClassifier(3) kcf.fit(X_train, y_train) y_predict = kcf.predict(X_test) #计算预测准确度 accuracy = accuracy_score(y_test, y_predict) print('Origin: ', y_test) print('Predict:', y_predict) print('Accuracy:', accuracy) print(kcf.score(X_test, y_test))
def readCommand( argv ): "Processes the command used to run from the command line." from optparse import OptionParser parser = OptionParser(USAGE_STRING) parser.add_option('-c', '--classifier', help=default('The type of classifier'), choices=['naiveBayes', 'perceptron', 'kNN'], default='naiveBayes') parser.add_option('-d', '--data', help=default('Dataset to use'), choices=['digits', 'faces'], default='digits') parser.add_option('-t', '--training', help=default('The size of the training set'), default=100, type="int") parser.add_option('-w', '--weights', help=default('Whether to print weights'), default=False, action="store_true") parser.add_option('-k', '--neighbors', help=default("Numbers of neighbors in k-Nearest Neighbors"), type="int", default=3) parser.add_option('-i', '--iterations', help=default("Maximum iterations to run training"), default=3, type="int") parser.add_option('-s', '--test', help=default("Amount of test data to use"), default=TEST_SET_SIZE, type="int") options, otherjunk = parser.parse_args(argv) if len(otherjunk) != 0: raise Exception('Command line input not understood: ' + str(otherjunk)) args = {} # Set up variables according to the command line input. print "Doing classification" print "--------------------" print "data:\t\t" + options.data print "classifier:\t\t" + options.classifier print "training set size:\t" + str(options.training) if(options.data=="digits"): printImage = ImagePrinter(DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT).printImage featureFunction = basicFeatureExtractorDigit elif(options.data=="faces"): printImage = ImagePrinter(FACE_DATUM_WIDTH, FACE_DATUM_HEIGHT).printImage featureFunction = basicFeatureExtractorFace else: print "Unknown dataset", options.data print USAGE_STRING sys.exit(2) if(options.data=="digits"): legalLabels = range(10) else: legalLabels = range(2) if options.training <= 0: print "Training set size should be a positive integer (you provided: %d)" % options.training print USAGE_STRING sys.exit(2) if options.neighbors <= 0: print "Neighbors for kNN should be a positive integer (you provided: %d)" % options.neighbors print USAGE_STRING sys.exit(2) if(options.classifier == "naiveBayes"): classifier = naiveBayes.NaiveBayesClassifier(legalLabels) elif(options.classifier == "perceptron"): classifier = perceptron.PerceptronClassifier(legalLabels,options.iterations) elif(options.classifier == "kNN"): classifier = kNN.kNNClassifier(legalLabels,options.neighbors) else: print "Unknown classifier:", options.classifier print USAGE_STRING sys.exit(2) args['classifier'] = classifier args['featureFunction'] = featureFunction args['printImage'] = printImage return args, options
print(targets_train) print("\n\nPreparing classifier...") classifier = GaussianNB() print("Training classifier...") model = classifier.fit(data_train, targets_train) print( "\nRunning test --- sending 30% data for testing and predicting targets..." ) targets_predicted = model.predict(data_test) print("\nComparing targets tested to the targets trained...") print("Accuracy Score:") percent = accuracy_score(targets_test, targets_predicted) print("{:.0%}".format(percent)) print("\n\nPreparing k-Nearest Neighbors classifier...") k = int(input("Enter the number of neighbors to check (k):")) classifier = kNNClassifier(k) print("Training classifier...") model = classifier.fit(data_train, targets_train) print( "\nRunning test --- sending 30% data for testing and predicting targets..." ) targets_predicted = model.predict(data_train, targets_train, data_test, k) print("\nComparing targets tested to the targets trained...") print("Accuracy Score:") # percent = accuracy_score(targets_test, targets_predicted) # print("{:.0%}".format(percent)) print(targets_predicted)