<jupyter_start><jupyter_text>------- **Data Cleaning Train Set**<jupyter_code>df_train.info()
df_train.describe()
df_train.drop(columns=['Loan_ID'], inplace=True)
df_train['Gender'] = df_train['Gender'].map({
    'Male': 1,
    'Female': 0
})
df_train['Married'] = df_train['Married'].map({
    'Yes': 1,
    'No': 0
})
df_train['Dependents'] = df_train['Dependents'].map({
    '0': 0,
    '1': 1,
    '2': 2,
    '3+': 3
})
df_train['Education'] = df_train['Education'].map({
    'Graduate': 1,
    'Not Graduate': 0
})
df_train['Self_Employed'] = df_train['Self_Employed'].map({
    'Yes': 1,
    'No': 0
})
df_train['Property_Area'] = df_train['Property_Area'].map({
    'Urban': 0,
    'Rural': 1,
    'Semiurban': 2
})
df_train['Loan_Status'] = df_train['Loan_Status'].map({
    'Y': 1,
    'N': 0
})
df_train.rename(columns={
    'Gender': 'gender',
    'Married': 'married',
    'Dependents': 'dependents',
    'Education': 'education',
    'Self_Employed': 'self_employed',
    'ApplicantIncome': 'applicant_income',
    'CoapplicantIncome': 'coapplicant_income',
    'LoanAmount': 'loan_amount',
    'Loan_Amount_Term': 'loan_amount_term',
    'Credit_History': 'credit_history',
    'Property_Area': 'property_area',
    'Loan_Status': 'loan_status'
}, inplace=True)

df_train.isnull().sum()
from sklearn.impute import SimpleImputer
my_imputer = SimpleImputer()
imputed_df_train = pd.DataFrame(my_imputer.fit_transform(df_train))
imputed_df_train.columns = df_train.columns
df_train = imputed_df_train
df_train['gender'] = df_train['gender'].astype('int64')
df_train['married'] = df_train['married'].astype('int64')
df_train['dependents'] = df_train['dependents'].astype('int64')
df_train['education'] = df_train['education'].astype('int64')
df_train['self_employed'] = df_train['self_employed'].astype('int64')
df_train['applicant_income'] = df_train['applicant_income'].astype('int64')
df_train['loan_amount'] = df_train['loan_amount'].astype('int64')
df_train['loan_amount_term'] = df_train['loan_amount_term'].astype('int64')
df_train['credit_history'] = df_train['credit_history'].astype('int64')
df_train['property_area'] = df_train['property_area'].astype('int64')
df_train['loan_status'] = df_train['self_employed'].astype('int64')
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
df_train.iloc[:, 5:9] = scaler.fit_transform(df_train.iloc[:, 5:9])
df_train.head()<jupyter_output><empty_output><jupyter_text>------ **Data Cleaning Test Set**<jupyter_code>df_test.info()
df_test.drop(columns=['Loan_ID'], inplace=True)
df_test['Gender'] = df_test['Gender'].map({
    'Male': 1,
    'Female': 0
})
df_test['Married'] = df_test['Married'].map({
    'Yes': 1,
    'No': 0
})
df_test['Dependents'] = df_test['Dependents'].map({
    '0': 0,
    '1': 1,
    '2': 2,
    '3+': 3
})
df_test['Education'] = df_test['Education'].map({
    'Graduate': 1,
    'Not Graduate': 0
})
df_test['Self_Employed'] = df_test['Self_Employed'].map({
    'Yes': 1,
    'No': 0
})
df_test['Property_Area'] = df_test['Property_Area'].map({
    'Urban': 0,
    'Rural': 1,
    'Semiurban': 2
})
df_test.rename(columns={
    'Gender': 'gender',
    'Married': 'married',
    'Dependents': 'dependents',
    'Education': 'education',
    'Self_Employed': 'self_employed',
    'ApplicantIncome': 'applicant_income',
    'CoapplicantIncome': 'coapplicant_income',
    'LoanAmount': 'loan_amount',
    'Loan_Amount_Term': 'loan_amount_term',
    'Credit_History': 'credit_history',
    'Property_Area': 'property_area'
}, inplace=True)

df_test.isnull().sum()
my_imputer_test = SimpleImputer()
imputed_df_test = pd.DataFrame(my_imputer_test.fit_transform(df_test))
imputed_df_test.columns = df_test.columns
df_test = imputed_df_test
df_test['gender'] = df_test['gender'].astype('int64')
df_test['married'] = df_test['married'].astype('int64')
df_test['dependents'] = df_test['dependents'].astype('int64')
df_test['education'] = df_test['education'].astype('int64')
df_test['self_employed'] = df_test['self_employed'].astype('int64')
df_test['applicant_income'] = df_test['applicant_income'].astype('int64')
df_test['loan_amount'] = df_test['loan_amount'].astype('int64')
df_test['loan_amount_term'] = df_test['loan_amount_term'].astype('int64')
df_test['credit_history'] = df_test['credit_history'].astype('int64')
df_test['property_area'] = df_test['property_area'].astype('int64')
df_test.iloc[:, 5:9] = scaler.transform(df_test.iloc[:, 5:9])
df_test.head()<jupyter_output><empty_output><jupyter_text>-------------- **Machine Learning**<jupyter_code>x = df_train.iloc[:, 0:-1]
y = df_train['loan_status']

from sklearn.model_selection import train_test_split
x_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size=0.3, random_state=3)
from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
    layers.Dense(units=512, activation='relu', input_shape=[11]),
    layers.Dense(units=512, activation='relu'),
    layers.Dense(units=512, activation='relu'),
    layers.Dense(units=2, activation='sigmoid')
])

model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

history = model.fit(
    x_train, y_train,
    validation_data=(x_valid, y_valid),
    batch_size=512,
    epochs=50,
    verbose=0
)
history_df_train = pd.DataFrame(history.history)
print(history_df_train['accuracy'].max())
print(history_df_train['val_accuracy'].max())
y_pred_test = model.predict_classes(df_test)
df_test_csv = pd.read_csv('test_data_loan.csv')
df_pred_test = pd.DataFrame(df_test_csv['Loan_ID'])
df_pred_test['Loan_Status'] = y_pred_test
df_pred_test['Loan_Status'] = df_pred_test['Loan_Status'].map({
    0: 'N',
    1: 'Y'
})
df_pred_test.set_index('Loan_ID', inplace=True)
df_pred_test.to_csv('train_data_predict.csv')
df_pred_test.head()
model.save('loan_model.h5')
import pickle

with open('scaler', 'wb') as f:
    pickle.dump(scaler, f)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18888144675150703, 'fraction_numerical': 0.01942397856664434, 'mean_word_length': 6.484962406015038, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '194130', 'license': 'MIT', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2255, 'n_words': 318, 'path': 'Jupyter Notebook/Loan Jupyternotebook.ipynb', 'repo_name': 'hafidh561/Machine-Learning-Predict-Loan-Status', 'size': '5972'}