# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import pandas as pd
from pathlib import Path
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, MinMaxScaler, LabelEncoder

train_df = pd.read_csv(Path('Resources/2019loans.csv'))
test_df = pd.read_csv(Path('Resources/2020Q1loans.csv'))

# # 1-Data Manage-Clean up-Remove Un named column

# # 1.1-Test Data 

test_df = test_df.drop('Unnamed: 0', axis=1)
test_df

# # 1.2-Train Data

train_df = train_df.drop('Unnamed: 0', axis=1)
train_df

# # 2-Data Conversion-Training Data

# # 2-1-Separate target feature

X_trainFeature = train_df.drop('loan_status', axis=1)
X_trainFeature

# # 2-2-Convert categorical data to numeric

X_train = pd.get_dummies(X_trainFeature)
X_train.head()

# # 2-3-output labels

y_train = train_df["loan_status"]
y_train

# # 3-Data Conversion-Test Data

# # 3-1-Separate target feature

X_testFeature = test_df.drop('loan_status', axis=1)
X_testFeature

# # 3-2-Convert categorical data to numeric.

X_test = pd.get_dummies(X_testFeature)
X_test.head()

# # 3-3-Find Missing Column

# +
for testColumn in X_test.columns:

    for trainColumn in X_train.columns:
            if trainColumn not in X_test.columns:
                   print(trainColumn)

             
# -

# # 3-4-Add missing dummy variables to testing set 

X_test["debt_settlement_flag_Y"]=0

# # 3-5: output labels

# y_test_label = LabelEncoder().fit_transform(test_df['loan_status'])
y_test = test_df['loan_status']
y_test

# # 4 Train the Logistic Regression model on the unscaled data and print the model score

# # 4.1-Train the Logistic Regression model on the unscaled data

from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier

# # 4.2 Fit (train) our model by using the training data

classifier.fit(X_train, y_train)
# X_test_dummies


# # 4.3 Validate the model by using the test data

# print(f"Training Data Score: {classifier.score(X_train, y_train)}")
# print(f"Testing Data Score: {classifier.score(X_test, y_test)}")
# X_dummies
print(f"Training Data Score: {classifier.score(X_train, y_train)}")
print(f"Testing Data Score: {classifier.score(X_test,y_test)}")
# print(X_test_dummies.head())

# # 5  Train Random Forest Classifier model and print the model score

from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(random_state=1, n_estimators=500).fit(X_train, y_train)
print(f'Training Score: {clf.score(X_train, y_train)}')
print(f'Testing Score: {clf.score(X_test, y_test)}')

# # 6 Scale the data

# Scaling the X data by using StandardScaler()
scaler = StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
X_train_scaled

X_test_scaled

from sklearn.metrics import classification_report

# # 6-1-Train the Logistic Regression model on the scaled data and print the  Model report

classifier = LogisticRegression()
# classifier
classifier.fit(X_train_scaled, y_train)
y_pred = classifier.predict(X_test_scaled)
print(classification_report(y_test, y_pred))


# # 6-2-Print the model score

print(f"Training Data Score: {classifier.score(X_train_scaled, y_train)}")
print(f"Testing Data Score: {classifier.score(X_test_scaled, y_test)}")

# # 7-Train a Random Forest Classifier model on the scaled data and print the model score

# Fit a model, and then print a classification report
clf = RandomForestClassifier(random_state=1).fit(X_train_scaled, y_train)
clf.fit(X_train_scaled, y_train)
y_pred = clf.predict(X_test_scaled)
print(classification_report(y_test, y_pred))
print(f"Training Data Score: {clf.score(X_train_scaled, y_train)}")
print(f"Testing Data Score: {clf.score(X_test_scaled, y_test)}")




StarcoderdataJupyterScriptsDedupFiltered