Spaces:
Runtime error
Runtime error
saifhmb
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import numpy as np
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import pandas as pd
|
| 6 |
import sklearn
|
|
|
|
| 7 |
from sklearn.compose import make_column_transformer
|
| 8 |
from sklearn.compose import make_column_selector
|
| 9 |
from sklearn.compose import ColumnTransformer
|
|
@@ -30,9 +31,18 @@ y = dataset.iloc[:, -1].values
|
|
| 30 |
dataset = dataset.drop(['RISK'], axis = 1)
|
| 31 |
|
| 32 |
# Encoding the Independent Variables
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Encoding the Dependent Variable
|
| 38 |
le = LabelEncoder()
|
|
@@ -41,13 +51,9 @@ y = le.fit_transform(y)
|
|
| 41 |
# Spliting the datset into Training and Test set
|
| 42 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.15, random_state = 0)
|
| 43 |
|
| 44 |
-
# Feature Scaling
|
| 45 |
-
sc = StandardScaler()
|
| 46 |
-
X_train = sc.fit_transform(X_train)
|
| 47 |
-
X_test = sc.transform(X_test)
|
| 48 |
-
|
| 49 |
# Training Logit Reg Model using the Training set
|
| 50 |
-
|
|
|
|
| 51 |
model.fit(X_train, y_train)
|
| 52 |
|
| 53 |
# Predicting the Test result
|
|
@@ -77,14 +83,10 @@ def welcome():
|
|
| 77 |
|
| 78 |
# defining the function which will make the prediction using the data which the user inputs
|
| 79 |
def prediction(AGE, INCOME, GENDER, MARITAL, NUMKIDS, NUMCARDS, HOWPAID, MORTGAGE, STORECAR, LOANS):
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
from sklearn.compose import make_column_transformer
|
| 83 |
-
from sklearn.compose import make_column_selector
|
| 84 |
-
ct = make_column_transformer((StandardScaler(),make_column_selector(dtype_include=np.number)),[OneHotEncoder(), make_column_selector(dtype_include=object)], remainder = 'passthrough')
|
| 85 |
-
X_test = ct.fit_transform(dataset)
|
| 86 |
-
prediction = model.predict(X_test)
|
| 87 |
print(prediction)
|
|
|
|
| 88 |
|
| 89 |
return prediction
|
| 90 |
|
|
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import pandas as pd
|
| 6 |
import sklearn
|
| 7 |
+
from sklearn.pipeline import Pipeline
|
| 8 |
from sklearn.compose import make_column_transformer
|
| 9 |
from sklearn.compose import make_column_selector
|
| 10 |
from sklearn.compose import ColumnTransformer
|
|
|
|
| 31 |
dataset = dataset.drop(['RISK'], axis = 1)
|
| 32 |
|
| 33 |
# Encoding the Independent Variables
|
| 34 |
+
categoricalColumns = ['GENDER', 'MARITAL', 'HOWPAID', 'MORTGAGE']
|
| 35 |
+
onehot_categorical = OneHotEncoder(handle_unknown='ignore')
|
| 36 |
+
categorical_transformer = Pipeline(steps = [('onehot', onehot_categorical)])
|
| 37 |
|
| 38 |
+
numericalColumns = dataset.select_dtypes(include = np.number).columns
|
| 39 |
+
sc = StandardScaler()
|
| 40 |
+
numerical_transformer = Pipeline(steps = [('scale', sc)])
|
| 41 |
+
|
| 42 |
+
preprocessorForCategoricalColumns = ColumnTransformer(transformers=[('cat', categorical_transformer, categoricalColumns)], remainder ='passthrough')
|
| 43 |
+
preprocessorForAllColumns = ColumnTransformer(transformers=[('cat', categorical_transformer, categoricalColumns),('num',numerical_transformer,numericalColumns)],
|
| 44 |
+
remainder="passthrough")
|
| 45 |
+
X = dataset
|
| 46 |
|
| 47 |
# Encoding the Dependent Variable
|
| 48 |
le = LabelEncoder()
|
|
|
|
| 51 |
# Spliting the datset into Training and Test set
|
| 52 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.15, random_state = 0)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Training Logit Reg Model using the Training set
|
| 55 |
+
classifier = LogisticRegression()
|
| 56 |
+
model = Pipeline(steps = [('preprocessor', preprocessorForCategoricalColumns),('classifier', classifier)])
|
| 57 |
model.fit(X_train, y_train)
|
| 58 |
|
| 59 |
# Predicting the Test result
|
|
|
|
| 83 |
|
| 84 |
# defining the function which will make the prediction using the data which the user inputs
|
| 85 |
def prediction(AGE, INCOME, GENDER, MARITAL, NUMKIDS, NUMCARDS, HOWPAID, MORTGAGE, STORECAR, LOANS):
|
| 86 |
+
X = pd.DataFrame([[AGE, INCOME, GENDER, MARITAL, NUMKIDS, NUMCARDS, HOWPAID, MORTGAGE, STORECAR, LOANS]], columns = ['AGE', 'INCOME', 'GENDER', 'MARITAL', 'NUMKIDS', 'NUMCARDS', 'HOWPAID', 'MORTGAGE', 'STORECAR', 'LOANS'])
|
| 87 |
+
prediction = model.predict(X)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
print(prediction)
|
| 89 |
+
return prediction
|
| 90 |
|
| 91 |
return prediction
|
| 92 |
|