<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>New Section 1. Understanding the dataset<jupyter_code>dataset = pd.read_csv("train.csv")
dataset.head()
dataset.shape<jupyter_output><empty_output><jupyter_text>**2.Data Preprocessing**1. Data Cleaning 2. Data Transformation3. Data Reuction 1. Data Cleaning<jupyter_code>dataset.info()
dataset.describe()<jupyter_output><empty_output><jupyter_text>**Exploring Dataset**<jupyter_code>pd.crosstab(dataset['Credit_History'], dataset['Loan_Status'], margins=True)
dataset.boxplot(column='ApplicantIncome')
dataset['ApplicantIncome'].hist(bins=20)
dataset.boxplot(column='ApplicantIncome' ,by='Education')
dataset.boxplot(column='LoanAmount')
dataset['LoanAmount'].hist(bins=20)
dataset['LoanAmount_log']=np.log(dataset['LoanAmount'])
dataset['LoanAmount_log'].hist(bins=20)
dataset.isnull().sum()
dataset['LoanAmount'].mean()
dataset['LoanAmount'].replace(np.NaN, dataset['LoanAmount'].mean()).head(15)
dataset['Gender'].fillna(dataset['Gender'].mode()[0],inplace=True)
dataset['Married'].fillna(dataset['Married'].mode()[0],inplace=True)
dataset['Dependents'].fillna(dataset['Dependents'].mode()[0],inplace=True)
dataset['Self_Employed'].fillna(dataset['Self_Employed'].mode()[0],inplace=True)       
dataset.LoanAmount = dataset.LoanAmount.fillna(dataset.LoanAmount.mean())
dataset.LoanAmount_log = dataset.LoanAmount_log.fillna(dataset.LoanAmount.mean())
dataset['Loan_Amount_Term'].fillna(dataset['Loan_Amount_Term'].mode()[0],inplace=True)
dataset['Credit_History'].fillna(dataset['Credit_History'].mode()[0],inplace=True)
dataset.isnull().sum()
dataset['TotalIncome']= dataset['ApplicantIncome']+ dataset['CoapplicantIncome']
dataset['TotalIncome_log'] =np.log(dataset['TotalIncome'])
dataset['TotalIncome_log'].hist(bins=20)
bins=[0,100,200,700]
group=['Low','Average','High']
dataset['LoanAmount_bin']=pd.cut(dataset['LoanAmount'],bins,labels=group)
LoanAmount_bin=pd.crosstab(dataset['LoanAmount_bin'],dataset['Loan_Status'])
LoanAmount_bin.div(LoanAmount_bin.sum(1).astype(float), axis=0).plot(kind="bar",stacked=True)
plt.xlabel('LoanAmount')
P=plt.ylabel('Percentage')
Married=pd.crosstab(dataset['Married'],dataset['Loan_Status'])
Dependents=pd.crosstab(dataset['Dependents'],dataset['Loan_Status'])
Education=pd.crosstab(dataset['Education'],dataset['Loan_Status'])
Self_Employed=pd.crosstab(dataset['Self_Employed'],dataset['Loan_Status'])
Married.div(Married.sum(1).astype(float), axis=0).plot(kind="bar",stacked=True,figsize=(4,4))
plt.show()
Dependents.div(Dependents.sum(1).astype(float), axis=0).plot(kind="bar",stacked=True,figsize=(4,4))
plt.show()
Education.div(Education.sum(1).astype(float), axis=0).plot(kind="bar",stacked=True,figsize=(4,4))
plt.show()
Self_Employed.div(Self_Employed.sum(1).astype(float), axis=0).plot(kind="bar",stacked=True,figsize=(4,4))
plt.show()
Credit_History=pd.crosstab(dataset['Credit_History'],dataset['Loan_Status'])
Property_Area=pd.crosstab(dataset['Property_Area'],dataset['Loan_Status'])
Credit_History.div(Credit_History.sum(1).astype(float), axis=0).plot(kind="bar",stacked=True,figsize=(4,4))
plt.show()
Property_Area.div(Property_Area.sum(1).astype(float), axis=0).plot(kind="bar",stacked=True)
plt.show()
matrix = dataset.corr()
f, ax = plt.subplots(figsize=(9,6))
sns.heatmap(matrix,vmax=.8,square=True,cmap="BuPu", annot = True)
#Variables with dark colour means there corelation is more.
dataset.head()
import matplotlib.pyplot as plt 
Y = dataset[dataset.Loan_Status == "Y"]
N = dataset[dataset.Loan_Status == "N"]
plt.title(" Yes vs No")
plt.xlabel("ApplicantIncome")
plt.ylabel("Loan Amount")
plt.scatter(Y.ApplicantIncome, Y.LoanAmount, color = "red", label = "Yes", alpha = 0.3)
plt.scatter(N.ApplicantIncome, N.LoanAmount, color = "lime", label = "No", alpha = 0.3)
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>2. data transformation<jupyter_code>X=dataset.iloc[:,np.r_[1:5,9:11,13:15]].values 
y=dataset.iloc[:,12].values
X
y<jupyter_output><empty_output><jupyter_text>Spliting the dataset into training and testing dataset<jupyter_code>from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=0)
print(x_train)
from sklearn.preprocessing import LabelEncoder
labelEncoder_x = LabelEncoder()
for i in range (0, 5):
  x_train[:, i]= labelEncoder_x.fit_transform(x_train[:,i])
x_train[:, i] = labelEncoder_x.fit_transform(x_train[:, 7])
x_train
labelencoder_y=LabelEncoder()
y_train=labelencoder_y.fit_transform(y_train)
y_train
for i in range (0, 5):
  x_test[:, i]= labelEncoder_x.fit_transform(x_test[:,i])
x_test[:, 7] = labelEncoder_x.fit_transform(x_test[:, 7])
labelencoder_y=LabelEncoder()
y_test=labelencoder_y.fit_transform(y_test)
x_test
y_test
from sklearn.preprocessing import StandardScaler
ss=StandardScaler()
x_train=ss.fit_transform(x_train)
x_test=ss.fit_transform(x_test)
from sklearn.metrics import confusion_matrix
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report
DTClassifier=DecisionTreeClassifier(criterion='entropy',random_state=0) 
DTClassifier.fit(x_train,y_train)
y_pred_DT=DTClassifier.predict(x_test)
print("y_pred_DT=",y_pred_DT)
print("\t")
print("y_test=",y_test)
print("\n")
from sklearn import metrics
from sklearn.metrics import accuracy_score
print ('The accuracy of decision tree classifier is ', metrics.accuracy_score(y_pred_DT, y_test))
print("classification report for logistic regression :-\n",classification_report(y_test, y_pred_DT))
print("\n")
print("Confusion matrix:\n")
confusion_matrix(y_test, y_pred_DT)
from sklearn.naive_bayes import GaussianNB
NBClassifier = GaussianNB()
NBClassifier.fit(x_train,y_train)
y_pred_NB= NBClassifier.predict(x_test)
print("y_pred_NB=",y_pred_NB)
print ('The accuracy of Naive Bayes is ', metrics.accuracy_score(y_pred_NB, y_test))
print("classification report for logistic regression :-\n",classification_report(y_test, y_pred_NB))
print("\n")
print("Confusion matrix:\n")
confusion_matrix(y_test, y_pred_NB)
#import ml model from sklearn package
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
logistic_model = LogisticRegression()
logistic_model.fit(x_train, y_train)
y_pred_LG = logistic_model.predict(x_test)
print('Coeeficient of model :', logistic_model.coef_)
print('Intercept of model', logistic_model.intercept_)
score = logistic_model.score(x_train, y_train)
print('accuracy_score overall :', score)
print('accuracy_score percent :', round(score*100,2))
print("classification report for logistic regression :-\n",classification_report(y_test, y_pred_LG))
print("\n")
print("Confusion matrix:\n")
confusion_matrix(y_test, y_pred_LG)
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators = 600)
rf.fit(x_train,y_train)
predictions_rfc = rf.predict(x_test)
score = rf.score(x_train, y_train)
print('accuracy_score overall :', score)
print('accuracy_score percent :', round(score*100,2))
print('Classification report for random forest:',classification_report(y_test, predictions_rfc))
print("\n")
print("Confusion matrix:\n")
confusion_matrix(y_test, predictions_rfc)
from sklearn import svm
from sklearn.metrics import accuracy_score
classifier = svm.SVC(kernel='linear')
classifier.fit(x_train,y_train)
x_train_prediction = classifier.predict(x_train)
y_pred_SVM = classifier.predict(x_test)
training_data_accuray = accuracy_score(x_train_prediction,y_train)
print('Accuracy on training data : ', training_data_accuray)
print('Classification report for support vector machine:\n',classification_report(y_test, y_pred_SVM))
print("\n")
print("Confusion matrix:\n")
confusion_matrix(y_test, y_pred_SVM)
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(x_train,y_train)
y_pred_KNN = knn.predict(x_test)
x_train_prediction = knn.predict(x_train)
training_data_accuray = accuracy_score(x_train_prediction,y_train)
print('Accuracy on training data : ', training_data_accuray)
print('Classification report for support vector machine:\n',classification_report(y_test, y_pred_KNN))
print("\n")
print("Confusion matrix:\n")
confusion_matrix(y_test, y_pred_KNN)
import pandas as pd

import pandas as sort_values
testdata = pd.read_csv("test.csv")
testdata.head()
testdata.info()
testdata.isnull().sum()
testdata['Gender'].fillna(testdata['Gender'].mode()[0],inplace=True)
testdata['Dependents'].fillna(testdata['Dependents'].mode()[0],inplace=True)
testdata['Self_Employed'].fillna(testdata['Self_Employed'].mode()[0],inplace=True)
testdata['Loan_Amount_Term'].fillna(testdata['Loan_Amount_Term'].mode()[0],inplace=True)
testdata['Credit_History'].fillna(testdata['Credit_History'].mode()[0],inplace=True)
testdata.isnull().sum()
testdata.boxplot(column='LoanAmount')
testdata.boxplot(column='ApplicantIncome')
testdata.LoanAmount = testdata.LoanAmount.fillna(testdata.LoanAmount.mean())
testdata['LoanAmount_log'] =np.log(testdata['LoanAmount'])
testdata.isnull().sum()
testdata['TotalIncome'] = testdata['ApplicantIncome']+testdata['CoapplicantIncome']
testdata['TotalIncome_log'] =np.log(testdata['TotalIncome'])
testdata.head()
test = testdata.iloc[:,np.r_[1:5,9:11,13:15]].values
for i in range(0,5):
  test[:,i]=labelEncoder_x.fit_transform(test[:,i])
test[:,7]=labelEncoder_x.fit_transform(test[:,7])
test
test = ss.fit_transform(test)
pred = rf.predict(test)
pred<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1550540765391015, 'fraction_numerical': 0.012063227953410981, 'mean_word_length': 11.250955414012738, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '339563', 'license': 'MIT', 'n_tokens_mistral': 3531, 'n_tokens_neox': 3400, 'n_words': 487, 'path': 'Loan_eligibility_prediction_machine_learning.ipynb', 'repo_name': 'Tarun-Kumar-Arcot/Loan-Eligibility-Prediction', 'size': '9616'}