-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
30 lines (23 loc) · 1.05 KB
/
Copy pathmodel.py
File metadata and controls
30 lines (23 loc) · 1.05 KB
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
import pandas as pd
from numpy import ravel
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.svm import SVR
import numpy as np
class BasicModel:
models = {'DecisionTreeRegressor': DecisionTreeRegressor,
'RandomForestRegressor': RandomForestRegressor,
'LinearRegression': LinearRegression,
'Ridge': Ridge,
'Lasso': Lasso,
'SVR': SVR}
def __init__(self, ingredients, prices, model_class_name, *args, **kwargs):
self.model = self.models[model_class_name](*args, **kwargs, random_state=1)
self.model.fit(ingredients, ravel(prices))
def predict(self, val_ingredients: pd.DataFrame):
return self.model.predict(val_ingredients)
def mae(self, val_price: pd.DataFrame, val_ingredients: pd.DataFrame):
return abs(np.array(val_price) - self.predict(val_ingredients)).mean()