109 lines
2.7 KiB
Python
109 lines
2.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Fri Dec 9 09:31:31 2022
|
|
|
|
@author: Daniel Häfliger
|
|
|
|
|
|
This file shows an example of how to use the Influxclient and Influxdataframe.
|
|
The example try to fit an prediction of a temperature of a specific measure
|
|
with time and a given prediction of the temperature as features with time series
|
|
forecasting depending ond them.
|
|
|
|
There is no data cleaning ect. in this example, its just to show how the lib
|
|
works...
|
|
|
|
Be aware that you need to add a influx- and weatherapi-token and aviable datas
|
|
to the config file. Otherwise the example, as the lib to, wont work.
|
|
|
|
|
|
"""
|
|
import pandas as pd
|
|
import Influxdataframe as Idf
|
|
import Influxclient as I
|
|
import seaborn as sns
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.preprocessing import MinMaxScaler
|
|
from sklearn.metrics import r2_score
|
|
from sklearn.neural_network import MLPRegressor
|
|
|
|
|
|
target_variable = "W_AT_5M_0"
|
|
interval = 60
|
|
valuesince = 10
|
|
prediction_hours = 24
|
|
|
|
shift_number=4
|
|
|
|
df = Idf.Get_Df()
|
|
|
|
df = df[::12]
|
|
|
|
pred =I.GetPrediction(prediction_hours, interval, "temp_c")
|
|
|
|
|
|
data = {
|
|
|
|
"y_r": pred[0],
|
|
"y_i": pred[1],
|
|
"d_r": pred[2],
|
|
"d_i": pred[3],
|
|
|
|
"P_AT_5M_0": pred[5],
|
|
}
|
|
|
|
|
|
preddf = pd.DataFrame(data)
|
|
|
|
df = pd.concat([df, preddf], axis=0).reset_index(drop=True)
|
|
|
|
|
|
df = Idf.ShiftTarget(df, shift_number, target_variable)
|
|
|
|
|
|
|
|
df_predict = df.dropna()
|
|
|
|
train_df, test_df = train_test_split(df_predict, test_size=0.3, random_state=42)
|
|
|
|
scaler = MinMaxScaler()
|
|
|
|
train_df = pd.DataFrame(scaler.fit_transform(
|
|
train_df), columns=train_df.columns, index=train_df.index)
|
|
test_df = pd.DataFrame(scaler.transform(
|
|
test_df), columns=test_df.columns, index=test_df.index)
|
|
|
|
y = train_df[[target_variable+str(0)]]
|
|
|
|
X = train_df
|
|
X.pop(target_variable+str(0))
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(
|
|
X, y, test_size=0.2, random_state=42)
|
|
|
|
dt = MLPRegressor(activation='relu')
|
|
dt.fit(X_train, y_train)
|
|
|
|
print(r2_score(y_test, dt.predict(X_test)))
|
|
|
|
|
|
|
|
for i in range(int(prediction_hours*60/interval)):
|
|
preddf = pd.DataFrame(df.iloc[(int(df_predict.shape[0]+shift_number)-1+i)]).transpose()
|
|
preddf = pd.DataFrame(scaler.transform(
|
|
preddf), columns=preddf.columns, index=preddf.index)
|
|
preddf.pop(target_variable+str(0))
|
|
at = dt.predict(preddf)
|
|
preddf[target_variable+str(0)]=at
|
|
newval = pd.DataFrame(scaler.inverse_transform(preddf))
|
|
df.at[(int(df_predict.shape[0]+shift_number)-1+i), target_variable+str(0)] = newval.at[0, 5]
|
|
|
|
for j in range(shift_number):
|
|
df.at[int(df_predict.shape[0]+shift_number)+i, target_variable+str(j+1)] = df.at[int(df_predict.shape[0]+shift_number)-1+i, target_variable+str(j)]
|
|
|
|
|
|
|
|
|
|
sns.lineplot(df.index, df[target_variable+str(0)])
|
|
|