Skip to content

Instantly share code, notes, and snippets.

@alejandrofloresm
Created April 16, 2016 18:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save alejandrofloresm/8721d3af5e944c6462b0ece61e67e9b3 to your computer and use it in GitHub Desktop.
Save alejandrofloresm/8721d3af5e944c6462b0ece61e67e9b3 to your computer and use it in GitHub Desktop.
Hello World for Google
# Code example for:
# Hello World - Machine Learning Recipes #1 - Google Developers
# https://www.youtube.com/watch?v=cKxRvEZd3Mw
from sklearn import tree
# Bumpy = 0, Smooth = 1
features = [[140, 1], [130, 1], [150, 0], [170, 0]]
# Apple = 0, Orange = 1
labels = [0, 0, 1, 1]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)
print clf.predict([[160, 0]])
@sentientmachine
Copy link

Excellent work, Now change the above code to handle the following input:

import numpy as np
import urllib2
from sklearn.utils import shuffle
def load_heart():
    url = "https://archive.ics.uci.edu/ml/machine-learning-databases/statlog/heart/heart.dat"
    y = np.genfromtxt(urllib2.urlopen(url), delimiter=" ", dtype=None, usecols=[12])
    x = np.loadtxt(urllib2.urlopen(url), delimiter=" ", usecols=range(0, 12))

    features_names = np.array(
        ['age', 'sex', 'chest pain type(4 values)', 'resting blood pressure', 
        'serum cholestoral in mg/dl', 'fasting blood sugar > 120 mg/dl', 
        'resting electrocardiographic results  (values 0,1,2)', 
        'maximum heart rate achieved', 
        'exercise induced angina',
        'oldpeak = ST depression induced by exercise relative to rest', 
        'the slope of the peak exercise ST segment',
        'the slope of the peak exercise ST segment'
        ])
    x, y = shuffle(x, y, random_state=13)
    return x, y, features_names

x represents the feature training rows. y represents the labels. Do this, and I'll guide you again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment