How Do You Write the Equivalent of a “hello world” Program with Machine Learning in Python?

Problem scenario
You want to be able to say you ran a machine learning program. You know some Python basic. What do you do to write a very simple machine learning program?

Solution
Prerequisite
This assumes that pip has been installed. If you need assistance see this posting.

Procedures

1. Run this command:
sudo pip install numpy scipy scikit-learn

2. Create a file like this called ml.py:

#This program was adapted from Google Developers here: https://www.youtube.com/watch?v=cKxRvEZd3Mw
from sklearn import tree
features = [[140, 1], [130, 1], [150, 0], [170, 0]]
labels = [0, 0, 1, 1]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)
print ("This program uses a decision tree that is part of the sklearn package")
print clf.predict([[140, 1]])
print ("Above is the pair with the 140 value and below is the pair with the 130 value")
print clf.predict([[130, 1]])
print clf.predict([[150, 0]])
print ("Above is the pair with the 150 value and below is the pair with the 170 value")
print clf.predict([[170, 0]])

3. Run it like this: python ml.py

Leave a comment

Your email address will not be published. Required fields are marked *