The steps for this recipe are as follows:
- Import the libraries:
from pyod.models.auto_encoder import AutoEncoder
from pyod.utils.data import generate_data
from pyod.utils.data import evaluate_print
import numpy as np
import pickle
- Load text files into our notebooks using NumPy arrays:
X_train = np.loadtxt('X_train.txt', dtype=float)
y_train = np.loadtxt('y_train.txt', dtype=float)
X_test = np.loadtxt('X_test.txt', dtype=float)
y_test = np.loadtxt('y_test.txt', dtype=float)
- Use the autoencoder algorithm to fix the model to the dataset:
clf = AutoEncoder(epochs=30)
clf.fit(X_train)
- Get the prediction scores:
y_test_pred = clf.predict(X_test) # outlier labels (0 or 1)
y_test_scores = clf.decision_function(X_test) # outlier scores
evaluate_print('AutoEncoder', y_test, y_test_scores)
- Save the model:
pickle.dump( clf, open( "autoencoder.p", "wb" ) )