Example: Training your Model


We've decided that we're making a decision tree. Let's use sklearn to do this!

Let's first import sklearn:

from sklearn.tree import DecisionTreeClassifier

Awesome! Let's now load our data into training and testing data:

creditScoreAndIncomeTrain = [
[700,80000],
[800,50000],
[550,40000],
[570,70000],
[500,75000]
]

approvedTrain = [
["Yes"],
["Yes"],
["No"],
["No"],
["No"]
]

creditScoreAndIncomeTest = [
[840,90000],
[450,40000]
]

approvedTest = [
"Yes",
"No"
]

Note that normally we would have way more data than this (ideally loaded from a file or database), but this is enough for the tutorial! :)

Now let's build our decision tree. Sklearn does all of the heavy lifting for us. If we wanted to write custom code to build the tree we could look at an existing algorithm such as C4.5.

tree = DecisionTreeClassifier().fit(creditScoreAndIncomeTrain, approvedTrain)

Awesome! We have now built our tree; we just need to test it! Continue to the next page to see how to test it.

results matching ""

    No results matching ""