Decision Trees
While you can use the Decision Trees algorithm for classification, just like KNN, it goes about the task of classification very differently. While KNN finds the most similar data objects for classification, Decision Trees first summarizes the data using a tree-like structure and then uses the structure to perform the classification.
Let's learn about Decision Trees using an example.
Example of using Decision Trees for classification
We will use DecisionTreeClassifier
from sklearn.tree
to apply the Decision Trees algorithm to applicant_df
. The code needed to use Decision Trees is almost identical to that of KNN. Let's see the code first, and then I will draw your attention to their similarities and differences. Here it is:
from sklearn.tree import DecisionTreeClassifier predictors = ['income','score'] target = 'default' Xs = applicant_df[predictors].drop(index=[20]) y= applicant_df[target].drop(index=[20]) classTree...