# Machine Learning Coding Tutorial 5. Build Our Own Classifier

In this tutorial, we are going to write machine learning program with our own classifier.

## 1. Steps

First, we need to create a classifier class ScrappyKNN.

Inside ScrappyKNN class, we will have fit and predict functions.

In the example, naive_predict is using a random guess approach to make predictions. closet_predict is using closet distance approach to make predictions.

## 2. Coding

Let’s head into Python for a programmatic example.

Create a python file classifier.py and write following code to program.

```"""
GoodTecher Machine Learning Coding Tutorial
http://72.44.43.28

Machine Learning Coding Tutorial 5. Build Our Own Classifier

The program demonstrate how to build our own classifier
"""
import random
from scipy.spatial import distance

def euc(a, b):
"""
Helper funciton to get distance between two points
"""
return distance.euclidean(a, b)

# class of our own Kneighbors classifier
class ScrappyKNN():
def fit(self, x_train, y_train):
self.x_train = x_train
self.y_train = y_train

def naive_predict(self, x_test):
predictions = []
for row in x_test:
label = random.choice(self.y_train)
predictions.append(label)
return predictions

def closet_predict(self, x_test):
predictions = []
for row in x_test:
label = self.closet(row)
predictions.append(label)
return predictions

def closet(self, row):
best_dist = euc(row, self.x_train)
best_index = 0
for i in range(1, len(self.x_train)):
dist = euc(row, self.x_train[i])
if dist < best_dist:
best_dist = dist
best_index = i
return self.y_train[best_index]

# import Iris dataset
from sklearn import datasets

# x is data, y is true label
x = iris.data
y = iris.target

# split half data as test data, half data as training data
from sklearn.cross_validation import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.5)

# use data to train Decision Tree classifier
from sklearn import tree

tree_classifier = tree.DecisionTreeClassifier()
tree_classifier.fit(x_train, y_train)

# use data to train kNeighbors classifier
from sklearn.neighbors import KNeighborsClassifier

kNeighbors_classifier = KNeighborsClassifier()
kNeighbors_classifier.fit(x_train, y_train)

# use data to train our own kNeighbors classifier
my_classifier = ScrappyKNN()
my_classifier.fit(x_train, y_train)
my_classifier.fit(x_train, y_train)

# predict
tree_predictions = tree_classifier.predict(x_test)
kNeighbors_predictions = kNeighbors_classifier.predict(x_test)
my_classifier_naive_predictions = my_classifier.naive_predict(x_test)
my_classifier_closet_predictions = my_classifier.closet_predict(x_test)

# compare true labels with prediction values to get accuracy score
from sklearn.metrics import accuracy_score

print("tree classifier accuracy: ", accuracy_score(y_test, tree_predictions))
print("k neighbors classifier accuracy: ", accuracy_score(y_test, kNeighbors_predictions))
print("naive classifier accuracy: ", accuracy_score(y_test, my_classifier_naive_predictions))
print("closet classifier accuracy: ", accuracy_score(y_test, my_classifier_closet_predictions))
```

`python classifier.py`
{"ft_lang_label":"__label__en","ft_lang_prob":0.6355467,"math_prob":0.9004266,"size":3148,"snap":"2021-31-2021-39","text_gpt3_token_len":674,"char_repetition_ratio":0.21119593,"word_repetition_ratio":0.04607046,"special_character_ratio":0.22363405,"punctuation_ratio":0.18825911,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9981039,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-08-05T14:00:38Z\",\"WARC-Record-ID\":\"<urn:uuid:64e4cec0-024d-40b7-98ce-f6a27afc1ca0>\",\"Content-Length\":\"36814\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e05f90e9-b1cb-4f66-8632-a4fa16bcffc1>\",\"WARC-Concurrent-To\":\"<urn:uuid:0a2f8c42-97da-4839-82e0-f2075aa3e302>\",\"WARC-IP-Address\":\"72.44.43.28\",\"WARC-Target-URI\":\"https://www.goodtecher.com/machine-learning-coding-tutorial-5-build-classifier/\",\"WARC-Payload-Digest\":\"sha1:PUEL5YM6SEBXKCVZUIQFTB4FDUERURGH\",\"WARC-Block-Digest\":\"sha1:RU7GI3MF6GQ4ZQIKBGUSD53CZ7QDUQT7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046155925.8_warc_CC-MAIN-20210805130514-20210805160514-00487.warc.gz\"}"}