AttributeError: module #39;__main__#39; has no attribute #39;ItemSelector#39;(AttributeError:模块#39;__main__#39;没有属性#39;项选择器#39;)
问题描述
我的项目中有典型的烧瓶结构。一切都很正常,直到我尝试在我的FASK应用程序中加载一个腌制的对象。我使用不同的一个Python脚本创建了Pickleed对象,并且依赖于一些定制类。我认为这是一个问题,当您在一个Main中进行访问,并期望类位于那里时,但我还没有想出如何解决这个问题。我尝试将类添加到Pipeline_Classes.py并将其导入,但不起作用。如有任何想法,我们将不胜感激。
这是生成已腌渍对象的脚本:
Train.py
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.multiclass import OneVsRestClassifier
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, 'et.pkl')
if __name__ == '__main__':
train()
然后是我的FASK应用程序,我试图在其中加载那个腌制的对象。
init.py
from flask import Flask
from .pipeline_classes import ItemSelector
from .pipeline_classes import TextStats
app = Flask(__name__)
app.config.from_object('config')
from app import views
run.py
from app import app
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
def load():
clf_ = pd.read_pickle('et.pkl')
我收到以下错误:
builtins.AttributeError
AttributeError: module '__main__' has no attribute 'ItemSelector'
带回溯:
Traceback (most recent call last) File
"/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1836, in
__call__ return self.wsgi_app(environ, start_response) File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1820, in
wsgi_app response = self.make_response(self.handle_exception(e)) File
"/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1403, in
handle_exception reraise(exc_type, exc_value, tb) File
"/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 33, in
reraise raise value File
"/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1817, in
wsgi_app response = self.full_dispatch_request() File
"/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1470, in
full_dispatch_request
self.try_trigger_before_first_request_functions() File
"/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1497, in
try_trigger_before_first_request_functions func() File
"/home/q423446/server/app/views.py", line 17, in load_classifier
loaded = load() File "/home/q423446/server/app/load.py", line 11, in
load clf_ = pd.read_pickle('app/ml/et_30.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) AttributeError: module '__main__'
has no attribute 'ItemSelector'
推荐答案
尝试在第一个文件的底部更改为pipeline_classes.py
:
if __name__ == "__main__":
ItemSelector.__module__ = "pipeline_classes"
train()
试着阅读http://stefaanlippens.net/python-pickling-and-dealing-with-attributeerror-module-object-has-no-attribute-thing.html
这篇关于AttributeError:模块';__main__';没有属性';项选择器';的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:AttributeError:模块';__main__';没有属性';项选择器';
基础教程推荐
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01