BATOMEN/Application_Complete
0
1import streamlit as st2import pandas as pd 3import seaborn as sns4import matplotlib.pyplot as plt5import pandas_profiling6import streamlit_pandas_profiling7import pickle8import numpy as np9import base6410 11from pycaret.classification import load_model, predict_model12# from sklearn.ensemble import HistGradientBoostingClassifier13from sklearn.ensemble import GradientBoostingClassifier14from sklearn.ensemble import RandomForestClassifier15from sklearn.tree import DecisionTreeClassifier16from sklearn.neighbors import KNeighborsClassifier17from xgboost import XGBClassifier18from pycaret.classification import load_model, predict_model19 20from streamlit_pandas_profiling import st_profile_report21 22from pycaret.regression import setup as setup_reg23from pycaret.regression import compare_models as compare_models_reg24from pycaret.regression import save_model as save_model_reg25from pycaret.regression import plot_model as plot_model_reg26 27from pycaret.classification import setup as setup_class28from pycaret.classification import compare_models as compare_models_class29from pycaret.classification import save_model as save_model_class30from pycaret.classification import plot_model as plot_model_class31 32import xgboost as xgb33from sklearn import svm34from sklearn.model_selection import cross_val_score35 36url = "https://www.linkedin.com/in/junior-batomen-003676239/"37 38# background image39def add_bg_from_local(image_file):40 with open(image_file, "rb") as image_file:41 encoded_string = base64.b64encode(image_file.read())42 st.markdown(43 f"""44 <style>45 .stApp {{46 background-image: url(data:image/{"jpg"};base64,{encoded_string.decode()});47 background-size: cover48 49 width: 300px50 height: 300px51 }}52 </style>53 """,54 unsafe_allow_html=True55 )56add_bg_from_local('./zobo.png')57 58@st.cache59def load_data(file):60 data = pd.read_csv(file)61 return data62 63st.set_option('deprecation.showPyplotGlobalUse', False)64@st.cache_data65def load_data(dataset):66 df = pd.read_csv(dataset)67 return df68 69 70st.sidebar.image("Image.png",width=300)71def main():72 73 st.sidebar.write("[Author: Batomen Junior](%s)" % url)74 75 menu=["AutoML (No-Code)","Import Packages & Data","Data Quality & Missing Value Assesment","Final Adjustments to Data","Making Prediction","Result","Machine Learning"]76 choice = st.sidebar.selectbox("select Menu",menu)77 78 if choice == "AutoML (No-Code)":79 80 81 st.subheader(":smile: Ici, l'utlisateur à la possibilité de Generer des prédictions (Classsification, Regression) avec du No-Code en chargant simplement son dataset")82 file = st.file_uploader("Upload your dataset (Loan+Approval+Prediction.csv) in csv format", type=["csv"])83 84 if file is not None:85 data = load_data(file)86 st.dataframe(data.head())87 88 profile = st.button("Profile Dataset")89 if profile:90 profile_df = data.profile_report()91 st_profile_report(profile_df)92 93 target = st.selectbox("Select the target variable", data.columns)94 task = st.selectbox("Select a ML task", ["Regression", "Classification"])95 data = data.dropna(subset=[target])96 97 if task == "Regression":98 if st.button("Run Modelling"):99 exo_reg = setup_reg(data, target =target)100 model_reg = compare_models_reg()101 save_model_reg(model_reg, "best_reg_model")102 st.success("Regression Model buil successfully!")103 104 # Results105 st.write("Residuals")106 plot_model_reg(model_reg, plot = 'residuals', save=True)107 st.image("Residuals.png")108 109 st.image("Feature importance")110 plot_model_reg(model_reg, plot = 'feature', save=True)111 st.image("Feature Importance.png")112 113 with open('best_reg_model.pkl', 'rb') as f:114 st.download_button('Download Pipeline Model', f, file_name="best_reg_moedel.pkl")115 116 117 if task == "Classification":118 if st.button('Run Modeling'):119 exp_class = setup_class(data, target = target)120 model_class = compare_models_class()121 save_model_class(model_class, 'best_class_model')122 st.success("Classification Model built Successfully!")123 124 # Results125 col5, col6 = st.columns(2)126 with col5:127 st.write("ROC curve")128 # plot_model_class(model_class, save=True)129 st.image("AUC.png")130 131 with col6:132 st.write("Classification Report")133 # plot_model_class(model_class, plot = 'class_report', save=True)134 st.image("Class Report.png")135 136 col7, col8 = st.columns(2)137 with col7:138 st.write("Confusion Matrix")139 # plot_model_class(model_class, plot = 'confusion_matrix', save=True)140 st.image("Confusion Matrix.png")141 142 with col8:143 st.write("Feature Importance")144 # plot_model_class(model_class, plot = 'feature', save=True)145 st.image("Feature Importance.png")146 147 # Download the model148 with open('best_class_model.pkl', 'rb') as f:149 st.download_button('Download Model', f, file_name="best_class_model.pkl")150 151 if choice == "Import Packages & Data":152 st.markdown("<h1 style='text-align:center;color: brown;'> Streamlit Python application </h1>",unsafe_allow_html=True)153 st.markdown("<h2 style='text-align:center;color: black;'> Classification on Datatset Loan+Approval+Prediction </h2>",unsafe_allow_html=True)154 left,middle,right = st.columns((2,3,2))155 with middle:156 st.image("Capture.png",width=400)157 st.write("Loan Approval Prediction using Machine Learning. ")158 st.subheader("Information Crédit")159 st.write("LOANS are the major requirement of the modern world. By this only, Banks get a major part of the total profit. It is beneficial for students to manage their education and living expenses, and for people to buy any kind of luxury like houses, cars, etc. But when it comes to deciding whether the applicant’s profile is relevant to be granted with loan or not. Banks have to look after many aspects.")160 st.write("Visualize DataSet")161 data = load_data("Loan+Approval+Prediction.csv")162 st.write(data.head(5))163 if choice == "Data Quality & Missing Value Assesment":164 st.subheader("Preview data information")165 data = load_data("Loan+Approval+Prediction.csv")166 st.write(data.describe())167 st.image("jojo.PNG")168 st.subheader("Check missing values")169 data = load_data("Loan+Approval+Prediction.csv")170 st.write(data.isnull().sum())171 st.subheader("Gender - Missing Values")172 st.write('Percent of missing "Gender" records is %.2f%%' %((data['Gender'].isnull().sum()/data.shape[0])*100))173 st.write("Number of people who take a loan group by gender :")174 st.write(data['Gender'].value_counts())175 fig = plt.figure(figsize = (5,5))176 data = load_data('Loan+Approval+Prediction.csv')177 sns.countplot(x='Gender', data=data)178 st.pyplot(fig)179 st.subheader("Married - Missing Values")180 st.write('Percent of missing "Married" records is %.2f%%' %((data['Married'].isnull().sum()/data.shape[0])*100))181 st.write("Number of people who take a loan group by marital status :")182 st.write(data['Married'].value_counts())183 fig = plt.figure(figsize = (5,5))184 data = load_data('Loan+Approval+Prediction.csv')185 sns.countplot(x='Married', data=data, palette = 'Set2')186 st.pyplot(fig)187 st.subheader("Dependents- Missing Values")188 st.write('Percent of missing "Dependents" records is %.2f%%' %((data['Dependents'].isnull().sum()/data.shape[0])*100))189 st.write("Number of people who take a loan group by dependents :")190 st.write(data['Dependents'].value_counts())191 fig = plt.figure(figsize = (5,5))192 data = load_data('Loan+Approval+Prediction.csv')193 sns.countplot(x='Dependents', data=data, palette = 'Set2')194 st.pyplot(fig)195 st.subheader("Self Employed - Missing Values")196 st.write('Percent of missing "Self_Employed" records is %.2f%%' %((data['Self_Employed'].isnull().sum()/data.shape[0])*100))197 st.write("Number of people who take a loan group by self employed :")198 st.write(data['Self_Employed'].value_counts())199 fig = plt.figure(figsize = (5,5))200 data = load_data('Loan+Approval+Prediction.csv')201 sns.countplot(x='Self_Employed', data=data, palette = 'Set2')202 st.pyplot(fig)203 st.subheader("Loan Amount - Missing Values")204 st.write('Percent of missing "LoanAmount" records is %.2f%%' %((data['LoanAmount'].isnull().sum()/data.shape[0])*100))205 ax = data["LoanAmount"].hist(density=True, stacked=True, color='teal', alpha=0.6)206 st.write(data["LoanAmount"].plot(kind='density', color='teal'))207 fig = plt.figure(figsize = (5,5))208 data = load_data('Loan+Approval+Prediction.csv')209 sns.countplot(x='LoanAmount', data=data, color='teal')210 st.pyplot(fig)211 plt.show()212 st.subheader("Loan Amount Term - Missing Values")213 st.write('Percent of missing "Loan_Amount_Term" records is %.2f%%' %((data['Loan_Amount_Term'].isnull().sum()/data.shape[0])*100))214 st.write("Number of people who take a loan group by loan amount term :")215 st.write(data['Loan_Amount_Term'].value_counts())216 fig = plt.figure(figsize = (5,5))217 data = load_data('Loan+Approval+Prediction.csv')218 sns.countplot(x='Loan_Amount_Term', data=data, palette = 'Set2')219 st.pyplot(fig)220 st.subheader("Credit History - Missing Values")221 st.write('Percent of missing "Credit_History" records is %.2f%%' %((data['Credit_History'].isnull().sum()/data.shape[0])*100))222 st.write("Number of people who take a loan group by credit history :")223 st.write(data['Credit_History'].value_counts())224 fig = plt.figure(figsize = (5,5))225 data = load_data('Loan+Approval+Prediction.csv')226 sns.countplot(x='Credit_History', data=data, palette = 'Set2')227 st.pyplot(fig)228 229 230 231 if choice == "Final Adjustments to Data":232 st.write("Based on my assessment of the missing values in the dataset, I'll make the following changes to the data:")233 st.write("--> If 'Gender' is missing for a given row, I'll impute with Male (most common answer).")234 st.write("--> If 'Married' is missing for a given row, I'll impute with yes (most common answer).")235 st.write("--> If 'Dependents' is missing for a given row, I'll impute with 0 (most common answer).")236 st.write("--> If 'Self_Employed' is missing for a given row, I'll impute with no (most common answer).")237 st.write("--> If 'LoanAmount' is missing for a given row, I'll impute with mean of data.")238 st.write("--> If 'Loan_Amount_Term' is missing for a given row, I'll impute with 360 (most common answer).")239 st.write("--> If 'Credit_History' is missing for a given row, I'll impute with 1.0 (most common answer).")240 data = load_data('Loan+Approval+Prediction.csv')241 train_data = data.copy()242 st.write(train_data['Gender'].fillna(train_data['Gender'].value_counts().idxmax(), inplace=True))243 st.write(train_data['Married'].fillna(train_data['Married'].value_counts().idxmax(), inplace=True))244 st.write(train_data['Dependents'].fillna(train_data['Dependents'].value_counts().idxmax(), inplace=True))245 st.write(train_data['Self_Employed'].fillna(train_data['Self_Employed'].value_counts().idxmax(), inplace=True))246 st.write(train_data["LoanAmount"].fillna(train_data["LoanAmount"].mean(skipna=True), inplace=True))247 st.write(train_data['Loan_Amount_Term'].fillna(train_data['Loan_Amount_Term'].value_counts().idxmax(), inplace=True))248 st.write(train_data['Credit_History'].fillna(train_data['Credit_History'].value_counts().idxmax(), inplace=True))249 st.subheader("Checking missing values")250 st.write(train_data.isnull().sum())251 st.write(train_data)252 253 st.subheader("Convert some object data type to int64")254 gender_stat = {"Female": 0, "Male": 1}255 yes_no_stat = {'No' : 0,'Yes' : 1}256 dependents_stat = {'0':0,'1':1,'2':2,'3+':3}257 education_stat = {'Not Graduate' : 0, 'Graduate' : 1}258 property_stat = {'Semiurban' : 0, 'Urban' : 1,'Rural' : 2}259 260 261 train_data['Gender'] = train_data['Gender'].replace(gender_stat)262 train_data['Married'] = train_data['Married'].replace(yes_no_stat)263 train_data['Dependents'] = train_data['Dependents'].replace(dependents_stat)264 train_data['Education'] = train_data['Education'].replace(education_stat)265 train_data['Self_Employed'] = train_data['Self_Employed'].replace(yes_no_stat)266 train_data['Property_Area'] = train_data['Property_Area'].replace(property_stat)267 268 data = load_data('Loan+Approval+Prediction.csv')269 st.write(data.describe())270 st.image("jojo.PNG")271 st.write(data.isnull().sum())272 273 274 st.subheader("Loan+Predictions Dataset")275 data = load_data("Loan+Approval+Prediction.csv")276 st.write(data.head(5))277 278 if st.checkbox("Summary"):279 st.write(data.describe().head())280 elif st.checkbox("Corrélation"):281 plt.figure(figsize=(15,15))282 st.write(sns.heatmap(data.corr(),annot=True))283 st.pyplot()284 285 if choice == "Making Prediction":286 287 288 289 st.write("Separate feature and target")290 291 gender_stat = {"Female": 0, "Male": 1}292 yes_no_stat = {'No' : 0,'Yes' : 1}293 dependents_stat = {'0':0,'1':1,'2':2,'3+':3}294 education_stat = {'Not Graduate' : 0, 'Graduate' : 1}295 property_stat = {'Semiurban' : 0, 'Urban' : 1,'Rural' : 2}296 toto = load_data("Loan+Approval+Prediction.csv")297 toto.dropna()298 299 toto['Gender']=toto['Gender'].replace(['Female','Male'],[0, 1]) #replace target values to binary300 toto['Married']=toto['Married'].replace(['No','Yes'],[0, 1])301 toto['Education']=toto['Education'].replace(['Not Graduate','Graduate'],[0, 1])302 toto['Self_Employed']=toto['Married'].replace(['No','Yes'],[0, 1])303 toto['Property_Area']=toto['Property_Area'].replace(['Semiurban','Urban', 'Rural'],[0, 1, 2])304 toto['Loan_Status']=toto['Loan_Status'].replace(['Y','N'],[1, 0])305 306 307 308 train_data = toto.copy().dropna()309 310 train_data['Gender'] = train_data['Gender'].replace(gender_stat)311 train_data['Married'] = train_data['Married'].replace(yes_no_stat)312 train_data['Dependents'] = train_data['Dependents'].replace(dependents_stat)313 train_data['Education'] = train_data['Education'].replace(education_stat)314 train_data['Self_Employed'] = train_data['Self_Employed'].replace(yes_no_stat)315 train_data['Property_Area'] = train_data['Property_Area'].replace(property_stat)316 317 x = train_data.iloc[:,1:12]318 y = train_data.iloc[:,12]319 320 321 322 323 st.write("make variabel for save the result and to show it")324 classifier = ('Gradient Boosting','XGBoost','Random Forest','Decision Tree','K-Nearest Neighbor','SVM')325 y_pos = np.arange(len(classifier))326 score = []327 328 st.subheader(":bar_chart: Prediction avec GradientBoostingClassifier")329 clf = GradientBoostingClassifier()330 scores = cross_val_score(clf, x, y,cv=5)331 st.write(score.append(scores.mean()))332 st.write('The accuration of classification is %.2f%%' %(scores.mean()*100))333 334 st.subheader(":clipboard: Prediction avec XGBClassifier")335 clf = XGBClassifier()336 scores = cross_val_score(clf, x, y,cv=5)337 st.write(score.append(scores.mean()))338 st.write('The accuration of classification is %.2f%%' %(scores.mean()*100))339 340 st.subheader(":bar_chart: Prediction avec RandomForestClassifier")341 clf = RandomForestClassifier(n_estimators=10)342 scores = cross_val_score(clf, x,y,cv=5)343 score.append(scores.mean())344 st.write('The accuration of classification is %.2f%%' %(scores.mean()*100))345 346 st.subheader(":clipboard: Prediction avec DecesionTreeClassifier")347 clf = DecisionTreeClassifier()348 scores = cross_val_score(clf, x, y,cv=5)349 score.append(scores.mean())350 st.write('The accuration of classification is %.2f%%' %(scores.mean()*100))351 352 st.subheader(":smile: Prediction avec KNeighbordClassifier")353 clf = KNeighborsClassifier()354 scores = cross_val_score(clf, x, y,cv=5)355 score.append(scores.mean())356 st.write('The accuration of classification is %.2f%%' %(scores.mean()*100))357 358 st.subheader(":mask: Prediction avec SVMLinearSVC")359 clf = svm.LinearSVC(max_iter=5000)360 scores = cross_val_score(clf, x, y,cv=5)361 score.append(scores.mean())362 st.write('The accuration of classification is %.2f%%' %(scores.mean()*100))363 364 365 366 367 368 369 if st.checkbox("Countplot"):370 fig = plt.figure(figsize = (5,5))371 data = load_data('Loan+Approval+Prediction.csv')372 sns.countplot(x="Credit_History",data=data)373 st.pyplot(fig)374 375 if st.checkbox("Scatter"):376 fig = plt.figure(figsize = (8,8))377 data = load_data('Loan+Approval+Prediction.csv')378 sns.scatterplot(x="ApplicantIncome",y="CoapplicantIncome",data=data,hue="Loan_Status")379 st.pyplot(fig)380 381 if choice == "Result":382 st.image("pred.png", width=800)383 384 # plt.barh(y_pos, score, align='center', alpha=0.5)385 # plt.yticks(y_pos, classifier)386 # plt.xlabel('Score')387 # plt.title('Classification Performance')388 # plt.show()389 390 391 # fig = plt.figure(figsize = (5,5))392 # data = load_data('Loan+Approval+Prediction.csv')393 # sns.countplot(x='Self_Employed', data=data, palette = 'Set2')394 # st.pyplot(fig)395 396 if choice == "Machine Learning":397 tab1, tab2, tab3 = st.tabs([":clipboard: Data",":bar_chart: Visualisation", ":mask: :smile: Prediction"])398 uploaded_file = st.sidebar.file_uploader("Upload your input CSV file", type=["csv"])399 if uploaded_file is not None:400 df = load_data(uploaded_file)401 402 with tab1:403 st.subheader("Loaded dataset")404 st.write(df)405 406 with tab2:407 st.subheader("Histplot")408 fig = plt.figure(figsize = (8,8))409 data = load_data(uploaded_file)410 sns.histplot(x = "ApplicantIncome",data=data)411 st.pyplot(fig)412 with tab3:413 data =load_model("class_loan_model")414 prediction = data.predict(df)415 st.subheader('Prediction')416 pp = pd.DataFrame(prediction,columns=["Prediction"])417 ndf = pd.concat([df,pp],axis=1)418 ndf.Prediction.replace(0, "Credit Autorise", inplace = True)419 ndf.Prediction.replace(1, "Credit Non Autorise", inplace = True)420 st.write(ndf)421 def filedownload(df):422 csv = df.to_csv(index=False)423 b64 = base64.b64encode(csv.encode()).decode() # strings <-> bytes conversions424 href = f'<a href="data:file/csv;base64,{b64}" download="pret_predictions.csv">Download CSV File</a>'425 return href426 button = st.button("Download")427 if button:428 st.markdown(filedownload(ndf), unsafe_allow_html=True)429 430 431 432 433 434 435if __name__=='__main__':436 main()