スクリプトtrain_et.pyを使用してpickleオブジェクトを作成していますが、フラスコサーバー内の関数内でオブジェクトをロードしようとしていますが、オブジェクト "ItemSelector"と "TextStats"。私はこのhttp://stefaanlippens.net/python-pickling-and-dealing-with-attributeerror-module-object-has-no-attribute-thing.htmlと他の投稿に基づいてそれを解決しようとしましたが、私はそれを理解することができませんでした。カスタムクラスを使用しない別のpickleオブジェクトを使用すると、機能します。任意のアイデアをどのようにこれを修正するには?コードと以下のエラー:Flask内のオブジェクトをunpickle化するときの属性エラー
これは、漬け物生成スクリプトです:私はrun.py
を実行し、私の典型的なフラスコのプロジェクト構造、そして、中train_et.py
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import Pipeline
import pickle
from sklearn.externals import joblib
from sklearn.pipeline import FeatureUnion
from sklearn.feature_extraction import DictVectorizer
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.base import BaseEstimator, TransformerMixin
class ItemSelector(BaseEstimator, TransformerMixin):
def __init__(self, column):
self.column = column
def fit(self, X, y=None, **fit_params):
return self
def transform(self, X):
return (X[self.column])
class TextStats(BaseEstimator, TransformerMixin):
"""Extract features from each document for DictVectorizer"""
def fit(self, x, y=None):
return self
def transform(self, posts):
return [{'REPORT_M': text} for text in posts]
def train():
data = joblib.load('data_df.pkl')
# train and predict
classifier = Pipeline([
('union', FeatureUnion([
('text', Pipeline([
('selector', ItemSelector(column='TEXT')),
('tfidf_vec', TfidfVectorizer(max_df=0.8
])),
('category', Pipeline([
('selector', ItemSelector(column='CATEGORY')),
('stats', TextStats()),
('vect', DictVectorizer())
]))
])),
('clf', ExtraTreesClassifier(n_estimators=30, max_depth=300, min_samples_split=6, class_weight='balanced'))])
classifier.fit(data, data.y)
joblib.dump(classifier, 'et20000.pkl')
if __name__ == '__main__':
train()
を
server
├── run.py
├── flask
├── app
│ ├── load.py
│ ├── __init.py__
│ ├── train_et.py
│ ├── views.py
│ ├── pipeline_classes.py
│ ├── ml
│ │ ├── et20000.pkl
__init.py__
from flask import Flask
app = Flask(__name__)
app.config.from_object('config')
from app import views
run.py
from app import app
if __name__ == '__main__':
app.run(debug=True)
views.py
from app import app
from flask import render_template
from .load import load
@app.before_first_request
def load_classifier():
print("data loading")
global loaded
loaded = load()
print("data loaded")
load.py
import pickle
import pandas as pd
from app import train_et
from app.train_et import ItemSelector, TextStats
def load():
clf_ = pd.read_pickle('app/ml/et20000.pkl')
return(clf)
トレースバックと
builtins.AttributeError
AttributeError: module '__main__' has no attribute 'ItemSelector'
:
は、私は次のエラーを取得する
func()
File "/home/q423446/server/app/views.py", line 19, in load_classifier
loaded = load()
File "/home/q423446/server/app/load.py", line 10, in load
clf_ = pd.read_pickle('app/ml/et20000.pkl')
File "/usr/local/lib/python3.5/dist-packages/pandas/io/pickle.py", line 68, in read_pickle
return try_read(path, encoding='latin1')
File "/usr/local/lib/python3.5/dist-packages/pandas/io/pickle.py", line 62, in try_read
return pc.load(fh, encoding=encoding, compat=True)
File "/usr/local/lib/python3.5/dist-packages/pandas/compat/pickle_compat.py", line 117, in load
return up.load()
File "/usr/lib/python3.5/pickle.py", line 1039, in load
dispatch[key[0]](self)
File "/usr/lib/python3.5/pickle.py", line 1334, in load_global
klass = self.find_class(module, name)
File "/usr/lib/python3.5/pickle.py", line 1388, in find_class
return getattr(sys.modules[module], name)