# -*- coding: utf-8 -*- """Homework04.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1tbhifsWI51P_52PWIa07jEbXJs136BXr ## **Task 1 (5 points). Prepare the Fashion MNIST dataset** """ from tensorflow import keras fashion_mnist = keras.datasets.fashion_mnist (X_train_full , y_train_full ),(X_test,y_test) = fashion_mnist.load_data() X_train_full.shape X_valid, X_train=X_train_full[:5000] /255.0, X_train_full[5000:] /255.0 y_valid, y_train=y_train_full[:5000],y_train_full[5000:] X_test=X_test/255.0 X_valid.shape print("X_train.shape: ", X_train.shape) print("X_valid.shape: ", X_valid.shape) print("X_test.shape: ", X_test.shape) print("y_train.shape: ", y_train.shape) print("y_valid.shape: ", y_valid.shape) print("y_test.shape: ", y_test.shape) """## **Task 2 (5 points). Visualize the sample images in the training data**""" class_name=["T-shirt/top","Trouser","Pullover","Dress","Coat","Sandal","Shirt","Sneaker","Bag","Ankle boot"] class_name[y_train[0]] import matplotlib as mpl import matplotlib.pyplot as plt k=0 n_row = 3 n_col = 3 for i in range(1,n_row*n_col+1): plt.subplot(n_row, n_col,i) some_fashion=X_train[i] plt.imshow(some_fashion, cmap=mpl.cm.binary) plt.show() """## **Task 3 (5 points). Examine the frequency of classes in train, validation, and test set.**""" import matplotlib.pyplot as plt plt.figure(figsize=(12,4)) plt.subplot(1,3,1) plt.hist(y_train,bins=50) plt.show() plt.figure(figsize=(12,4)) plt.subplot(1,3,2) plt.hist(y_valid,bins=50) plt.show() plt.figure(figsize=(12,4)) plt.subplot(1,3,3) plt.hist(y_test,bins=50) plt.tight_layout plt.show() """## **Task 4: (45 points) Build several classification models** **Task 4.1.1. (2 points) According to official document of sklearn.neighbors.KNeighborsClassifier, give your own descriptions for the following four parameters about their purpose defined in this function: 'n_neighbors', 'metric', 'p', 'n_jobs'.** 1. n_neighbors-This represents the number of neighbors to be considered to predict. 2. metric-It is the disctance computation one where it calculates the distance between 2 points or neighbours. The default metric is minkowski. 3. n_jobs-This parameter is used to specify how many concurrent processes should be used for routines that are parallelized with joblib .For maximum all CPU's working when its -1 and none as default 4. p-This tells which distance function is used. **Task 4.1.2. (2 points) According to official document of sklearn.neighbors.KNeighborsClassifier, give your own descriptions for the following four methods about their purpose defined in this function: ‘fit(X,y)’, 'predict(X)', 'predict_proba', 'score(X,y)'.** 1. fit(X,y)-Fit the k-nearest neighbors classifier from the training dataset. 2. predict(X)-Predicts the class label for given data. 3. predict_proba-Gives probability estimates for the test data X. 4. score(X,y)-Gives the mean accuracy on given test data and labels. **Task 4.1.3: (2 points) Review the steps in Lab05 to build the KNN models.** """ img_shape = X_train.shape n_samples = img_shape[0] width = img_shape[1] height = img_shape[2] X_train_flatten = X_train.reshape(n_samples, width*height) print("x_train_flatten.shape: ",X_train_flatten.shape) img_shape1 = X_test.shape n_samples1 = img_shape1[0] width1 = img_shape1[1] height1 = img_shape1[2] X_test_flatten = X_test.reshape(n_samples1, width1*height1) print("x_test_flatten.shape: ",X_test_flatten.shape) img_shape2 = X_valid.shape n_samples2 = img_shape2[0] width2 = img_shape2[1] height2 = img_shape2[2] X_valid_flatten = X_valid.reshape(n_samples2, width2*height2) print("x_valid_flatten.shape: ",X_valid_flatten.shape) from sklearn.neighbors import KNeighborsClassifier KNN_classifier = KNeighborsClassifier(n_neighbors=3) KNN_classifier.fit(X_train_flatten, y_train) y_valid_pred = KNN_classifier.predict(X_valid_flatten) """**Task 4.1.4: (2 points) Practice how to save the trained model to disk. Write codes to re-load the model to answer the remaining questions.** **Task 4.1.5. (2 points) Based on the validation predictions from Task 4.1.3, organize the predicted classes and actual classes into Pandas dataframe as follows:** """ import pandas as pd prediction_summary = pd.DataFrame({'predicted_label':y_valid_pred, 'actual_label':y_valid}) prediction_summary from sklearn import metrics print("Accuracy: ", metrics.accuracy_score(y_valid, y_valid_pred)) c=0 for i in range(0,5000): if(y_valid_pred[i]==y_valid[i]): c=c+1 print(c) val_acc=4305/5000 print("Accuracy on validation set is ",val_acc) """**Task 4.1.7. (2 points) Calculate the per-class accuracy of the predictions.**""" frequency = {} for item in y_valid: if item in frequency: frequency[item] += 1 else: frequency[item] = 1 print(frequency) class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"] def get_per_class_accuracy(y, y_pred): actual_count={'0':0, '1':0, '2':0,'3':0,'4':0,'5':0,'6':0,'7':0,'8':0,'9': 0} pred_count={'0':0, '1':0, '2':0,'3':0,'4':0,'5':0,'6':0,'7':0,'8':0,'9': 0} for i in range(5000): actual_count[str(y[i])]+=1 if y_pred[i]==y[i]: pred_count[str(y[i])]+=1 t=[] for i in actual_count.keys(): t.append(pred_count[i]/actual_count[i]) for i in range(10): print(f'The validation accuracy of {class_names[i]} is : ', t[i], ' ------->', round(t[i]*100,2),"%" ) get_per_class_accuracy(y_valid, y_valid_pred) """**Task 4.1.8. (1 points) Let's visualize the classification confusion matrix to check the details of the validation predictions for each class.**""" import matplotlib.pyplot as plt from sklearn.metrics import ConfusionMatrixDisplay ConfusionMatrixDisplay.from_predictions(y_valid, y_valid_pred) plt.title("Classification Confusion matrix") plt.show() """**Task 4.1.9: (3 points) Try different K values, and select the best model that has highest validation accuracy. Make the predictions on test set (10000 images). And visualize the classification confusion matrix on the test set to report the details of predictions over every class (as Figure in Task 4.1.8).**""" import time knn = KNeighborsClassifier(n_neighbors=2) knn.fit(X_test_flatten,y_test) start = time.time() y_test_predicted = knn.predict(X_test_flatten) end = time.time() time_duration = end-start print("Program finishes in {} seconds:".format(time_duration)) ConfusionMatrixDisplay.from_predictions(y_test, y_test_predicted) plt.title("Classification Confusion matrix") plt.show() print("Overall Accuracy = ", metrics.accuracy_score(y_test, y_test_predicted)) knn = KNeighborsClassifier(n_neighbors=4) knn.fit(X_test_flatten,y_test) start = time.time() y_test_predicted = knn.predict(X_test_flatten) end = time.time() time_duration = end-start print("Program finishes in {} seconds:".format(time_duration)) ConfusionMatrixDisplay.from_predictions(y_test, y_test_predicted) plt.title("Classification Confusion matrix") plt.show() print("Overall Accuracy = ", metrics.accuracy_score(y_test, y_test_predicted)) knn = KNeighborsClassifier(n_neighbors=6) knn.fit(X_test_flatten,y_test) start = time.time() y_test_predicted = knn.predict(X_test_flatten) end = time.time() time_duration = end-start print("Program finishes in {} seconds:".format(time_duration)) ConfusionMatrixDisplay.from_predictions(y_test, y_test_predicted) plt.title("Classification Confusion matrix") plt.show() print("Overall Accuracy = ", metrics.accuracy_score(y_test, y_test_predicted)) knn = KNeighborsClassifier(n_neighbors=8) knn.fit(X_test_flatten,y_test) start = time.time() y_test_predicted = knn.predict(X_test_flatten) end = time.time() time_duration = end-start print("Program finishes in {} seconds:".format(time_duration)) ConfusionMatrixDisplay.from_predictions(y_test, y_test_predicted) plt.title("Classification Confusion matrix") plt.show() print("Overall Accuracy = ", metrics.accuracy_score(y_test, y_test_predicted)) knn = KNeighborsClassifier(n_neighbors=10) knn.fit(X_test_flatten,y_test) start = time.time() y_test_predicted = knn.predict(X_test_flatten) end = time.time() time_duration = end-start print("Program finishes in {} seconds:".format(time_duration)) ConfusionMatrixDisplay.from_predictions(y_test, y_test_predicted) plt.title("Classification Confusion matrix") plt.show() print("Overall Accuracy = ", metrics.accuracy_score(y_test, y_test_predicted)) """**Task 4.1.10: (2 points) Calculate the overall accuracy of the predictions over validation set and test set using the best model from Task 4.1.9.**""" knn = KNeighborsClassifier(n_neighbors=2) knn.fit(X_test_flatten,y_test) y_test_predicted = knn.predict(X_test_flatten) print("Overall Accuracy of valid set =",metrics.accuracy_score(y_valid,y_valid_pred)) print("Overall Accuracy of test set = ", metrics.accuracy_score(y_test, y_test_predicted)) """## **(2) (16 points) Task 4.2: Linear discriminant analysis:** **Task 4.2.1: (4 points) describe how is the bayes classification rule used for multi-class classification?** The bayes classification is done on bases on bayes theorm. The equation is given as P(class n/ x)=P(x /class n).P(class n)/P(x). We need to calculate P(class n/x) for all the classes and check which has the highest of all and the prediction goes into the class which has the highest. **Task 4.2.2: (5 points) Train the model on the training set (55000 images), and report the running time during the prediction process on the validation set (5000 images). Compare the running time with the prediction process of KNN In Task 4.1, which method's prediction is faster?** """ import numpy as np from sklearn.discriminant_analysis import LinearDiscriminantAnalysis clf = LinearDiscriminantAnalysis() clf.fit(X_train_flatten, y_train) start = time.time() predicted_labels = clf.predict(X_valid_flatten) end = time.time() time_duration = end-start print("Program finishes in {} seconds:".format(time_duration)) """The running time of KNN for validation set is 1.07 seconds whereas it takes 0.025 seconds for Linear Discriminant Analysys. Hence, Linear Discriminant Analysis runs faster for validation set. **Task 4.2.3: (2 points) Save the trained model to disk. Write codes to re-load the model to answer the remaining questions.** **Task 4.2.4: (3 points) Calculate the overall accuracy of the predictions over training set, validation set and test set.** """ y_test_pred = clf.predict(X_test_flatten) print("Accuracy of testing Set: ", metrics.accuracy_score(y_test, y_test_pred)) y_valid_pred = clf.predict(X_valid_flatten) print("Accuracy of validation Set: ", metrics.accuracy_score(y_valid, y_valid_pred)) """**Task 4.2.5: (2 points) Calculate the per-class accuracy of the predictions over the test set. For instance, among images of T-shirt, we need to calculate how many of T-shirt images are correctly predicted.**""" get_per_class_accuracy(y_test,y_test_pred) """# **(3) (12 points) Task 4.3: Quadratic discriminant analysis:** **Task 4.3.1: (5 points) Train the model on the training set (55000 images), and report the running time during the prediction process on the validation set (5000 images). Compare the running time with the prediction process of KNN, which method's prediction is faster?** """ from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis qda = QuadraticDiscriminantAnalysis() qda.fit(X_train_flatten, y_train) start = time.time() predicted_labels = qda.predict(X_valid_flatten) end = time.time() time_duration = end-start print("Program finishes in {} seconds:".format(time_duration)) """The running time of KNN for validation set is 1.07 seconds whereas it takes 1.61 seconds for Quadratic Discriminant Analysis. Hence, KNN runs faster for validation set. **Task 4.3.2: (2 points) Save the trained model to disk. Write codes to re-load the model to answer the remaining questions.** **Task 4.3.3: (3 points) Calculate the overall accuracy of the predictions over training set, validation set and test set.** """ y_train_pred = qda.predict(X_train_flatten) print("Accuracy of training Set: ", metrics.accuracy_score(y_train, y_train_pred)) y_test_pred = qda.predict(X_test_flatten) print("Accuracy of testing Set: ", metrics.accuracy_score(y_test, y_test_pred)) y_valid_pred = qda.predict(X_valid_flatten) print("Accuracy of validating Set: ", metrics.accuracy_score(y_valid, y_valid_pred)) """**Task 4.3.4: (2 points) Calculate the per-class accuracy of the predictions over the test set. For instance, among images of T-shirt, we need to calculate how many of T-shirt images are correctly predicted.**""" get_per_class_accuracy(y_test,y_test_pred) """# **(3) (12 points) Task 4.4: Gaussian Naive Bayes Classifier** **Task 4.4.1: (5 points) Train the model on the training set (55000 images), and report the running time during the prediction process on the validation set (5000 images). Compare the running time with the prediction process of KNN, which method's prediction is faster?** """ from sklearn.naive_bayes import GaussianNB gnb = GaussianNB() gnb.fit(X_train_flatten, y_train) start = time.time() predicted_labels = gnb.predict(X_valid_flatten) end = time.time() time_duration = end-start print("Program finishes in {} seconds:".format(time_duration)) """The running time of KNN for validation set is 1.07 seconds whereas it takes 0.104 seconds for Gaussian Naive Bayes. Hence, Gaussian Naive bayes model runs faster for validation set. **Task 4.4.2: (2 points) Save the trained model to disk. Write codes to re-load the model to answer the remaining questions.** **Task 4.4.3: (3 points) Calculate the overall accuracy of the predictions over training set, validation set and test set.** """ y_train_pred = gnb.predict(X_train_flatten) print("Accuracy of training Set: ", metrics.accuracy_score(y_train, y_train_pred)) y_test_pred = gnb.predict(X_test_flatten) print("Accuracy of testing Set: ", metrics.accuracy_score(y_test, y_test_pred)) y_valid_pred = gnb.predict(X_valid_flatten) print("Accuracy of validating Set: ", metrics.accuracy_score(y_valid, y_valid_pred)) """**Task 4.4.4: (2 points) Calculate the per-class accuracy of the predictions over the test set. For instance, among images of T-shirt, we need to calculate how many of T-shirt images are correctly predicted.**""" get_per_class_accuracy(y_test,y_test_pred) """# **Part II (20 points): Deploy the machine learning models on Gradio or huggingface**""" from gradio.outputs import Label import gradio as gr def caption(image,input_module1): class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"] image=image.reshape(1,28*28) if input_module1=="KNN": output1=KNN_classifier.predict(image)[0] predictions=KNN_classifier.predict_proba(image)[0] elif input_module1==("Linear discriminant analysis"): output1=clf.predict(image)[0] predictions=clf.predict_proba(image)[0] elif input_module1==("Quadratic discriminant analysis"): output1=qda.predict(image)[0] predictions=qda.predict_proba(image)[0] elif input_module1=="Naive Bayes classifier": output1=gnb.predict(image)[0] predictions=gnb.predict_proba(image)[0] #print(predictions) output2 = {} for i in range(len(predictions)): output2[class_names[i]] = predictions[i] return output1 ,output2 input_module = gr.inputs.Image(label = "Input Image",image_mode="L",shape=(28,28)) input_module1 = gr.inputs.Dropdown(choices=["KNN","Linear discriminant analysis", "Quadratic discriminant analysis","Naive Bayes classifier"], label = "Method") output1 = gr.outputs.Textbox(label = "Predicted Class") output2=gr.outputs.Label(label= "probability of class") gr.Interface(fn=caption, inputs=[input_module,input_module1], outputs=[output1,output2]).launch(debug=True)