ScikitLearn model giving 'LocalOutlierFactor' object has no attribute 'predict' Error

爱⌒轻易说出口 提交于 2019-12-07 00:18:26

LocalOutlierFactor does not have a predict method, but only a private _predict method. Here is the justification from the source.

def _predict(self, X=None):
    """Predict the labels (1 inlier, -1 outlier) of X according to LOF.
    If X is None, returns the same as fit_predict(X_train).
    This method allows to generalize prediction to new observations (not
    in the training set). As LOF originally does not deal with new data,
    this method is kept private.

https://github.com/scikit-learn/scikit-learn/blob/a24c8b46/sklearn/neighbors/lof.py#L200

It looks like this may be a Python version thing (although it's not clear to me why scikit learn behaves differently in Python 2 vs. Python 3). I was able to verify locally -- on the same machine -- that my Python 2 install reproduces the error above while Python 3 succeeds (both are using sci-kit learn 0.19.1).

The solution is to specify the python version when you deploy the model (note the last line which, if omitted, defaults to "2.7"):

gcloud beta ml-engine versions create $VERSION_NAME \
    --model $MODEL_NAME --origin $DEPLOYMENT_SOURCE \
    --runtime-version="1.5" --framework $FRAMEWORK
    --python-version="3.5"

Surprisingly, the issue is the runtime version, it will be solved when you will re-create your model version as:

gcloud beta ml-engine versions create $VERSION_NAME  --model $MODEL_NAME --origin $DEPLOYMENT_SOURCE --runtime-version="1.6" --framework $FRAMEWORK --python-version="3.5"

Use Runtime version 1.6 instead of 1.5, turn it to a running model at least.

I worked on a project which seems to be very identic. I got the same error. My problem was a typo in the if-statement.

Regards Lorenz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!