import numpy as np import gradio as gr import pandas as pd def homework01_solution2(question, K, X1, X2): K = int(K) # Verify your solutions by code import numpy as np import pandas as pd X = np.array([[-1,1], [0,1], [0,2], [1,-1], [1,0], [1,2], [2,2], [2,3]]) if question == 'Question 2': y = np.array([1,0,1,1,0,0,1,0]) else: y = np.array([0,1,0,0,1,1,0,1]) train_data = pd.DataFrame(X, columns=['X1', 'X2']) train_data['Label (Y)'] = y from sklearn.neighbors import KNeighborsClassifier #(1) predict class for point (3,3) with K = 3 neigh = KNeighborsClassifier(n_neighbors=K) neigh.fit(X, y) pred = neigh.predict(X) train_data['Predicted Label ('+str(K)+'-NN)'] = pred predicted_label = neigh.predict(np.array([[X1, X2]]))[0] (nb_dist, nb_indice) = neigh.kneighbors(np.array([[X1, X2]]), K) import pandas as pd results = pd.DataFrame(columns=['Rank of closest neighbor', 'Features (X_1,X_2)', 'Label (Y)', 'Distance to query data']) for i in range(K): idx = nb_indice[0][i] fea = X[idx].tolist() fea = '({})'.format(', '.join(map(str, fea))) dist = nb_dist[0][i] label = y[idx] #print(idx, fea, dist, label) # Dictionary to append new_data = {'Rank of closest neighbor': i, 'Features (X_1,X_2)': fea, 'Label (Y)':label , 'Distance to query data': dist} tmp = pd.DataFrame(new_data, index=[0]) # Append dictionary to DataFrame #data = data.append(new_data, ignore_index=True) results = pd.concat([results, tmp], ignore_index=True) results = results.sort_values(by='Rank of closest neighbor') results['Distance to query data'] = results['Distance to query data'].round(3) results #Task 2: Based on predicted labels and actual labels provided in the above table, what's the number of correct predictions? # Initialize a counter for correct predictions correct_predictions = 0 for actual, predicted in zip(y, pred): if actual == predicted: correct_predictions += 1 #Task 3: What's the total number of predictions? total_predictions = len(y) #Task 4: What's the classification accuracy? acc = np.round(correct_predictions/total_predictions,3) #Task 5: What's the classification error? err = 1 - acc return train_data, results, predicted_label, correct_predictions, total_predictions, acc, err ### configure inputs set_question = gr.Number(value=7) set_question = gr.Dropdown( ["Question 2", "Question 4"], value="Question 2", label="Select question" ) set_K = gr.Number(value=7) set_X1 = gr.Number(value=1) set_X2 = gr.Number(value=2) ### configure outputs set_output_traindata = gr.Dataframe(type='pandas', label ='Train Dataset') set_output_q1a = gr.Dataframe(type='pandas', label ='Question 2: KNN-Classifier Search') set_output_q1b = gr.Textbox(label ='Question 2: KNN-Classifier Prediction') set_output_q4b = gr.Textbox(label ='Question 4b: Number of correct prediction') set_output_q4c = gr.Textbox(label ='Question 4c: Total number of prediction') set_output_q4d = gr.Textbox(label ='Question 4d: Classfication Accuracy') set_output_q4e = gr.Textbox(label ='Question 4e: Classfication Error') ### configure Gradio interface = gr.Interface(fn=homework01_solution2, inputs=[set_question, set_K, set_X1, set_X2], outputs=[set_output_traindata, set_output_q1a, set_output_q1b, set_output_q4b, set_output_q4c, set_output_q4d, set_output_q4e], title="CSCI4750/5750(hw01-PartI): Mathematics for KNN (Question 2 & 4: KNN-Classifier Search)", description= "Click examples below for a quick demo", theme = 'huggingface' ) interface.launch(debug=True)