<jupyter_start><jupyter_text>Predictive maintenance Part 3: Inference<jupyter_code>import os
os.environ["CUDA_VISIBLE_DEVICES"]="-1" 
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import numpy as np

import pylab as plt

from tensorflow import keras as K

import vaex
import vaex.ml
import vaex.ml.metrics

import vaex.formatting
vaex.formatting.ARRAY_TRESHHOLD = 1<jupyter_output><empty_output><jupyter_text>Define scoring metrics<jupyter_code>def score_func(df, y_true, y_pred):
    print(f'mean absolute error: {np.round(df.ml.metrics.mean_absolute_error(y_true, y_pred), decimals=3)}')
    print(f'mean squared error: {np.round(df.ml.metrics.mean_squared_error(y_true, y_pred), decimals=3)}')
    print(f'R2 score: {np.round(df.ml.metrics.r2_score(y_true, y_pred), decimals=3)}')
    print()    
    
def r2_keras(y_true, y_pred):
    SS_res =  K.backend.sum(K.backend.square(y_true - y_pred))
    SS_tot = K.backend.sum(K.backend.square(y_true - K.backend.mean(y_true)))
    return (1 - SS_res/(SS_tot + K.backend.epsilon()))<jupyter_output><empty_output><jupyter_text>Load the data<jupyter_code>df_test = vaex.open('./data/data_test.hdf5')
df_test._future()

df_test<jupyter_output><empty_output><jupyter_text>Transform the incoming production data (in this case the test data)This is one with a single easy step!<jupyter_code># This is the transformations on per sample / sequence basis
df_test_trans = df_test.pipeline.load_and_transform('./model/pipeline.json', trusted=True)

df_test_trans
# This is the transformation done to obtain the final predictions on per engine basis
df_test_final = df_test.pipeline.load_and_transform('./model/pipeline_final.json', trusted=True)

df_test_final<jupyter_output><empty_output><jupyter_text>Statistics on the Test set:<jupyter_code>print('Score on per sequence basis on the test set:')
score_func(df_test_trans, 'RUL_target', 'pred_value')

print('Score on the test set - final results!')
score_func(df_test_final, 'RUL_gt', 'RUL_pred')

print('Score on the test set - for engines with a failure within the next 50 cycles.')
score_func(df_test_final[df_test_final.RUL_gt<50], 'RUL_gt', 'RUL_pred')

plt.figure(figsize=(7, 7))
df_test_final.viz.scatter('RUL_gt', 'RUL_pred')
plt.plot([0, 165], [0, 165], lw=1, ls='--', color='black', alpha=0.5)
plt.xlabel('final labels', fontsize=14)
plt.ylabel('final predictions', fontsize=14)
plt.xlim(0, 165)
plt.ylim(0, 165)
plt.show()<jupyter_output>Score on per sequence basis on the test set:
mean absolute error: 26.983
mean squared error: 1540.833
R2 score: 0.435

Score on the test set - final results!
mean absolute error: 10.783
mean squared error: 223.435
R2 score: 0.868

Score on the test set - for engines with a failure within the next 50 cycles.
mean absolute error: 3.333
mean squared error: 25.867
R2 score: 0.791<jupyter_text>Export data - ready for serving!<jupyter_code>df_test_trans.export_hdf5('./data/df_test_trans.hdf5')
df_test_final.export_hdf5('./data/df_test_final.hdf5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12383798140770252, 'fraction_numerical': 0.031208499335989376, 'mean_word_length': 6.156769596199525, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '430979', 'license': 'MIT', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1117, 'n_words': 256, 'path': 'Predictive-Maintenance-Data-Production.ipynb', 'repo_name': 'vaexio/dash-predictive-maintenance-app', 'size': '3012'}