101 lines
2.5 KiB
Python
101 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Fri Dec 9 08:51:35 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.
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Fri Dec 2 08:51:19 2022
|
|
|
|
@author: Daniel Häfliger
|
|
"""
|
|
import pandas as pd
|
|
import Influxdataframe as Idf
|
|
import Influxclient as I
|
|
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
|
|
|
|
|
|
# Get a dataframe from the influxDB with the specified values in the config.xml
|
|
df = Idf.Get_Df()
|
|
|
|
|
|
# Do a train test split and scale the datas
|
|
train_df, test_df = train_test_split(df, 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)
|
|
|
|
X = test_df[["y_r", "y_i", "d_r", "d_i", "P_AT_5M_0"]]
|
|
y = test_df[["W_AT_5M_0"]]
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(
|
|
X, y, test_size=0.2, random_state=42)
|
|
|
|
|
|
# Train data and print r2
|
|
dt = MLPRegressor(activation='relu')
|
|
dt.fit(X_train, y_train)
|
|
print(r2_score(y_test, dt.predict(X_test)))
|
|
|
|
|
|
# Get a df for the current prediciton with values from weather api
|
|
pred = I.GetPrediction(48, 5, "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],
|
|
"W_AT_5M_0": pred[5],
|
|
}
|
|
preddf = pd.DataFrame((data))
|
|
|
|
|
|
preddf = pd.DataFrame(scaler.transform(
|
|
preddf), columns=preddf.columns, index=preddf.index)
|
|
|
|
preddf = preddf[["y_r", "y_i", "d_r", "d_i", "P_AT_5M_0"]]
|
|
|
|
|
|
# Predict temprature for the values returned by the api
|
|
ATpred = dt.predict(preddf)
|
|
Prog24 = pd.DataFrame(ATpred)
|
|
|
|
df_all_cols = pd.concat([preddf, Prog24], axis=1)
|
|
|
|
Prog24 = scaler.inverse_transform(df_all_cols)
|
|
|
|
# Rewrite datas to get it into a df
|
|
data = {
|
|
|
|
"W_AT_5M_0": Prog24[:, 5].tolist(),
|
|
"Timestamp": pred[4],
|
|
|
|
}
|
|
|
|
prognose = pd.DataFrame(data)
|
|
prognose.plot(x='Timestamp', y='W_AT_5M_0')
|
|
|
|
|