Skip to content

Instantly share code, notes, and snippets.

@magixer
Created October 20, 2017 16:51
Show Gist options
  • Save magixer/ae6048ab46e74fe3d0149fc8c8ce9259 to your computer and use it in GitHub Desktop.
Save magixer/ae6048ab46e74fe3d0149fc8c8ce9259 to your computer and use it in GitHub Desktop.
Beginners guide to their first machine learning program.
'''
The program predicts if a fruit is an apple or an orange from examining a
given data set of four fruits with two labels - apple or orange.
'''
import sklearn # Import SciKit Learn
from sklearn import tree # Import a classifier called 'DescisionTreeClassifier'
# Dataset of 4 fruits with 2 labels, apple and orange
'''
Feeding data in form [x, y] where,
x is weight of fruit in grams and, y is texture of the fruit - smooth or bumpy.
0 for smooth and 1 for bumpy.
'''
features = [[140, 0], [130, 0], [150, 1], [145, 1]] # Feeding features of the data in 'features'
labels = ['apple','apple','orange','orange'] # Feeding output labels of the data in 'labels'
classifier = tree.DecisionTreeClassifier() # Initializing the tree classifier
classifier.fit(features, labels) # Feeding the data to the classifier
# Ask your program to evaluate a fruit from the given data.
print(classifier.predict([[147, 1]])) # Predicting a New Fruit of 147 grams and bumpy texture.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment