Python use a custom learner

I want to use a custom class as learner. 

I have a python learner block with the following code:

import sklearn as sk
from sklearn.svm import OneClassSVM
#import sys
#sys.path.append('/Users/donbeo/Dropbox (Personal)/pythoncode/Intel/libraries_knime/')

X = input_table.as_matrix()

class AnomalyDetector():

	def __init__(self, nu=0.001):
		self.ocsvm = OneClassSVM(nu=nu)

	def fit(self, x): = x.mean(axis=0)
		self.sigma = x.std(axis=0)
		x = (x - / self.sigma
		return self

	def predict(self, x):
		x_scaled = (x - / self.sigma
		return -self.ocsvm.decision_function(x)

output_model = AnomalyDetector().fit(X)


and it is connected to a predictor block with the following code:


import pandas as pd
# Only use numeric columns
X = input_table.as_matrix()

anomaly_score = input_model.predict(X)

output_table = pd.DataFrame({'prediction': anomaly_score}) 


When I try to run the prediction block I receive the following error:

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 1382, in loads

    return Unpickler(file).load()

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 858, in load


  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 1069, in load_inst

    klass = self.find_class(module, name)

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 1126, in find_class

    klass = getattr(mod, name)

AttributeError: 'module' object has no attribute 'AnomalyDetector'


How can I solve? 


the created model is pickled in the learner and unpickled again in the predictor in a new Python process. This process does not know your class definition and can't load the pickled object. What you could do is create a module containing your class that can then be loaded from all Python processes.

Cheers, Patrick