forked from noahgift/Python-MLOps-Cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlib.py
124 lines (94 loc) · 3.28 KB
/
mlib.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""MLOps Library"""
import numpy as np
import pandas as pd
from sklearn.linear_model import Ridge
import joblib
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import logging
logging.basicConfig(level=logging.INFO)
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
def load_model(model="model.joblib"):
"""Grabs model from disk"""
clf = joblib.load(model)
return clf
def data():
df = pd.read_csv("htwtmlb.csv")
return df
def retrain(tsize=0.1, model_name="model.joblib"):
"""Retrains the model
See this notebook: Baseball_Predictions_Export_Model.ipynb
"""
df = data()
y = df["Height"].values # Target
y = y.reshape(-1, 1)
X = df["Weight"].values # Feature(s)
X = X.reshape(-1, 1)
scaler = StandardScaler()
X_scaler = scaler.fit(X)
X = X_scaler.transform(X)
y_scaler = scaler.fit(y)
y = y_scaler.transform(y)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=tsize, random_state=3
)
clf = Ridge()
model = clf.fit(X_train, y_train)
accuracy = model.score(X_test, y_test)
logging.debug(f"Model Accuracy: {accuracy}")
joblib.dump(model, model_name)
return accuracy, model_name
def format_input(x):
"""Takes int and converts to numpy array"""
val = np.array(x)
feature = val.reshape(-1, 1)
return feature
def scale_input(val):
"""Scales input to training feature values"""
df = data()
features = df["Weight"].values
features = features.reshape(-1, 1)
input_scaler = StandardScaler().fit(features)
scaled_input = input_scaler.transform(val)
return scaled_input
def scale_target(target):
"""Scales Target 'y' Value"""
df = data()
y = df["Height"].values # Target
y = y.reshape(-1, 1) # Reshape
scaler = StandardScaler()
y_scaler = scaler.fit(y)
scaled_target = y_scaler.inverse_transform(target)
return scaled_target
def height_human(float_inches):
"""Takes float inches and converts to human height in ft/inches"""
feet = int(round(float_inches / 12, 2)) # round down
inches_left = round(float_inches - feet * 12)
result = f"{feet} foot, {inches_left} inches"
return result
def human_readable_payload(predict_value):
"""Takes numpy array and returns back human readable dictionary"""
height_inches = float(np.round(predict_value, 2))
result = {
"height_inches": height_inches,
"height_human_readable": height_human(height_inches),
}
return result
def predict(weight):
"""Takes weight and predicts height"""
clf = load_model() # loadmodel
np_array_weight = format_input(weight)
scaled_input_result = scale_input(np_array_weight) # scale feature input
scaled_height_prediction = clf.predict(scaled_input_result) # scaled prediction
height_predict = scale_target(scaled_height_prediction)
payload = human_readable_payload(height_predict)
predict_log_data = {
"weight": weight,
"scaled_input_result": scaled_input_result,
"scaled_height_prediction": scaled_height_prediction,
"height_predict": height_predict,
"human_readable_payload": payload,
}
logging.debug(f"Prediction: {predict_log_data}")
return payload