Spaces:
Sleeping
Sleeping
File size: 3,974 Bytes
5a8f3c0 5d1f8af 5a8f3c0 a759893 5a8f3c0 7a4164c 09ec2b8 a759893 5a8f3c0 3178f28 9524046 5a8f3c0 1d3380c 5a8f3c0 3178f28 b2b427f 3178f28 5a8f3c0 3178f28 5a8f3c0 a759893 e9b933c 5a8f3c0 e9b933c 5a8f3c0 03ba39e 3178f28 5a8f3c0 5d1f8af a759893 3178f28 5a8f3c0 3178f28 5a8f3c0 a1dd0fb 5a8f3c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
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) |