saifhmb commited on
Commit
e80acd3
·
unverified ·
1 Parent(s): de010b5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # importing libraries
2
+ from datasets import load_dataset, load_dataset_builder
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import pandas as pd
6
+ import sklearn
7
+ from sklearn.compose import ColumnTransformer
8
+ from sklearn.model_selection import train_test_split
9
+ from sklearn.preprocessing import OneHotEncoder, LabelEncoder, StandardScaler
10
+ from sklearn.linear_model import LogisticRegression
11
+ from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, accuracy_score, precision_score, recall_score, classification_report
12
+ from transformers import Trainer, TrainingArguments
13
+ from skops import hub_utils
14
+ import pickle
15
+ from skops.card import Card, metadata_from_config
16
+ from pathlib import Path
17
+ from tempfile import mkdtemp, mkstemp
18
+ import streamlit as st
19
+ from PIL import Image
20
+
21
+ # Loading the dataset
22
+ dataset_name = "saifhmb/CreditCardRisk"
23
+ dataset = load_dataset(dataset_name, split = 'train')
24
+ dataset = pd.DataFrame(dataset)
25
+ X = dataset.iloc[:, :-1].values
26
+ y = dataset.iloc[:, -1].values
27
+
28
+ # Encoding the Independent Variables
29
+ ct = ColumnTransformer(transformers = [('encoder', OneHotEncoder(), [2, 3, 6, 7])], remainder = 'passthrough')
30
+ X = np.array(ct.fit_transform(X))
31
+
32
+ # Encoding the Dependent Variable
33
+ le = LabelEncoder()
34
+ y = le.fit_transform(y)
35
+
36
+ # Spliting the datset into Training and Test set
37
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.15, random_state = 0)
38
+
39
+ # Feature Scaling
40
+ sc = StandardScaler()
41
+ X_train = sc.fit_transform(X_train)
42
+ X_test = sc.transform(X_test)
43
+
44
+ # Training Logit Reg Model using the Training set
45
+ model = LogisticRegression()
46
+ model.fit(X_train, y_train)
47
+
48
+ # Predicting the Test result
49
+ y_pred = model.predict(X_test)
50
+
51
+ # Making the Confusion Matrix and evaluating performance
52
+ cm = confusion_matrix(y_pred, y_test, labels=model.classes_)
53
+ display_labels = np.array(['bad loss', 'bad profit', 'good risk'])
54
+ disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=display_labels)
55
+ disp.plot()
56
+ plt.show()
57
+ acc = accuracy_score(y_test, y_pred)
58
+ ps = precision_score(y_test, y_pred, average ='micro')
59
+ rs = recall_score(y_test, y_pred, average ='micro')
60
+
61
+ # Pickling the model
62
+ pickle_out = open("model.pkl", "wb")
63
+ pickle.dump(model, pickle_out)
64
+ pickle_out.close()
65
+
66
+ # Loading the model to predict on the data
67
+ pickle_in = open('model.pkl', 'rb')
68
+ model = pickle.load(pickle_in)
69
+
70
+ def welcome():
71
+ return 'welcome all'
72
+
73
+ # defining the function which will make the prediction using the data which the user inputs
74
+ def prediction(AGE, INCOME, GENDER, MARITAL, NUMKIDS, NUMCARDS, HOWPAID, MORTGAGE, STORECAR, LOANS):
75
+ prediction = model.predict(sc.transform([[AGE, INCOME, GENDER, MARITAL, NUMKIDS, NUMCARDS, HOWPAID, MORTGAGE, STORECAR, LOANS]]))
76
+ print(prediction)
77
+ return prediction
78
+
79
+ # this is the main function in which we define our webpage
80
+ def main():
81
+ # giving the webpage a title
82
+ st.title("Credit Card Risk Assessment ML App")
83
+ st.header("Model Description", divider = "gray")
84
+ multi = '''This is a logistic regression model trained on customers' credit card risk dataset in a bank using sklearn library.
85
+ The model predicts whether a customer is worth issuing a credit card or not.
86
+ For more details on the model please refer to the model card at https://huggingface.co/saifhmb/Credit-Card-Risk-Model
87
+ '''
88
+ st.markdown(multi)
89
+ st.markdown("To determine whether a customer is worth issuing a credit card or not, please **ENTER** the AGE INCOME, GENDER, MARITAL, NUMKIDS, NUMCARDS, HOWPAID, MORTGAGE, STORECAR, and LOANS:")
90
+ col1, col2, col3 = st.columns(3)
91
+ with col1:
92
+ AGE = st.number_input("AGE")
93
+ with col2:
94
+ INCOME = st.number_input("INCOME")
95
+ with col3:
96
+ GENDER = st.text_input("GENDER (Please enter 'm' for male and 'f' for female)")
97
+
98
+ col4, col5, col6 = st.columns(3)
99
+ with col4:
100
+ MARITAL = st.text_input("MARITAL STATUS (Please enter one of the following options: 'single', 'married', or 'divsepwid')")
101
+ with col5:
102
+ NUMKIDS = st.number_input("Number of dependent children")
103
+ with col6:
104
+ NUMCARDS = st.number_input("Number of credit cards excluding store credit cards")
105
+
106
+ col7, col8, col9 =st.columns(3)
107
+ with col7:
108
+ HOWPAID = st.text_input("How often is customer paid by employer (weekly or monthly)")
109
+ with col8:
110
+ MORTGAGE = st.text_input("Does customer have a mortgage? please enter 'y' for yes or 'n' for no")
111
+ with col9:
112
+ STORECAR = st.number_input("Number of store credit cards")
113
+
114
+ LOANS = st.number_input("Number of outstanding loans")
115
+ result = ""
116
+ if st.button("Predict"):
117
+ result = prediction(AGE, INCOME, GENDER, MARITAL, NUMKIDS, NUMCARDS, HOWPAID, MORTGAGE, STORECAR, LOANS)
118
+ if result == 0:
119
+ st.success("The output is {}".format(result) + " which falls under 'bad loss' and thus the customer is NOT worth issuing a credit card")
120
+ if result == 1:
121
+ st.success("The output is {}".format(result) + " which falls under 'bad profit' and thus the customer MAYBE worth issuing a credit card")
122
+ if result == 2:
123
+ st.success("The output is {}".format(result) + " which falls under 'good risk' and thus the customer worth issuing a credit card")
124
+
125
+ if __name__=='__main__':
126
+ main()
127
+
128
+