Skip to content

Instantly share code, notes, and snippets.

@emmagrimaldi
Created September 10, 2018 14:51
Show Gist options
  • Select an option

  • Save emmagrimaldi/0d813e5494a2d587572869ef584d6d4e to your computer and use it in GitHub Desktop.

Select an option

Save emmagrimaldi/0d813e5494a2d587572869ef584d6d4e to your computer and use it in GitHub Desktop.
from sklearn.tree import DecisionTreeClassifier
import pandas as pd
# create a matrix including the data
data = [[8,8,'dog'],[50,40,'dog'],[8,9,'cat'],[15,12,'dog'],[9,9.8,'cat']]
# generating a dataframe from the matrix
df = pd.DataFrame(data, columns = ['weight','height','label'])
# defining predictors
X = df[['weight','height']]
# definig the target variable and mapping it to 1 for dog and 0 for cat
y = df['label'].replace({'dog':1, 'cat':0})
# instantiating the model
tree = DecisionTreeClassifier()
# fitting the model
model = tree.fit(X,y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment