Paul's Python CoursePPC

Project 6 of 10, for Paul

Loan Approval Tree

Approve or decline a loan from seven answers on the form, for 1,000 real applicants: a decision tree grown from scratch and read as questions, then a random forest from scratch and gradient boosting, judged by what their mistakes cost.

About 3 hours, in 19 steps.

Sign up free to open itLog in

Act 6 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • PaulIf a lender says no, they should be able to explain it with actual questions written down, rather than just a score.
  • PaulThere's a German bank's thousand applicants, public since the nineties, with seven answers each and whether they repaid.
  • RiyaI've started growing one already. How deep can it go?
  • PaulAs deep as the questions stay true, and then you stop.

loans.csv holds 1,000 real past applicants: their checking account (0 overdrawn, 1 under 200 DM, 2 200 DM or more, 3 none), the loan's length in months, their credit history (0 to 4, worse as it rises), the amount, their savings (0 to 4) and years in work (0 to 4), their age, and whether they repaid.

In loans.py:

  1. Grow a tree of yes-or-no questions from scratch, choosing each question by how well it sorts the applicants. Lock a fifth of the applicants away for testing, choose the depth on validation applicants, print the chosen tree as questions, and test it once against approving everyone.
  2. Grow a forest of 50 different trees from scratch: each tree learns from its own random draw of applicants, and each question looks at three columns chosen at random. Approve when half the trees say so.
  3. Boost small trees with scikit-learn, each fixing what the last got wrong.
  4. Compare them all on the test applicants by accuracy and by the bank's cost: 5 for a bad loan approved, 1 for a good one declined. Then the forest again, approving only when five trees in six agree.
What it prints
depth  training  validation
    1     0.718       0.700
    2     0.747       0.685
    3     0.778       0.735
    4     0.793       0.735
    5     0.808       0.715
    6     0.838       0.675
    8     0.888       0.680
Chosen depth: 3
checking <= 1.5?
  yes: duration_months <= 22.5?
    yes: history <= 1.5?
      yes: decline
      no: approve
    no: savings <= 0.5?
      yes: decline
      no: approve
  no: amount <= 10924.5?
    yes: employment <= 2.5?
      yes: approve
      no: approve
    no: amount <= 12714.5?
      yes: decline
      no: approve
                         accuracy   cost
Approving everyone          0.645  1.775
One tree, depth 3           0.745  1.075
A forest of 50 trees        0.740  1.200
Boosting, 100 rounds        0.750  1.130
The forest, 5 in 6 sure     0.655  0.665

What you learn

What's in it

  1. A chain of questions
  2. Follow the questions
  3. How mixed is a pile?
  4. How mixed is a pile
  5. Finding the best question
  6. How Paul would find one question
  7. Growing the tree
  8. Fix Riya's version
  9. Grow the loan tree
  10. A panel of guessers
  11. Fifty guessers
  12. A forest of different trees
  13. Drawn with putting back
  14. Each tree fixes the last
  15. How Paul would boost
  16. What a mistake costs
  17. Ask the whole forest
  18. Ship it