Last active
May 25, 2023 11:20
-
-
Save PulkitS01/a8495a69822ad6df6842612b74d02655 to your computer and use it in GitHub Desktop.
ML_model_deployment_streamlit.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %%writefile app.py | |
| import pickle | |
| import streamlit as st | |
| # loading the trained model | |
| pickle_in = open('classifier.pkl', 'rb') | |
| classifier = pickle.load(pickle_in) | |
| @st.cache() | |
| # defining the function which will make the prediction using the data which the user inputs | |
| def prediction(Gender, Married, ApplicantIncome, LoanAmount, Credit_History): | |
| # Pre-processing user input | |
| if Gender == "Male": | |
| Gender = 0 | |
| else: | |
| Gender = 1 | |
| if Married == "Unmarried": | |
| Married = 0 | |
| else: | |
| Married = 1 | |
| if Credit_History == "Unclear Debts": | |
| Credit_History = 0 | |
| else: | |
| Credit_History = 1 | |
| LoanAmount = LoanAmount / 1000 | |
| # Making predictions | |
| prediction = classifier.predict( | |
| [[Gender, Married, ApplicantIncome, LoanAmount, Credit_History]]) | |
| if prediction == 0: | |
| pred = 'Rejected' | |
| else: | |
| pred = 'Approved' | |
| return pred | |
| # this is the main function in which we define our webpage | |
| def main(): | |
| # front end elements of the web page | |
| html_temp = """ | |
| <div style ="background-color:yellow;padding:13px"> | |
| <h1 style ="color:black;text-align:center;">Streamlit Loan Prediction ML App</h1> | |
| </div> | |
| """ | |
| # display the front end aspect | |
| st.markdown(html_temp, unsafe_allow_html = True) | |
| # following lines create boxes in which user can enter data required to make prediction | |
| Gender = st.selectbox('Gender',("Male","Female")) | |
| Married = st.selectbox('Marital Status',("Unmarried","Married")) | |
| ApplicantIncome = st.number_input("Applicants monthly income") | |
| LoanAmount = st.number_input("Total loan amount") | |
| Credit_History = st.selectbox('Credit_History',("Unclear Debts","No Unclear Debts")) | |
| result ="" | |
| # when 'Predict' is clicked, make the prediction and store it | |
| if st.button("Predict"): | |
| result = prediction(Gender, Married, ApplicantIncome, LoanAmount, Credit_History) | |
| st.success('Your loan is {}'.format(result)) | |
| print(LoanAmount) | |
| if __name__=='__main__': | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| X = train[['Gender', 'Married', 'ApplicantIncome', 'LoanAmount', 'Credit_History']] | |
| y = train.Loan_Status | |
| X.shape, y.shape |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| !pip install -q pyngrok | |
| !pip install -q streamlit | |
| !pip install -q streamlit_ace |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| train = pd.read_csv('train_ctrUa4K.csv') | |
| train.head() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| train.isnull().sum() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| train = train.dropna() | |
| train.isnull().sum() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.ensemble import RandomForestClassifier | |
| model = RandomForestClassifier(max_depth=4, random_state = 10) | |
| model.fit(x_train, y_train) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # saving the model | |
| import pickle | |
| pickle_out = open("classifier.pkl", mode = "wb") | |
| pickle.dump(model, pickle_out) | |
| pickle_out.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| train['Gender']= train['Gender'].map({'Male':0, 'Female':1}) | |
| train['Married']= train['Married'].map({'No':0, 'Yes':1}) | |
| train['Loan_Status']= train['Loan_Status'].map({'N':0, 'Y':1}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pyngrok import ngrok | |
| public_url = ngrok.connect('8501') | |
| public_url |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| !streamlit run app.py &>/dev/null& |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pred_train = model.predict(x_train) | |
| accuracy_score(y_train,pred_train) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.model_selection import train_test_split | |
| x_train, x_cv, y_train, y_cv = train_test_split(X,y, test_size = 0.2, random_state = 10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.metrics import accuracy_score | |
| pred_cv = model.predict(x_cv) | |
| accuracy_score(y_cv,pred_cv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment