我正在尝试使用 NLTK 创建一个分类器,但是,我相信我的数据格式存在无法解决的问题。
我的数据如下所示:
data = [("TEXT 1", 'no'), ("TEXT 2", 'yes'), ("TEXT 3", 'no'), ("TEXT 4", 'no'), ("TEXT 5", 'yes')]
然后,我运行以下代码:
import nltk
from nltk.classify import maxent
classifier = maxent.MaxentClassifier.train(data, bernoulli=False, max_iter=10)
但是,不幸的是我有以下错误。这个错误包括什么,我如何克服它?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-93-e1b29adbeebb> in <module>
----> 1 classifier = maxent.MaxentClassifier.train(data, bernoulli=False, max_iter=10)
/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train(cls, train_toks, algorithm, trace, encoding, labels, gaussian_prior_sigma, **cutoffs)
324 algorithm = algorithm.lower()
325 if algorithm == "iis":
--> 326 return train_maxent_classifier_with_iis(
327 train_toks, trace, encoding, labels, **cutoffs
328 )
/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train_maxent_classifier_with_iis(train_toks, trace, encoding, labels, **cutoffs)
1175 # Construct an encoding from the training data.
1176 if encoding is None:
-> 1177 encoding = BinaryMaxentFeatureEncoding.train(train_toks, labels=labels)
1178
1179 # Count how many times each feature occurs in the training data.
/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train(cls, train_toks, count_cutoff, labels, **options)
665
666 # Record each of the features.
--> 667 for (fname, fval) in tok.items():
668
669 # If a count cutoff is given, then only add a joint
AttributeError: 'str' object has no attribute 'items'
从文档:
火车(train_toks,算法=无,trace=3,编码=无,标签=无,gaussian_prior_sigma=0,**cutoffs)
参数 train_toks (list) – 训练数据,表示为对列表,其中第一个成员是featureset,第二个成员是分类标签。
您的元组需要将第一个元素设为dict
“将 [s] 字符串映射到数字、布尔值或字符串”,然后您需要将第二个元素设为分类标签。
from nltk.classify import maxent
data = [({"TEXT 1": 'no'}, "Label")]
classifier = maxent.MaxentClassifier.train(data, bernoulli=False, max_iter=10)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句