难以转换列表:'str' 对象没有属性 'items'
Difficulty converting a list: 'str' object has no attribute 'items'
我正在尝试使用 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(train_toks, algorithm=None, trace=3, encoding=None, labels=None, gaussian_prior_sigma=0, **cutoffs)
Parameters train_toks (list) – Training data, represented as a list of pairs, the first member of which is a featureset, and the second of which is a classification label.
您的元组需要让第一个元素成为 dict
,即“将 [s] 字符串映射到数字、布尔值或字符串”,然后您需要让第二个元素成为分类标签。
from nltk.classify import maxent
data = [({"TEXT 1": 'no'}, "Label")]
classifier = maxent.MaxentClassifier.train(data, bernoulli=False, max_iter=10)
我正在尝试使用 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(train_toks, algorithm=None, trace=3, encoding=None, labels=None, gaussian_prior_sigma=0, **cutoffs)
Parameters train_toks (list) – Training data, represented as a list of pairs, the first member of which is a featureset, and the second of which is a classification label.
您的元组需要让第一个元素成为 dict
,即“将 [s] 字符串映射到数字、布尔值或字符串”,然后您需要让第二个元素成为分类标签。
from nltk.classify import maxent
data = [({"TEXT 1": 'no'}, "Label")]
classifier = maxent.MaxentClassifier.train(data, bernoulli=False, max_iter=10)