text_plain_output_1.png
from sklearn.metrics import mean_absolute_error

from sklearn.tree import DecisionTreeRegressor

import numpy as np

import pandas as pd

import seaborn as sns

import statsmodels.api as sm

df = pd.read_csv('/kaggle/input/personal-loan-modeling/Bank_Personal_Loan_Modelling.csv')

df.columns

model = sm.OLS.from_formula('Income ~ CCAvg', data=df)
inc_ccavg = model.fit()
inc_ccavg.summary()

model = sm.OLS.from_formula('Income ~ Mortgage', data=df)
inc_ccavg = model.fit()
inc_ccavg.summary()

df.rename(columns={'ID': 'ID', 'Age': 'Age', 'Experience': 'Experience', 'Income': 'Income', 'ZIP Code': 'ZIP Code', 'Family': 'Family', 'CCAvg': 'CCAvg', 'Education': 'Education', 'Mortgage': 'Mortgage', 'Personal Loan': 'PersonalLoan', 'Securities Account': 'SecuritiesAccount', 'CD Account': 'CDAccount', 'Online': 'Online', 'CreditCard': 'CreditCard'}, inplace=True)
df['CDAccount'].replace({0: 'No', 1: 'Yes'}, inplace=True)
df['PersonalLoan'].replace({0: 'No', 1: 'Yes'}, inplace=True)
df['Education'].replace({1: 'Undergrad', 2: 'Graduate', 3: 'Advanced/Professional'}, inplace=True)
df['Family'].replace({1: 'One', 2: 'Two', 3: 'Three', 4: 'Four'}, inplace=True)

categ_features = ['CDAccount', 'PersonalLoan', 'Education', 'Family']
for i in categ_features:
    x = 'Income ~' + i
    model = sm.OLS.from_formula(x, data=df)
    corr = model.fit()

from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_absolute_error
best = -1000
for i in range(5, 600, 5):
    model = DecisionTreeRegressor(random_state=1, max_leaf_nodes=i)
    income = model.fit(trainX, trainy)
    predicted_values = income.predict(valX)
    mae = -mean_absolute_error(valy, predicted_values)
    if mae > best:
        best = mae
print(-best)
x = 'model = DecisionTreeRegressor(random_state = 1, max_leaf_nodes = i)\nincome = model.fit(trainX, trainy)\npredicted_values = income.predict(valX)\nmae = mean_absolute_error(valy, predicted_values)'