Add prediction script

This commit is contained in:
Paul Schaub 2018-09-09 17:25:43 +02:00
parent e97ba96dd4
commit c6261134c9
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
7 changed files with 52 additions and 9 deletions

View file

@ -7,7 +7,6 @@ import os.path
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "pywatts.db") db_path = os.path.join(BASE_DIR, "pywatts.db")
print(db_path)
db = SqliteExtDatabase(db_path) db = SqliteExtDatabase(db_path)

View file

@ -1,6 +1,4 @@
import random import random
import itertools
from pywatts import db
def split(data, k): def split(data, k):
@ -18,7 +16,7 @@ def split(data, k):
data_list = data['dc'].tolist() data_list = data['dc'].tolist()
# Each sample has 337 elements # Each sample has 337 elements
samples = [data_list[i:i+337] for i in range(0, len(data_list) - 337, 337)] samples = [data_list[i:i+337] for i in range(0, len(data_list) - 337)]
# Randomly shuffle samples # Randomly shuffle samples
random.shuffle(samples) random.shuffle(samples)

View file

@ -1,5 +1,3 @@
import pandas
import numpy as np
import tensorflow as tf import tensorflow as tf

View file

@ -0,0 +1,30 @@
import os
import sys
import tensorflow as tf
import pywatts.db
from pywatts.routines import *
# get rid of TF debug message
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
if len(sys.argv) != 3:
print("Usage: python predict_for_json.py 24h|1h <file.json>")
exit(1)
type = sys.argv[1] # '1h' or '24h'
json_file = sys.argv[2] # json file
queries = input_queries(json_file)
feature_col = [tf.feature_column.numeric_column(str(idx)) for idx in range(336)]
n = pywatts.neural.Net(feature_cols=feature_col)
predictions = []
for query in queries:
if type == '1h':
predictions.extend(predict(n, query).astype('Float64').tolist())
else:
predictions.append(predict24h(n, query))
print(predictions)

View file

@ -36,6 +36,18 @@ def input_query(json_str, idx=0):
'wind': tmp_df['wind'][idx]} 'wind': tmp_df['wind'][idx]}
) )
def input_queries(json_str):
tmp_df = pandas.read_json(json_str)
queries = []
for i in range(len(tmp_df)):
queries.append(pandas.DataFrame.from_dict(
{'dc': tmp_df['dc'][i],
'temp': tmp_df['temp'][i],
'wind': tmp_df['wind'][i]}
))
return queries
def input_result(json_str, idx=0): def input_result(json_str, idx=0):
tmp_df = pandas.read_json(json_str) tmp_df = pandas.read_json(json_str)
@ -81,7 +93,7 @@ def predict24h(nn, X_pred):
# Remove first value and append predicted value # Remove first value and append predicted value
del input['dc'][0] del input['dc'][0]
input['dc'].append(predictions[-1]) input['dc'].append(predictions[-1])
print("Prediction for hour %d/%d" % (i+1, 24)) # print("Prediction for hour %d/%d" % (i+1, 24))
return predictions return predictions
@ -94,3 +106,9 @@ def eval_prediction(prediction, result):
print("The Median Absolute Error: %.2f volt dc" % median_absolute_error( print("The Median Absolute Error: %.2f volt dc" % median_absolute_error(
result, prediction)) result, prediction))
def jsonify(predictions):
json_out = "["
for v in predictions:
json_out += "[" + str(v) + "],"
json_out = json_out[:-1] + "]"
return json_out

View file

@ -1,5 +1,5 @@
import peewee
import tensorflow as tf import tensorflow as tf
import pywatts.db import pywatts.db
from pywatts import kcross from pywatts import kcross

View file

@ -1,5 +1,5 @@
import peewee
import tensorflow as tf import tensorflow as tf
import pywatts.db import pywatts.db
from pywatts.routines import * from pywatts.routines import *