Python:获取 AttributeError:'LogisticRegression' 对象在尝试使用预测时没有属性 'classes_'

Posted

技术标签:

【中文标题】Python:获取 AttributeError:\'LogisticRegression\' 对象在尝试使用预测时没有属性 \'classes_\'【英文标题】:Python: getting AttributeError: 'LogisticRegression' object has no attribute 'classes_' when tryig to use predictPython:获取 AttributeError:'LogisticRegression' 对象在尝试使用预测时没有属性 'classes_' 【发布时间】:2017-10-12 16:13:45 【问题描述】:

我正在尝试使用 LogisticRegression 从 Twitter 预测人口统计属性,但我得到“文件”Anaconda\lib\site-packages\sklearn\linear_model\base.py”,第 341 行,在预测中 返回 self.classes_[索引] AttributeError: 'LogisticRegression' 对象没有属性 'classes_'"

代码:

_predictor = Predictor()
pred = _predictor.predict_attribute(_model_dir, attr, tweetsArr, _sent_vocab_file, _emo_vocab_file)



class Predictor:
    def __init__(self):
        self.clf = linear_model.LogisticRegression(C=1.0, dual=False, penalty='l2', tol=1e-6)
        self.hv = HashingVectorizer(ngram_range=(1, 2), binary=True)
        self.pred_classes = []
        self.pred_probs = []



def predict_attribute(self, dir, attr, tweets, sent_vocab_file, emo_vocab_file):
    pred_dict = 
    # loading pre-trained model
    current = os.getcwd()
    if dir not in os.getcwd():
        os.chdir(dir)
        for file in glob.glob("*.pkl"):
            if attr in file:
                self.clf = joblib.load(file)
    pred_probs = self.clf.predict_proba(features).tolist()
    pred_classes = self.clf.predict(features).tolist()
    pred_dict['pred_probs'] = pred_probs
    pred_dict['pred_class'] = pred_classes
    return pred_dict

完整的追溯:

C:\Users\orensig\Anaconda\lib\site-packages\sklearn\base.py:315: UserWarning: Trying to unpickle estimator LabelEncoder from version pre-0.18 when using version 0.18.1. This might lead to breaking code or invalid results. Use at your own risk.

UserWarning)
C:\Users\orensig\Anaconda\lib\site-packages\sklearn\base.py:315: UserWarning: Trying to unpickle estimator LogisticRegression from version pre-0.18 when using version 0.18.1. This might lead to breaking code or invalid results. Use at your own risk.

UserWarning)
Traceback (most recent call last):
  File "Algo/streaming_tweets/Eugenia_predict_psycho-demographics_emotions.py", line 71, in predict_attribute

pred_classes = self.clf.predict(features).tolist()
  File "C:\Users\orensig\Anaconda\lib\site-packages\sklearn\linear_model\base.py", line 341, in predict

return self.classes_[indices]
AttributeError: 'LogisticRegression' object has no attribute 'classes_'

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.2\helpers\pydev\pydevd.py", line 1585, in <module>

globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.2\helpers\pydev\pydevd.py", line 1015, in run

pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)

File "Algo/streaming_tweets/Eugenia_predict_psycho-demographics_emotions.py", line 251, in <module>

prediction = pred.predict_attribute(model_dir, attr, tweets1, sent_vocab_file, emo_vocab_file)

File "Algo/streaming_tweets/Eugenia_predict_psycho-demographics_emotions.py", line 77, in predict_attribute

pred_dict['pred_class'] = pred_classes
UnboundLocalError: local variable 'pred_classes' referenced before assignment

Process finished with exit code 1

【问题讨论】:

您确定从文件中加载的模型是经过预训练的吗?你能分别测试它们吗?另外请发布错误的完整堆栈跟踪 @VivekKumar 我在上面添加了完整的回溯 检查最后一个函数的缩进。 @aryamccarthy 缩进不是问题。问题是返回 self.classes_[indices] AttributeError: 'LogisticRegression' object has no attribute 'classes_' 我很确定问题是因为模型是用比 scikit-learn 0.18 更早的版本训练的,我不知道如何重新训练模型 【参考方案1】:

您需要 scikit-learn 0.15 版:

pip install 'scikit-learn==0.15'

【讨论】:

以上是关于Python:获取 AttributeError:'LogisticRegression' 对象在尝试使用预测时没有属性 'classes_'的主要内容,如果未能解决你的问题,请参考以下文章

获取错误 - AttributeError:'module'对象在运行subprocess.run时没有属性'run'([“ls”,“ - l”])

python3爬取网页,报错AttributeError: 'NoneType' object has no attribute 'xpath'?

Python - AttributeError:模块'mysql'没有属性'connector'

初学者 Python:AttributeError:'list' 对象没有属性

Python:AttributeError:'str'对象没有属性'datetime'

Python类继承:AttributeError:'[SubClass]'对象没有属性'xxx'