gpt4 book ai didi

Python:尝试使用预测时出现 AttributeError: 'LogisticRegression' 对象没有属性 'classes_'

转载 作者:太空宇宙 更新时间:2023-11-03 15:14:51 24 4
gpt4 key购买 nike

我正在尝试使用 LogisticRegression 预测 Twitter 的人口统计属性,但我在预测中收到“File "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

最佳答案

您需要 scikit-learn 版本 0.15:

pip install 'scikit-learn==0.15'

关于Python:尝试使用预测时出现 AttributeError: 'LogisticRegression' 对象没有属性 'classes_',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43953023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com