Machine Learning – My own First Classifier

I’m excited to create my own Machine Learning Classifier, it produce above 90 percent accuracy. The more training and test data, the better prediction (accuracy) of results.

Resources I’m using to study Machine Learning.

  • Video tutorial hosted by Josh Gordon. I highly recommend that you watch all the videos from the beginning if you’re interested to know more about Machine Learning – https://www.youtube.com/watch?v=AoeEHqVSNOw
  • Python for programming language.
  • SciKit Learn and Tensor Flow for Machine Learning framework.

Here’s the complete code for my first Classifier.

# Writing my First Classifier
# Tutorial - https://www.youtube.com/watch?v=AoeEHqVSNOw

from scipy.spatial import distance
#Euclidean Distance
def euc(a,b):
  return distance.euclidean(a,b)

#import random
class MyOwnClassifier():
  def fit(self, X_train, y_train): # Using TRAINING data.
    self.X_train = X_train
    self.y_train = y_train
  
  def predict(self, X_test): # using TEST data.
    predictions = []
    for row in X_test:
#      label = random.choice(self.y_train)
      label = self.closest(row)
      predictions.append(label)
    return predictions
  
  def closest(self, row):
    best_dist = euc(row, self.X_train[0])
    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]

from sklearn import datasets
iris = datasets.load_iris()

X = iris.data # input data, feature
y = iris.target # output label

from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .5) 

#My Own Classifier.
my_classifier = MyOwnClassifier()

#train our classifier using TRAINING data
my_classifier.fit(X_train, y_train)

#use predict method, use to classify TEST data.
predictions = my_classifier.predict(X_test)
# print (predictions)

#test the accuracy, compare the predicted label to the true label, tally the score.
from sklearn.metrics import accuracy_score
print ("Accuracy %:", accuracy_score(y_test, predictions))

I’m still finishing my youtube training and hopefully to expand my knowledge.

Next week, I will also try the Amazon SageMaker and that’s another post to share.

Image by xresch pixabay

 

Support @QUE.COM

Founder, QUE.COM Internet Media. | Founder, Yehey.com a Shout for Joy! | MAJ.COM Management of Assets and Joint Ventures. More at KING.NET Ideas to Life.

Leave a Reply