2020-12-03 00:20:50 +01:00
|
|
|
# See LICENSE for licensing information.
|
|
|
|
|
#
|
|
|
|
|
# Copyright (c) 2016-2019 Regents of the University of California and The Board
|
|
|
|
|
# of Regents for the Oklahoma Agricultural and Mechanical College
|
|
|
|
|
# (acting for and on behalf of Oklahoma State University)
|
|
|
|
|
# All rights reserved.
|
|
|
|
|
#
|
|
|
|
|
|
2020-11-20 21:40:04 +01:00
|
|
|
import os
|
|
|
|
|
from sklearn.linear_model import LinearRegression
|
2020-12-03 00:20:50 +01:00
|
|
|
from .analytical_util import *
|
|
|
|
|
from globals import OPTS
|
|
|
|
|
import debug
|
|
|
|
|
|
|
|
|
|
relative_data_path = "/sim_data"
|
|
|
|
|
data_filename = "data.csv"
|
|
|
|
|
tech_path = os.environ.get('OPENRAM_TECH')
|
|
|
|
|
data_dir = tech_path+'/'+OPTS.tech_name+relative_data_path
|
|
|
|
|
|
|
|
|
|
class linear_regression():
|
|
|
|
|
|
2020-12-07 22:11:04 +01:00
|
|
|
def __init__(self):
|
|
|
|
|
self.model = None
|
|
|
|
|
|
2020-12-07 23:36:01 +01:00
|
|
|
def get_prediction(self, model_inputs):
|
2020-12-03 00:20:50 +01:00
|
|
|
|
|
|
|
|
file_path = data_dir +'/'+data_filename
|
2020-12-10 00:03:04 +01:00
|
|
|
scaled_inputs = np.asarray([scale_input_datapoint(model_inputs, file_path)])
|
2020-12-07 23:22:53 +01:00
|
|
|
|
2020-12-10 00:03:04 +01:00
|
|
|
features, labels = get_scaled_data(file_path)
|
2020-12-07 23:22:53 +01:00
|
|
|
self.train_model(features, labels)
|
2020-12-07 23:36:01 +01:00
|
|
|
scaled_pred = self.model_prediction(scaled_inputs)
|
2020-12-10 00:03:04 +01:00
|
|
|
pred = unscale_data(scaled_pred.tolist(), file_path)
|
2020-12-07 23:22:53 +01:00
|
|
|
debug.info(1,"Unscaled Prediction = {}".format(pred))
|
|
|
|
|
return pred
|
|
|
|
|
|
|
|
|
|
def train_model(self, features, labels):
|
|
|
|
|
"""
|
|
|
|
|
Supervised training of model.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.model = LinearRegression()
|
|
|
|
|
self.model.fit(features, labels)
|
|
|
|
|
|
|
|
|
|
def model_prediction(self, features):
|
|
|
|
|
"""
|
|
|
|
|
Have the model perform a prediction and unscale the prediction
|
|
|
|
|
as the model is trained with scaled values.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
pred = self.model.predict(features)
|
|
|
|
|
debug.info(1, "pred={}".format(pred))
|
|
|
|
|
return pred
|
|
|
|
|
|