chanelcolgate / hydroelectric-project

0 stars 0 forks source link

Demo machine learning cho việc dự đoán giá #23

Open chanelcolgate opened 2 years ago

chanelcolgate commented 2 years ago

Mô tả

def download_dataset(url, LOCAL_FILE_NAME): urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) c = urllib3.PoolManager() with c.request("GET", url, preload_content=False) as res, open( LOCAL_FILE_NAME, "wb" ) as out_file: shutil.copyfileobj(res, out_file) logging.info("Dowload completed.")

logging.info("Started download script") URL = 'https://raw.githubusercontent.com/chanelcolgate/hydroelectric-project/19eb96f0d37b6dda92cceb21fde531ef9ff1f6c8/data/SMP_09-2021.csv' LOCAL_FILE_NAME = 'SMP_09-2021.csv' download_dataset(URL, LOCAL_FILE_NAME)

- Unzip model folder

!unzip "/content/saved_models-07-10.zip" -d "/"

- Predict
```python
import tensorflow as tf
from pandas import read_csv
from numpy import array
from math import sqrt
from sklearn.metrics import mean_squared_error
import numpy as np

# forecast with the fit model
def model_predict_2(model, history, n_input, n_features=1):
    # shape input for model
    x_input = array(history[-n_input:]).reshape((1, n_input, n_features))
    # make forecast
    yhat = model.predict(x_input, verbose=0)
    # correct forecast if it was differenced
    return yhat[0]

# root mean squared error or rmse
def measure_rmse(actual, predicted):
    return sqrt(mean_squared_error(actual, predicted))

model_path = '/content/saved_models/1633610713'
model = tf.keras.models.load_model(model_path)

series = read_csv(LOCAL_FILE_NAME, header=0, usecols=['SMP'])
data = series.values
# du doan 1 ngay, 0 ngay 1, 48 ngay 2
numberOfPredict = 1
periodOFPredict = numberOfPredict * 48
# dung ngay 5, 6 du doan ngay 7
indexOfPredict = 288 + periodOFPredict
actual = array(data[288:indexOfPredict]).reshape((periodOFPredict, 1)) # du lieu ngay 7 de so sanh
test = data[192:288] # du lieu ngay 5, 6 thang 9 nam 2021
test = array(test).reshape((1, 96))
predictions = list()
for i in range(0, periodOFPredict):
    prediction = model.predict(test, verbose=0)
    test = test.tolist()[0]
    test.extend(prediction.tolist()[0])
    test = array(test[-96:]).reshape((1, 96))
    predictions.extend(prediction.tolist()[0])
predictions = [1503.5 if num > 1503.5 else num for num in predictions] # gioi han tren
predictions = [0.0 if num < 0.0 else num for num in predictions] # gioi han duoi
predictions = [round(num, 2) for num in predictions]
measure_rmse(actual, predictions)

class Smp: def init( self, fileName: str = "Bảng dự kiến chào giá T9.xlsx", sheetName: List[str] = ['07-09'], index: int = 0, A: List[str] = ['0', '5.5'], n: int = 2, priceContract : float = 917.220, expectedPrice: List[float] = [0.0]) -> None: df = read_excel( fileName, sheet_name = sheetName, skiprows = 3, nrows = 48, usecols = "A:J") self.data = df[sheetName[index]] self.A = A self.n = n self.priceContract = priceContract self.expectedPrice = expectedPrice

def strings(self) -> Any:
    index_of = {x: i for i, x in enumerate(self.A)}
    s = [self.A[0]] * self.n
    while True:
        yield ','.join(s)
        for i in range(1, self.n + 1):
            if s[-i] == self.A[-1]:
                s[-i] = self.A[0]
            else:
                s[-i] = self.A[index_of[s[-i]] + 1]
                break
        else:
            break

def revenue(
        self,
        outputContract: Any,
        priceContract: float,
        expectedPrice: Any,
        expectedOutput: Any,
        priceCan: Any) -> Tuple[float, float]:
    expectedRevenue = outputContract*(priceContract - expectedPrice - priceCan) + \
                      expectedOutput*(expectedPrice + priceCan)
    expectedRevenueContract = expectedOutput*priceContract
    if expectedRevenueContract.sum():
        result = expectedRevenue.sum()/expectedRevenueContract.sum() * 100 - 100
    else:
        result = -100.0
    return (expectedRevenueContract.sum(), result)

@property
def scores(self) -> List[Any]:
    configs = self.strings()
    return sorted((
            self.revenue(
                outputContract = self.data['Sản lượng hợp đồng (Qc)'].values,
                priceContract = self.priceContract,
                expectedPrice = array(self.expectedPrice),
                expectedOutput = array([float(e) for e in row.split(',') for _ in (0, 1)]),
                priceCan = self.data['Giá CAN'].values
            ),
            str([float(e) for e in row.split(',') for _ in (0, 1)])
        )
        for row in tqdm(configs, total=pow(2, self.n))
    )
- Import smp and predict to expected output. Ở đây đầu vào là ngày 7 tháng 9 và công suất là 11 MW
```python
from smp import Smp

smp = Smp(expectedPrice = predictions, sheetName = ['07-09'], A = ['0', '11'], n=24)
scores = smp.scores