Last active
January 27, 2020 14:43
-
-
Save cosmic-cortex/6ee14ef6b4d34983b2f59f3700180820 to your computer and use it in GitHub Desktop.
Implementation of the machine learning model for the API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import joblib | |
import numpy as np | |
from pathlib import Path | |
from sklearn.ensemble import RandomForestRegressor | |
from sklearn.datasets import load_boston | |
class Model: | |
def __init__(self, model_path: str = None): | |
self._model = None | |
self._model_path = model_path | |
self.load() | |
def train(self, X: np.ndarray, y: np.ndarray): | |
self._model = RandomForestRegressor() | |
self._model.fit(X, y) | |
return self | |
def predict(self, X: np.ndarray) -> np.ndarray: | |
return self._model.predict(X) | |
def save(self): | |
if self._model is not None: | |
joblib.dump(self._model, self._model_path) | |
else: | |
raise TypeError("The model is not trained yet, use .train() before saving") | |
def load(self): | |
try: | |
self._model = joblib.load(self._model_path) | |
except: | |
self._model = None | |
return self | |
model_path = Path(__file__).parent / "model.joblib" | |
n_features = load_boston(return_X_y=True)[0].shape[1] | |
model = Model(model_path) | |
def get_model(): | |
return model | |
if __name__ == "__main__": | |
X, y = load_boston(return_X_y=True) | |
model.train(X, y) | |
model.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment