Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- model.joblib +3 -0
- requirements.txt +1 -0
- train.py +80 -0
model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6c3c382c7233f0463a9c2698c7190fa6a89f2704433ae79735d9a6a1acfb5529
|
| 3 |
+
size 8439
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
scikit-learn==1.2.2
|
train.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
| 6 |
+
from sklearn.compose import make_column_transformer
|
| 7 |
+
from sklearn.impute import SimpleImputer
|
| 8 |
+
from sklearn.pipeline import Pipeline
|
| 9 |
+
from sklearn.pipeline import make_pipeline
|
| 10 |
+
|
| 11 |
+
from sklearn.model_selection import train_test_split, RandomizedSearchCV
|
| 12 |
+
|
| 13 |
+
from sklearn.linear_model import LogisticRegression
|
| 14 |
+
from sklearn.metrics import accuracy_score, classification_report
|
| 15 |
+
|
| 16 |
+
data_df = pd.read_csv("Bank_Telemarketing.csv")
|
| 17 |
+
|
| 18 |
+
target = 'subscribed'
|
| 19 |
+
numerical_features = ['Age', 'Duration(Sec)', 'CC Contact Freq', 'Days Since PC','PC Contact Freq']
|
| 20 |
+
categorical_features = ['Job', 'Marital Status', 'Education', 'Defaulter', 'Home Loan',
|
| 21 |
+
'Personal Loan', 'Communication Type', 'Last Contacted', 'Day of Week',
|
| 22 |
+
'PC Outcome']
|
| 23 |
+
|
| 24 |
+
print("Creating data subsets")
|
| 25 |
+
|
| 26 |
+
X = data_df[numerical_features + categorical_features]
|
| 27 |
+
y = data_df[target]
|
| 28 |
+
|
| 29 |
+
Xtrain, Xtest, ytrain, ytest = train_test_split(
|
| 30 |
+
X, y,
|
| 31 |
+
test_size=0.2,
|
| 32 |
+
random_state=42
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
numerical_pipeline = Pipeline([
|
| 36 |
+
('imputer', SimpleImputer(strategy='median')),
|
| 37 |
+
('scaler', StandardScaler())
|
| 38 |
+
])
|
| 39 |
+
|
| 40 |
+
categorical_pipeline = Pipeline([
|
| 41 |
+
('imputer', SimpleImputer(strategy='most_frequent')),
|
| 42 |
+
('onehot', OneHotEncoder(handle_unknown='ignore'))
|
| 43 |
+
])
|
| 44 |
+
|
| 45 |
+
preprocessor = make_column_transformer(
|
| 46 |
+
(numerical_pipeline, numerical_features),
|
| 47 |
+
(categorical_pipeline, categorical_features)
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
model_logistic_regression = LogisticRegression(n_jobs=-1)
|
| 51 |
+
|
| 52 |
+
print("Estimating Best Model Pipeline")
|
| 53 |
+
|
| 54 |
+
model_pipeline = make_pipeline(
|
| 55 |
+
preprocessor,
|
| 56 |
+
model_logistic_regression
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
param_distribution = {
|
| 60 |
+
"logisticregression__C": [0.001, 0.01, 0.1, 0.5, 1, 5, 10]
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
rand_search_cv = RandomizedSearchCV(
|
| 64 |
+
model_pipeline,
|
| 65 |
+
param_distribution,
|
| 66 |
+
n_iter=3,
|
| 67 |
+
cv=3,
|
| 68 |
+
random_state=42
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
rand_search_cv.fit(Xtrain, ytrain)
|
| 72 |
+
|
| 73 |
+
print("Logging Metrics")
|
| 74 |
+
print(f"Accuracy: {rand_search_cv.best_score_}")
|
| 75 |
+
|
| 76 |
+
print("Serializing Model")
|
| 77 |
+
|
| 78 |
+
saved_model_path = "model.joblib"
|
| 79 |
+
|
| 80 |
+
joblib.dump(rand_search_cv.best_estimator_, saved_model_path)
|