import pandas as pd import numpy as np import seaborn as sns import matplotlib as plt import matplotlib.pyplot as plt from sklearn import preprocessing from sklearn.preprocessing import LabelEncoder import gradio as gr from array import * #from google.colab import drive #drive.mount('/content/drive') df_train = pd.read_csv("train_ctrUa4K.csv") #Reading the dataset in a dataframe using Pandas df_train.head() df_train.describe() df_train.shape df_train.info() df_train.isnull().sum() print(df_train['Gender'].value_counts()) print(df_train['Married'].value_counts()) print(df_train['Dependents'].value_counts()) print(df_train['Self_Employed'].value_counts()) print(df_train['Credit_History'].value_counts()) print(df_train['Property_Area'].value_counts()) df_train['Gender'].fillna("Male", inplace = True) df_train['Married'].fillna("Yes", inplace = True) df_train['Dependents'].fillna("0", inplace = True) df_train['Self_Employed'].fillna("No", inplace = True) df_train['Credit_History'].fillna(1.0, inplace = True) df_train.isnull().sum() duplicate=df_train.duplicated() print(duplicate.sum()) df_train[duplicate] fig, ax = plt.subplots(3, 2, figsize = (10, 7)) sns.boxplot(x= df_train["ApplicantIncome"], ax = ax[0,0]) sns.distplot(df_train['ApplicantIncome'], ax = ax[0,1]) sns.boxplot(x= df_train["CoapplicantIncome"], ax = ax[1,0]) sns.distplot(df_train['CoapplicantIncome'], ax = ax[1,1]) sns.boxplot(x= df_train["Loan_Amount_Term"], ax = ax[2,0]) sns.distplot(df_train['Loan_Amount_Term'], ax = ax[2,1]) def remove_outlier(col): sorted(col) Q1, Q3=col.quantile([0.25, 0.75]) IQR=Q3-Q1 lower_range=Q1-(1.5*IQR) upper_range=Q3+(1.5*IQR) return lower_range, upper_range low_AI, high_AI=remove_outlier(df_train['ApplicantIncome']) df_train['ApplicantIncome']=np.where(df_train['ApplicantIncome']>high_AI, high_AI, df_train['ApplicantIncome']) df_train['ApplicantIncome']=np.where(df_train['ApplicantIncome']high_CI, high_CI, df_train['CoapplicantIncome']) df_train['CoapplicantIncome']=np.where(df_train['CoapplicantIncome']high_LAT, high_LAT, df_train['Loan_Amount_Term']) df_train['Loan_Amount_Term']=np.where(df_train['Loan_Amount_Term'] thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') from sklearn.model_selection import GridSearchCV,RandomizedSearchCV from sklearn.linear_model import LogisticRegression #from sklearn.metrics import confusion_matrix parametersLR={ 'penalty' : ['l1', 'l2', 'elasticnet', 'none'], 'C': [1, 0.5, 0.1, 0.01], 'fit_intercept': [True, False], 'solver' : ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'], 'random_state':[10, 50, 100, 'none'] } LR = LogisticRegression() #r = RandomizedSearchCV(LR,parametersLR) g=GridSearchCV(LR, parametersLR) g.fit(x_train, y_train) ypred = g.predict(x_test) ypred print (classification_report(y_test, ypred)) l = {'Gender': [1], 'Married': [0], 'Dependents':[0], 'Education':[0], 'Self_Employed':[0], 'LoanAmount':[130], 'Loan_Amount_Term':[360], 'Credit_History':[1], 'Property_Area':[2], 'Total_income':[5849] } df=pd.DataFrame(l) ans = g.predict(df) ans2 = ans.tolist() ans2[0] df def pred(Gender, Marital_Status, Dependents, Education, Self_Employed, Loan_Amount, Credit_History, Property_Area, Total_Income): if Gender == "Male": gen=1 elif Gender =="Female": gen=0 if Marital_Status=="Married": m=1 elif Marital_Status=="Unmarried": m=0 if Dependents=="0": d=0 elif Dependents=="1": d=1 elif Dependents=="2": d=2 elif Dependents=="3+": d=3 if Education=="Educated": e=1 elif Education == "Uneducated": e=0 if Self_Employed=="Yes": se=1 elif Self_Employed=="No": se=0 if Credit_History=="1": ch=1 elif Credit_History=="0": ch=0 if Property_Area=="0": pa=0 elif Property_Area=="1": pa=1 elif Propert_Area=="2": pa=2 l = {'Gender': [gen], 'Married': [m], 'Dependents':[d], 'Education':[e], 'Self_Employed':[se], 'LoanAmount':[Loan_Amount], 'Loan_Amount_Term':[360], 'Credit_History':[ch], 'Property_Area':[pa], 'Total_income':[Total_Income] } df=pd.DataFrame(l) ans = g.predict(df) ans2 = ans.tolist() if ans2[0]=="Y": return "Loan Status: Approved!" elif ans2[0]=="N": return "Loan Status: Disapproved" iface = gr.Interface( fn=pred, inputs=[gr.inputs.Radio(["Male", "Female"]), gr.inputs.Radio(["Married", "Unmarried"]),gr.inputs.Radio(["0", "1","2", "3+"]), gr.inputs.Radio(["Educated", "Uneducated"]), gr.inputs.Radio(["Yes", "No"]), "text", gr.inputs.Radio(["1", "0"]), gr.inputs.Radio(["0", "1", "2"]), "text"], outputs="text") iface.launch(inline=False)