Tryag File Manager
Home
||
Turbo Force
||
B-F Config_Cpanel
Current Path :
/
paip
/
script
/
weight
/
Or
Select Your Path :
Upload File :
New :
File
Dir
//paip/script/weight/weightRegressor.py
''' Created on Fri Dec 10 10:35 @author: han ''' import pandas as pd import pickle import sys,os import glob from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures HOME_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir) DATA_DIR = 'data' MODULE_DIR = 'util' OUTPUT_DIR = 'out' # set sys path to import PyDBconnector sys.path.append(os.path.join(HOME_PATH, MODULE_DIR)) from PyDBconnector import PyDBconnector class WeightRegressor(): _pixelWeightModel = None _realWeightModel = None _conn = None def __init__(self, db_host_ip = '127.0.0.1'): with open('model/pixelWeightModel.pkl', 'rb') as f: self._pixelWeightModel = pickle.load(f) with open('model/realWeightModel.pkl', 'rb') as f: self._realWeightModel = pickle.load(f) self._conn = PyDBconnector(host = db_host_ip) def getpixelWeight(self, pixelvalues): ''' after load model, update model :return: ''' return self._pixelWeightModel.predict(pixelvalues) def getrealWeight(self, dayAge): ''' after load model, update model :return: ''' return self._realWeightModel.predict(dayAge) def writeModel(self, isPixelWeightModel): ''' write updated model to disk as pickle format :return: ''' if isPixelWeightModel : with open('model/pixelWeightModel.pkl', 'wb') as f: pickle.dump(self._pixelWeightModel, f) else : with open('model/realWeightModel', 'wb') as f : pickle.dump(self._realWeightModel, f) def predict(self): ''' return predicted values :return: ''' pass def updatepixelWeightModel(self): # check if model-update is needed pass def updaterealWeightModel(self): # check if model-update is needed sql_str = f'''select medianWeight,medianPixel \ from tbl_weight_stats \ where 1=1 order by create_time desc \ ''' rows = self._conn.select_from_db(sql_str) X, y = rows[['medianPixel']], rows['medianWeight'] line_fitter = LinearRegression() line_fitter.fit(X, y) with open('model/chery_linear_model.pkl', 'wb') as f: pickle.dump(line_fitter, f) @classmethod def initCreateModel(self): # init pixel to weight model raw_df = pd.read_csv(glob.glob('../util/real_weight.csv')[0]) X, y = raw_df[['no_distortion_pixel_mean']], raw_df['real_weight'] line_fitter = LinearRegression() line_fitter.fit(X, y) with open('model/pixelWeightModel.pkl', 'wb') as f: pickle.dump(line_fitter, f) # init weight trend model models_df = pd.read_csv(glob.glob('../util/dayAge_weight_trend.csv')[0]) poly_feature = PolynomialFeatures(degree=2, include_bias=False) A_poly = poly_feature.fit_transform(models_df.drop('predWeightMean', 1)) lin_reg = LinearRegression() # LinearRegression 객체 생성 lin_reg.fit(A_poly, models_df['predWeightMean']) with open('model/realWeightModel.pkl', 'wb') as f: pickle.dump(line_fitter, f) if __name__ == '__main__': # init compile base model - just once #WeightRegressor.initCreateModel() # update weight linear model WeightRegressor().updaterealWeightModel()