我正在尝试使用LogisticRegression从Twitter预测人口统计属性,但是我在预测中获得“文件“ Anaconda \ lib \ site-packages \ sklearn \ linear_model \ base.py”,行341
返回self.classes_ [indices]
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'