+ - 0:00:00
Notes for current slide
Notes for next slide

Classification in Python: the k-NN solution

[email protected]

Data Science: Techniques and Applications (DSTA)

January 29th, 2020

1 / 9

Plan of the lab experience

  • Introduction (5 min.)

  • The k-NN algorithm (5 min.)

  • Splitting a test dataset in pure Python (10 min.)

  • Code Euclidean norm in pure Python (5 min.)

  • Develop a classifier for Iris (10 min.)

  • Solution (5 min.)

2 / 9

Development of a k-NN classifer in Python

  • Split the data-set into training(75%) and test(25%)

  • Run the k-NN classification algorithm

  • Plot output from the classifier

  • Find the basic machinery from iris_k-NN_base.py

3 / 9

Task 1: Data-set split

def train_and_test_split(N, test_fraction=None):
#Hint:use np.random function to generate an array of True and False
inputs=np.array([[1, 2], [3, 4],[5,6]])
train_part=[True, False, True]
train_inputs=inputs[train_part,:]
print(train_inputs)
4 / 9

KNN classification algorithm

KNN algorithm


Display from Tan et al., 2014.

5 / 9

Task 2: Find distance between two vectors

def euclidean_distance(v1, v2, n):
#distance-- a scalar value of euclidian distance between two vectors
distance = 0
%%TODO: write codes to find distances between two vectors
return distance
6 / 9

Task 3: Get prediction

def classify(neighbors, train_targets):
predicted_class=2.0
# %% Write codes to classify
#return the majority class
return predicted_class
7 / 9

Output

Actual classification Predicted classification
scatter_plot_actual_classification scatter_plot_predicted_classification
8 / 9

To complete...

Use the basic entropy module seen in class to compute the quality of your solution

File entropy.py is for download from the class repository

import entropy as ent
# ...
myeta = ent.H(myclass, target)
9 / 9

Plan of the lab experience

  • Introduction (5 min.)

  • The k-NN algorithm (5 min.)

  • Splitting a test dataset in pure Python (10 min.)

  • Code Euclidean norm in pure Python (5 min.)

  • Develop a classifier for Iris (10 min.)

  • Solution (5 min.)

2 / 9
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow