def test_pred(md_clf): unseen = mpimg.imread("unseen_dig.png") unseen = interpol_im(unseen, plot_new_im=True) unseen = rescale_pixel(unseen) result = md_clf.predict(unseen.reshape(1, -1)) print("The prediction: ", result[0])
import warnings warnings.filterwarnings("ignore") from pattern_recog_func import interpol_im, pca_X, rescale_pixel, svm_train, pca_svm_pred ################################################ dig_data = load_digits() X = dig_data.data y = dig_data.target index = 15 dig_img = dig_data.images unseen = mpimg.imread('unseen_dig.png') flatten_unseen = interpol_im(unseen, plot_new_im = False) rescaled_unseen = rescale_pixel(X, flatten_unseen, index, plot_ref = True, plot_unseen = True) print('Mean pixel value of the rescaled image and the X[{}] image are: {}, {}, repectively'.format(index, np.mean(rescaled_unseen), np.mean(X[15]))) print('The rescaled unseen has all the pixel values as integers, flatten array shown as following:') print(rescaled_unseen) ###################### Trianing ########################## dig_data = load_digits() X = dig_data.data y = dig_data.target md_clf = svm_train(X[0:60], y[0:60], gamma = 0.001, C = 100) ####################### Validation #########################
tot_end = 80 for i in range(tot_start, tot_end): ans = y[i] pre = md_clf.predict(X[i].reshape(1, -1))[0] if ans != pre: mis += 1 plt.imshow(X[i].reshape((8, 8)), cmap='binary') plt.show() print("--------> index, actual digit, svm_prediction: {:d}, {:d}, {:d}".format(i, ans, pre)) if ans == pre: perc += 1. percentage = (perc/(tot_end-tot_start))*100. print("Total number of mis-identifications: {:d}".format(mis)) print("Success rate: {:f}".format(percentage)) ############################################################################ ############################## unseen_dig.png ############################## ############################################################################ unseen = mpimg.imread('unseen_dig.png') unseen_flat = interpol_im(unseen, plot_new_im=True) plt.imshow(X[15].reshape((8, 8)), cmap='binary') plt.show() unseen_scaled = rescale_pixel(X, unseen_flat, ind=15) unseen_pre = md_clf.predict(unseen_flat.reshape(1, -1)) unseen_scaled_pre = md_clf.predict(unseen_scaled.reshape(1, -1)) print("Predictions for unscaled and scaled unseen image: {:d}, {:d}".format(unseen_pre[0], unseen_scaled_pre[0]))
warnings.filterwarnings("ignore") from pattern_recog_func import interpol_im, pca_X, rescale_pixel, svm_train, pca_svm_pred ################################################ dig_data = load_digits() X = dig_data.data y = dig_data.target index = 15 dig_img = dig_data.images unseen = mpimg.imread('unseen_dig.png') flatten_unseen = interpol_im(unseen, plot_new_im=False) rescaled_unseen = rescale_pixel(X, flatten_unseen, index, plot_ref=True, plot_unseen=True) print( 'Mean pixel value of the rescaled image and the X[{}] image are: {}, {}, repectively' .format(index, np.mean(rescaled_unseen), np.mean(X[15]))) print( 'The rescaled unseen has all the pixel values as integers, flatten array shown as following:' ) print(rescaled_unseen) ###################### Trianing ########################## dig_data = load_digits() X = dig_data.data
# applying md_clf for the next 20 images for i in range(tot_start, tot_end): ans = y[i] pre = md_clf.predict(X[i].reshape(1, -1))[0] if ans != pre: mis += 1 plt.imshow(X[i].reshape((8, 8)), cmap='binary') plt.show() print( "--------> index, actual digit, svm_prediction: {:d}, {:d}, {:d}". format(i, ans, pre)) if ans == pre: perc += 1. percentage = (perc / (tot_end - tot_start)) print("Total number of mis-identifications: {:d}".format(mis)) print("Success rate: {:1.2f}".format(percentage)) # Testing the unseen_dig unseen = mpimg.imread('unseen_dig.png') unseen_flat = interpol_im(unseen, plot_new_im=True) # reshaping the image plt.imshow(X[15].reshape((8, 8)), cmap='binary') plt.show() unseen_scaled = rescale_pixel(X, unseen_flat, ind=15) # predictions for scaled and unscaled images unseen_pre = md_clf.predict(unseen_flat.reshape(1, -1)) unseen_scaled_pre = md_clf.predict(unseen_scaled.reshape(1, -1)) print("Predictions for unscaled and scaled unseen image: {:d}, {:d}".format( unseen_pre[0], unseen_scaled_pre[0]))