Example #1
0
    def test_textDecisionTree(self):
        iris = load_iris()
        X = iris.data
        y = iris.target
        X_train, _, y_train, _ = train_test_split(X, y, random_state=0)

        estimator = DecisionTreeClassifier(max_leaf_nodes=3, random_state=0)
        estimator.fit(X_train, y_train)

        representation = textDecisionTree(estimator)
        # print(representation)
        self.assertIn('node=0 test node', representation)
        self.assertIn('node=1 leaf node', representation)
        self.assertIn('node=2 test node', representation)
        self.assertIn('node=3 leaf node', representation)
        self.assertIn('node=4 leaf node', representation)
predictors = ['borrower_score', 'payment_inc_ratio']
outcome = 'outcome'

X = loan3000[predictors]
y = loan3000[outcome]

loan_tree = DecisionTreeClassifier(random_state=1,
                                   criterion='entropy',
                                   min_impurity_decrease=0.003)
loan_tree.fit(X, y)
plotDecisionTree(loan_tree,
                 feature_names=predictors,
                 class_names=loan_tree.classes_)

print(textDecisionTree(loan_tree))

### The Recursive Partitioning Algorithm

fig, ax = plt.subplots(figsize=(6, 4))

loan3000.loc[loan3000.outcome == 'paid off'].plot(x='borrower_score',
                                                  y='payment_inc_ratio',
                                                  style='.',
                                                  markerfacecolor='none',
                                                  markeredgecolor='C1',
                                                  ax=ax)
loan3000.loc[loan3000.outcome == 'default'].plot(x='borrower_score',
                                                 y='payment_inc_ratio',
                                                 style='o',
                                                 markerfacecolor='none',