-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
70 lines (53 loc) · 1.45 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import joblib
import pandas as pd
import uvicorn
from catboost import CatBoostClassifier
from sklearn.feature_extraction import DictVectorizer
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
from utils import drop_corr_features, change_feature
class Campaigns(BaseModel):
age: int
job: str
marital: str
education: str
default: str
housing: str
loan: str
contact: str
month: str
duration: str
day_of_week: str
campaign: int
pdays: int
previous: int
poutcome: str
emp_var_rate: int
cons_price_idx: int
cons_conf_idx: int
euribor3m: int
nr_employed: int
app = FastAPI()
# get model and vectorizer
dv = joblib.load("./dictvectorizer.joblib")
model = joblib.load("./catboost.model")
@app.post("/predict/")
def predict(user_input: Campaigns):
# change the base model to dataframe
df_test = pd.DataFrame([jsonable_encoder(user_input)])
df_test = change_feature(df_test)
df_test = drop_corr_features(df_test)
df_test = df_test.to_dict("records")
X_test = dv.transform(df_test)
y_pred = model.predict(X_test)[0]
if y_pred == 0:
return {
"Prediction": "Customer won't subscribe"
}
return {
"Prediction": "Customer will subscribe"
}
if __name__ == "__main__":
uvicorn.run("predict:app", debug=True,
host='localhost', port=5000, reload=True)