首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python:获取预测:当尝试使用AttributeError时,'LogisticRegression‘对象没有'classes_’属性

Python:获取预测:当尝试使用AttributeError时,'LogisticRegression‘对象没有'classes_’属性
EN

Stack Overflow用户
提问于 2017-05-13 20:11:58
回答 1查看 3.7K关注 0票数 1

我试图使用LogisticRegression预测推特的人口统计属性,但得到的结果是“文件对象没有属性'classes_'”,第341行,in predict return self.classes_indices AttributeError:'LogisticRegression‘object has no attribute’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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-15 07:47:12

你需要scikit-learn版本0.15:

pip install 'scikit-learn==0.15'

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43953023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档