<jupyter_start><jupyter_text>Classification Problem<jupyter_code>import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns

import os
import sys
dataset = pd.read_csv(os.getcwd()+'/Python_Credit_Risk_XTrain.csv')
dataset.isnull().sum()/len(dataset)*100
dataset.info()
sns.boxplot(y='LoanAmount', data=dataset)
dataset.groupby('Credit_History').size()
dataset['Gender'] = dataset['Gender'].fillna('Male')
dataset['Married'] = dataset['Married'].fillna('Yes')
dataset['Dependents'] = dataset['Dependents'].fillna('0')
dataset['Self_Employed'] = dataset['Self_Employed'].fillna('No')
dataset['LoanAmount'] = dataset['LoanAmount'].fillna(dataset['LoanAmount'].median())
dataset['Loan_Amount_Term'] = dataset['Loan_Amount_Term'].fillna(360)
dataset['Credit_History'] = dataset['Credit_History'].fillna(1.0)
dataset.isnull().sum()/len(dataset)*100
dataset['Gender'] = dataset['Gender'].astype('category')
dataset['Gender'] = dataset['Gender'].cat.codes
dataset['Married'] = dataset['Married'].astype('category')
dataset['Married'] = dataset['Married'].cat.codes
dataset['Dependents'] = dataset['Dependents'].astype('category')
dataset['Dependents'] = dataset['Dependents'].cat.codes
dataset['Education'] = dataset['Education'].astype('category')
dataset['Education'] = dataset['Education'].cat.codes
dataset['Self_Employed'] = dataset['Self_Employed'].astype('category')
dataset['Self_Employed'] = dataset['Self_Employed'].cat.codes
dataset['Loan_Amount_Term'] = dataset['Loan_Amount_Term'].astype('category')
dataset['Loan_Amount_Term'] = dataset['Loan_Amount_Term'].cat.codes
dataset['Property_Area'] = dataset['Property_Area'].astype('category')
dataset['Property_Area'] = dataset['Property_Area'].cat.codes
dataset['Loan_Status'] = dataset['Loan_Status'].astype('category')
dataset['Loan_Status'] = dataset['Loan_Status'].cat.codes
dataset
x = dataset.iloc[:,1:12].values
y = dataset.iloc[:,-1].values
from sklearn.preprocessing import StandardScaler
sc_x = StandardScaler()
x = sc_x.fit_transform(x)<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression
logmodel = LogisticRegression(solver='lbfgs')
logmodel.fit(x,y)
test = pd.read_csv('Python_Module_Day_15.4_Credit_Risk_Validate_data_XTEST.csv')
test.isnull().sum()/len(test)*100
test.groupby('Credit_History').size()
test['Gender'] = test['Gender'].fillna('Male')
test['Gender'] = test['Gender'].astype('category')
test['Gender'] = test['Gender'].cat.codes
test['Dependents'] = test['Dependents'].fillna('0')
test['Dependents'] = test['Dependents'].astype('category')
test['Dependents'] = test['Dependents'].cat.codes
test['Self_Employed'] = test['Self_Employed'].fillna('No')
test['Self_Employed'] = test['Self_Employed'].astype('category')
test['Self_Employed'] = test['Self_Employed'].cat.codes
sns.boxplot(y='LoanAmount', data=test)
test['LoanAmount'] = test['LoanAmount'].fillna(test['LoanAmount'].median())
test['Loan_Amount_Term'] = test['Loan_Amount_Term'].fillna(360)
test['Loan_Amount_Term'] = test['Loan_Amount_Term'].astype('category')
test['Loan_Amount_Term'] = test['Loan_Amount_Term'].cat.codes
test['Credit_History'] = test['Credit_History'].fillna(1.0)
test['outcome'] = test['outcome'].astype('category')
test['outcome'] = test['outcome'].cat.codes
test['Married'] = test['Married'].astype('category')
test['Married'] = test['Married'].cat.codes
test['Education'] = test['Education'].astype('category')
test['Education'] = test['Education'].cat.codes
test['Property_Area'] = test['Property_Area'].astype('category')
test['Property_Area'] = test['Property_Area'].cat.codes
test
x_test = test.iloc[:,1:12].values
x_test
y_test = test.iloc[:,-1].values
from sklearn.preprocessing import StandardScaler
sc_x = StandardScaler()
x_test = sc_x.fit_transform(x_test)
y_pred = logmodel.predict(x_test)
y_pred
from sklearn.metrics import confusion_matrix
cm =confusion_matrix(y_test, y_pred)
(cm[0,0]+cm[1,1])/(cm[0,0]+cm[1,0]+cm[0,1]+cm[1,1])*100<jupyter_output><empty_output><jupyter_text>With Logistic regression we are getting 95% accuracy  K-Nearest Neighbours<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
classifier_knn = KNeighborsClassifier(n_neighbors=5, metric='euclidean', p=2)
classifier_knn.fit(x,y)
y_pred = classifier_knn.predict(x_test)
cm=confusion_matrix(y_test,y_pred)
cm
(cm[0,0]+cm[1,1])/(cm[0,0]+cm[1,0]+cm[0,1]+cm[1,1])*100<jupyter_output><empty_output><jupyter_text>With KNN we are getting 92% accuracy  Naive Bayes<jupyter_code>from sklearn.naive_bayes import GaussianNB
classifier_nb = GaussianNB()
classifier_nb.fit(x, y)
y_pred = classifier_nb.predict(x_test)
cm=confusion_matrix(y_test,y_pred)
(cm[0,0]+cm[1,1])/(cm[0,0]+cm[1,0]+cm[0,1]+cm[1,1])*100<jupyter_output><empty_output><jupyter_text>With KNN we are getting 92% accuracy  Support Vector Machine - Linear Kernel<jupyter_code>from sklearn.svm import SVC
classifier_svm_linear = SVC(kernel='linear')
classifier_svm_linear.fit(x, y)
y_pred = classifier_svm_linear.predict(x_test)
cm=confusion_matrix(y_test,y_pred)
(cm[0,0]+cm[1,1])/(cm[0,0]+cm[1,0]+cm[0,1]+cm[1,1])*100<jupyter_output><empty_output><jupyter_text>With SVM - Linear Kernel we are getting 95% accuracy  Support Vector Machine - Sigmoid Kernel<jupyter_code>from sklearn.svm import SVC
classifier_svm_sigmoid = SVC(kernel='sigmoid')
classifier_svm_sigmoid.fit(x, y)
y_pred = classifier_svm_sigmoid.predict(x_test)
cm=confusion_matrix(y_test,y_pred)
(cm[0,0]+cm[1,1])/(cm[0,0]+cm[1,0]+cm[0,1]+cm[1,1])*100<jupyter_output><empty_output><jupyter_text>With SVM - Sigmoid Kernel we are getting 91% accuracy  Support Vector Machine - Radial Basis Function Kernel<jupyter_code>from sklearn.svm import SVC
classifier_svm_rbf = SVC(kernel='rbf')
classifier_svm_rbf.fit(x, y)
y_pred = classifier_svm_rbf.predict(x_test)
cm=confusion_matrix(y_test,y_pred)
(cm[0,0]+cm[1,1])/(cm[0,0]+cm[1,0]+cm[0,1]+cm[1,1])*100<jupyter_output><empty_output><jupyter_text>With SVM - RBF Kernel we are getting 94% accuracy  Support Vector Machine - Polynomial Function Kernel<jupyter_code>from sklearn.svm import SVC
classifier_svm_poly = SVC(kernel='poly')
classifier_svm_poly.fit(x, y)
y_pred = classifier_svm_poly.predict(x_test)
(cm[0,0]+cm[1,1])/(cm[0,0]+cm[1,0]+cm[0,1]+cm[1,1])*100<jupyter_output><empty_output><jupyter_text>With SVM - Poly Kernel we are getting 94% accuracy  Decision Tree<jupyter_code>from sklearn.tree import DecisionTreeClassifier
classifier_dt = DecisionTreeClassifier(criterion='entropy')
classifier_dt.fit(x, y)
y_pred = classifier_dt.predict(x_test)
cm=confusion_matrix(y_test, y_pred)
(cm[0,0]+cm[1,1])/(cm[0,0]+cm[1,0]+cm[0,1]+cm[1,1])*100<jupyter_output><empty_output><jupyter_text>With Decision Tree we are getting 74% accuracy  Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
classifier_rf = RandomForestClassifier(n_estimators=3, criterion='entropy')
classifier_rf.fit(x, y)
y_pred = classifier_rf.predict(x_test)
cm=confusion_matrix(y_test, y_pred)
(cm[0,0]+cm[1,1])/(cm[0,0]+cm[1,0]+cm[0,1]+cm[1,1])*100<jupyter_output><empty_output><jupyter_text>With Decision Tree we are getting 86% accuracy<jupyter_code>validation = pd.read_csv('Python_Module_Day_15.3_Credit_Risk_Test_data.csv')
validation.isnull().sum()/len(validation)*100
validation.groupby('Credit_History').size()
validation['Gender'] = validation['Gender'].fillna('Male')
validation['Gender'] = validation['Gender'].astype('category')
validation['Gender'] = validation['Gender'].cat.codes
validation
validation['Dependents'] = validation['Dependents'].fillna('0')
validation['Dependents'] = validation['Dependents'].astype('category')
validation['Dependents'] = validation['Dependents'].cat.codes
validation['Self_Employed'] = validation['Self_Employed'].fillna('No')
validation['Self_Employed'] = validation['Self_Employed'].astype('category')
validation['Self_Employed'] = validation['Self_Employed'].cat.codes
sns.boxplot(y='LoanAmount', data=validation)
validation['LoanAmount'] = validation['LoanAmount'].fillna(validation['LoanAmount'].median())
validation['Loan_Amount_Term'] = validation['Loan_Amount_Term'].fillna(360)
validation['Credit_History'] = validation['Credit_History'].fillna(1.0)
validation['Married'] = validation['Married'].astype('category')
validation['Married'] = validation['Married'].cat.codes
validation['Education'] = validation['Education'].astype('category')
validation['Education'] = validation['Education'].cat.codes
validation['Property_Area'] = validation['Property_Area'].astype('category')
validation['Property_Area'] = validation['Property_Area'].cat.codes
x_val=validation.iloc[:,1:12].values
from sklearn.preprocessing import StandardScaler
sc_x = StandardScaler()
x_val = sc_x.fit_transform(x_val)<jupyter_output><empty_output><jupyter_text>Applying model in the validation data<jupyter_code>y_pred = logmodel.predict(x_val)
y_pred
y_pred = classifier_knn.predict(x_val)
y_pred
y_pred = classifier_nb.predict(x_val)
y_pred
y_pred = classifier_svm_linear.predict(x_val)
y_pred
y_pred = classifier_svm_sigmoid.predict(x_val)
y_pred
y_pred = classifier_svm_rbf.predict(x_val)
y_pred
y_pred = classifier_svm_poly.predict(x_val)
y_pred
y_pred = classifier_dt.predict(x_val)
y_pred
y_pred = classifier_rf.predict(x_val)
y_pred<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16943521594684385, 'fraction_numerical': 0.02175543885971493, 'mean_word_length': 11.311345646437994, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '541567', 'license': 'MIT', 'n_tokens_mistral': 3504, 'n_tokens_neox': 3344, 'n_words': 440, 'path': 'Classification/.ipynb_checkpoints/SupervisedLearning-Classification-Credit Risk-checkpoint.ipynb', 'repo_name': 'pratikbarjatya/DataScience', 'size': '9331'}