PRACTICAL 12 — LOAN APPROVAL PREDICTION USING A DECISION TREE
============================================================

OBJECTIVE
Predict whether a loan application is approved (1) or rejected (0) from applicant features using a Decision Tree Classifier.

PROGRAM FLOW
1. Import libraries.
2. Read the CSV and inspect its shape, first rows, and missing values.
3. Convert Loan_Status values Y/N to 1/0.
4. Remove rows with missing/invalid target labels.
5. Separate X (input features) and y (target).
6. Drop Loan_ID because it is an identifier, not a meaningful applicant feature.
7. Identify numeric and categorical columns.
8. Impute missing numeric values with the median.
9. Impute missing categorical values with the most frequent category.
10. One-hot encode categorical features.
11. Split data into training and test sets (80/20), using stratification.
12. Train the Decision Tree pipeline.
13. Predict test labels.
14. Evaluate using accuracy, confusion matrix, precision, recall, and F1-score.
15. Plot the confusion matrix and a readable portion of the tree.

IMPORTANT CODE CONCEPTS
- pandas.read_csv(): reads a CSV file into a DataFrame.
- df.shape: returns (number of rows, number of columns).
- df.head(): shows the first five rows by default.
- df.isnull().sum(): counts missing values by column.
- astype(str): converts values to strings.
- str.strip(): removes leading/trailing whitespace.
- str.upper(): normalizes labels to uppercase.
- map({"Y": 1, "N": 0}): maps approved/rejected labels to binary numbers.
- notna(): identifies values that are not missing.
- X: input features; y: target labels.
- ColumnTransformer: applies different preprocessing to different column types.
- SimpleImputer(strategy="median"): fills missing numeric values with each column's median.
- SimpleImputer(strategy="most_frequent"): fills missing categories with the most common category.
- OneHotEncoder(handle_unknown="ignore"): turns categories into indicator columns and tolerates unseen test categories.
- StandardScaler(): standardizes numeric columns. Scaling is not normally required for a Decision Tree, but this pipeline uses it consistently.
- train_test_split(test_size=0.2): reserves 20% for evaluation.
- random_state=42: makes randomized steps reproducible.
- stratify=y: approximately preserves class proportions in train and test data.
- Pipeline: chains preprocessing and model fitting to help avoid leakage.
- DecisionTreeClassifier: learns feature-based decision rules for classification.
- max_depth=4: limits tree depth to control complexity.
- min_samples_leaf=4: requires at least four training observations in each leaf.
- class_weight="balanced": gives more weight to less frequent classes.
- fit(): trains/learns from the training data.
- predict(): produces predictions for new examples.
- accuracy_score(): fraction of predictions that are correct.
- confusion_matrix(): compares actual and predicted classes.
- precision: among predicted positives, the fraction that are truly positive.
- recall: among actual positives, the fraction correctly identified.
- F1-score: harmonic mean of precision and recall.
- plot_tree(): visualizes a fitted decision tree.
- max_depth=3 in plot_tree(): limits only the visualization, not the fitted tree.

CONFUSION MATRIX
For labels 0 = Rejected and 1 = Approved, sklearn's binary confusion matrix is arranged as:
[[TN, FP],
 [FN, TP]]

TN: actual rejected and predicted rejected.
FP: actual rejected but predicted approved.
FN: actual approved but predicted rejected.
TP: actual approved and predicted approved.

VIVA QUESTIONS AND ANSWERS
1. What is the objective of this program?
   To predict loan approval status using applicant information and a Decision Tree Classifier.
2. What type of machine learning is used?
   Supervised learning, because the training data includes the known target Loan_Status.
3. Is this classification or regression?
   Classification, because the output is a category: approved or rejected.
4. What is the target variable?
   Loan_Status.
5. What do X and y represent?
   X contains input features; y contains the target labels.
6. Why convert Y and N to 1 and 0?
   To create consistent binary labels for the classification task.
7. Why remove rows with missing target labels?
   The model needs known target labels for supervised training and evaluation.
8. Why drop Loan_ID?
   It is an identifier and normally carries no meaningful information about a person's loan eligibility.
9. Why inspect missing values?
   Missing values can cause model errors or reduce data quality, so they must be handled appropriately.
10. Why use median imputation for numerical features?
    The median is robust to unusually large or small values.
11. Why use most-frequent imputation for categorical features?
    It fills missing categories with a valid, commonly observed category.
12. What is One-Hot Encoding?
    A method that represents categories as separate binary indicator columns.
13. Why use One-Hot Encoding?
    Many scikit-learn estimators require numerical input rather than raw text categories.
14. What is a ColumnTransformer?
    It applies specified transformations to selected groups of columns.
15. What is a Pipeline?
    It chains preprocessing and model steps into one fitted workflow.
16. Why split the dataset?
    To train on one subset and estimate performance on unseen records using another subset.
17. What does test_size=0.2 mean?
    Twenty percent of the available records are used for testing and approximately eighty percent for training.
18. What does random_state=42 do?
    It makes randomized splitting and model operations reproducible.
19. What does stratify=y do?
    It helps preserve the class proportions in training and test subsets.
20. What is a Decision Tree?
    A supervised algorithm that makes predictions by applying a sequence of feature-based splits.
21. How does a Decision Tree choose splits?
    It selects splits that improve a criterion such as Gini impurity or information gain, depending on the configured criterion.
22. What is a root node?
    The first node of the tree, representing the first split.
23. What is a leaf node?
    A terminal node that gives the final prediction.
24. What is max_depth=4?
    It limits the maximum depth of the fitted tree to four.
25. Why limit tree depth?
    To control model complexity and reduce the risk of overfitting.
26. What does min_samples_leaf=4 mean?
    Each leaf must contain at least four training samples.
27. What does class_weight="balanced" do?
    It assigns class weights inversely related to class frequencies in the training data.
28. Is StandardScaler necessary for Decision Trees?
    Usually not. Tree splits are generally unaffected by monotonic scaling, but scaling is common for algorithms based on distances or regularization.
29. What is fit()?
    It learns the preprocessing parameters and trains the model using the training data.
30. What is predict()?
    It generates predicted labels for new or held-out examples.
31. What is accuracy?
    The proportion of all predictions that are correct.
32. What is a confusion matrix?
    A table comparing actual and predicted class labels.
33. What are TP, TN, FP, and FN?
    True Positive, True Negative, False Positive, and False Negative.
34. What is precision?
    TP / (TP + FP), when the denominator is nonzero.
35. What is recall?
    TP / (TP + FN), when the denominator is nonzero.
36. What is the F1-score?
    The harmonic mean of precision and recall.
37. Why can accuracy be misleading?
    With imbalanced classes, a model can predict the majority class frequently and still achieve high accuracy while missing the minority class.
38. What is overfitting?
    When a model fits training data too closely and performs poorly on unseen data.
39. How can overfitting be reduced?
    Limit tree depth, increase minimum leaf size, prune the tree, use cross-validation, or collect more representative data.
40. What does plot_tree() do?
    It displays the fitted Decision Tree as a diagram.
41. Why use get_feature_names_out()?
    Preprocessing, especially one-hot encoding, can create new feature columns; the method provides their transformed names.
42. What does the confusion-matrix plot show?
    It visually displays counts of correct and incorrect predictions for each class.
43. Is this model ready to make real bank lending decisions?
    No. It is an educational baseline. Real lending requires validation, fairness checks, explainability, governance, and compliance review.
44. What is the main limitation of this program?
    Its performance depends on the dataset's quality and representativeness; its predictions are not guarantees of actual loan repayment or suitability.
45. How could this model be improved?
    Use cross-validation and hyperparameter tuning, evaluate class-specific metrics and fairness, compare alternative models, and validate on more recent representative data.

SHORT VIVA EXPLANATION
“This program predicts loan approval using supervised classification. I load the CSV, map Loan_Status from Y/N to 1/0, separate features and target, and remove the Loan_ID identifier. I identify numeric and categorical columns, impute missing values, and one-hot encode categories. I split the data into training and testing subsets, train a Decision Tree using a pipeline, and predict the test labels. Finally, I evaluate the model with accuracy, a confusion matrix, precision, recall, and F1-score, and display the confusion matrix and decision tree.”

SAMPLE QUESTIONS FOR ORAL PRACTICE
1. Why is this a supervised learning problem?
2. Why is Loan_Status the target?
3. What is the difference between fit() and predict()?
4. What is overfitting and how is max_depth related to it?
5. Why can accuracy be insufficient for an imbalanced dataset?
6. What is the difference between precision and recall?
7. What do false positives and false negatives mean in this example?
8. Why do we use a Pipeline?
9. Why are categorical columns encoded?
10. Why should this model not be used as the sole basis for real loan decisions?
