# --- # jupyter: # jupytext_format_version: '1.2' # kernelspec: # display_name: Python 3 # language: python # name: python3 # language_info: # codemirror_mode: # name: ipython # version: 3 # file_extension: .py # mimetype: text/x-python # name: python # nbconvert_exporter: python # pygments_lexer: ipython3 # version: 3.5.2 # --- # # KNN Classification # # # # + % matplotlib inline import matplotlib.pyplot as plt from sklearn import datasets, neighbors, linear_model # load data digits = datasets.load_digits() X_digits = digits.data y_digits = digits.target print("Feature dimensions: ", X_digits.shape) print("Label dimensions: ", y_digits.shape) # + # plot sample images nplot = 10 fig, axes = plt.subplots(nrows=1, ncols=nplot) for i in range(nplot): img = X_digits[i].reshape(8, 8) axes[i].imshow(img) axes[i].set_title(y_digits[i]) # + # split train / test data n_samples = len(X_digits) n_train = int(0.4 * n_samples) X_train = X_digits[:n_train] y_train = y_digits[:n_train] X_test = X_digits[n_train:] y_test = y_digits[n_train:] # + # do KNN classification knn = neighbors.KNeighborsClassifier() logistic = linear_model.LogisticRegression() print('KNN score: %f' % knn.fit(X_train, y_train).score(X_test, y_test)) print('LogisticRegression score: %f' % logistic.fit(X_train, y_train).score(X_test, y_test)) # - # ## References # * [Digits Classification Exercise](http://scikit-learn.org/stable/auto_examples/exercises/plot_digits_classification_exercise.html) #