【问题标题】:POS tags for train and test sets: ValueError训练集和测试集的 POS 标签:ValueError
【发布时间】:2021-05-20 18:17:04
【问题描述】:

我正在尝试从以下数据集中提取 POS 标签信息

                 Sentences                                        Characters    Label
803    A Complete Bibliography of Scientific American...             128        1
1373    Mandated MVNO access would 'likely lead to del...            244        0 
1257    What is PANS/PANDAS? And Why Are Cases On The ...            212        0
2405    St Laurence School | Care • Inspire • Succ Hea...            124        1
2589    Study reveals: The 50 most Instagrammed island...            212        0

我正在应用以下功能:

(根据艾莉亚的建议)

import nltk
 
tagged_sentences = nltk.corpus.treebank.tagged_sents()
cutoff = int(.75 * len(tagged_sentences))
    
import nltk
 
tagged_sentences = nltk.corpus.treebank.tagged_sents()
cutoff = int(.75 * len(tagged_sentences))

    def features(sentence, index):
        return {
            'word': sentence[index],
            'is_first_word': int(index == 0),
            'is_last_word': int(index == len(sentence) - 1),
            'is_capitalized': sentence[index][0].upper() == sentence[index][0],
            'is_all_upper': int(sentence[index].upper() == sentence[index]), 
            'is_all_lower': int(sentence[index].lower() == sentence[index]), 
            'prev_word': '' if index == 0 else sentence[index - 1],
            'next_word': '' if index == len(sentence) - 1 else sentence[index + 1],
            'prefix-1': sentence[index][0],
            'prefix-2': sentence[index][:2],
            'prefix-3': sentence[index][:3],
            'suffix-1': sentence[index][-1],
            'suffix-2': sentence[index][-2:],
            'suffix-3': sentence[index][-3:],
        }
    
    

按照本文中描述的步骤:https://medium.com/analytics-vidhya/pos-tagging-using-conditional-random-fields-92077e5eaa31,我想将其应用于我的数据(仍在考虑Xy,例如X=df[['Sentences','Characters']]y=df['Label']

X=df[['Sentences','Characters']]
y=df['Label']

X_train, X_test, y_train, y_test  = train_test_split(X, y, test_size=0.25, random_state=40) 

train_df= pd.concat([X_train, y_train], axis=1)
test_df = pd.concat([X_test, y_test], axis=1)

此步骤应将数据集拆分为训练和测试。但是,我已经有了这些信息,所以我不需要将数据集拆分为训练和测试。

def untag(tagged_sentences):
    return [w for w, t in tagged_sentences]


def prepareData(tagged_sentences):
    X,y=[],[]
    for sentence in tagged_sentences.Sentences:
        X.append([features(untag(sentence), index) for index in range(len(sentence))])
        y.append([tag for word,tag in sentence])
    return X,y
    
    X_train,y_train=prepareData(train_df)
    X_test,y_test=prepareData(test_df)

运行我的数据集,我得到了错误:

----> 8 X_train,y_train=prepareData(train_df)

ValueError: not enough values to unpack (expected 2, got 1)

希望您能告诉我如何修复 ValueError。

我需要使用句子来分配标签。困难在于使用我的数据集(训练和测试)来执行与我共享的链接中相同的操作。

【问题讨论】:

  • 您的问题不需要 CRF,因为 y 不是结构化的;这是一个标量。相反,逻辑回归就足够了。
  • 请发帖minimal reproducible example。变量 training_sentences 未定义。
  • 对不起,艾莉亚,我没有明白你的意思。你能解释一下吗?我认为其他帖子可能很有用,但我知道我应该在那里替换什么:nlpforhackers.io/training-pos-tagger 我正在尝试在我的数据框中使用这些句子。所以 training_sentence 应该是我的训练数据集(train_df)中的句子。我想我把所有信息都放在那里了,但很高兴提供更多信息
  • 您从哪里获得 POS 标签?我看到的只有 L,整个句子是 0 或 1。
  • @StupidWolf 是的,我们必须解决其他问题才能解决这个问题。我们正在下面的聊天中讨论它。

标签: python scikit-learn


【解决方案1】:

好的,我看到了问题。嗯,三个问题。

问题1.prepareData变量名

您没有小心地从the tutorial you used 复制。 这就是他们定义prepareData的方式:

def prepareData(tagged_sentences):
    X,y=[],[]
    for sentences in tagged_sentences:
        X.append([features(untag(sentences), index) for index in range(len(sentences))])
        y.append([tag for word,tag in sentences])
    return X,y

(顺便说一句,他们调用了一个变量 sentences 而不是 sentence,这是一个非常令人困惑的命名约定,因为它包含一个 sentence

但是当您复制教程时,您在两个地方更改了名称tagged_sentences,每个地方都不同!

作为函数参数的名称,您将其更改为sentences,已使用。作为函数内部变量的名称,您将其更改为training_sentences。这两个名称都会让您感到困惑,因为它们在您的代码中还有其他含义。但他们应该匹配!尝试在这两个地方将其重命名为 tagged_sentences

问题 2。prepareData 不是为 DataFrames 设计的。

一旦你解决了这个问题,你就会有一个新问题。本教程未使用DataFrame。它正在使用列表。上线会遇到问题:

    for sentences in tagged_sentences:

因为循环遍历 Pandas DataFrame 会遍历列名。 (见我的回答here。)

相反,您只想查看句子。

将该行更改为:

    for sentences in tagged_sentences.Sentences:

这样,您只会得到您关心的Series。 (反正你没有使用Characters 列!)

问题 3. 你没有做 POS 标记!您不需要 CRF!

我想在这里澄清一下。 POS 标记意味着对于句子中的每个输入单词,您都可以预测该单词的标签。 (标签是词的词性。)

你没有这样做。您正在为 整个 句子创建 one 标签。 (请参阅您的Label 专栏。)对于这种类型的问题,您不需要 CRF。没有的输出。逻辑回归是满足您需要的等效模型。

【讨论】:

  • 谢谢艾莉亚。我会尽快实施你的建议,看看它是否有效。只是为了澄清:我的数据框中的“标签”列与 POS 标记无关(它是一个状态变量)
  • 是的,你说y=df['Label']。您正在准备数据来预测这一点,而不是预测 POS 标签。
  • 是的,完全正确。我想使用 POS 标签作为特征。在这个项目中:github.com/nishitpatel01/Fake_News_Detection/blob/master/… POS 标签用作特征。我一直在努力遵循这一点
  • 是的,这是一件完全正常的事情。您仍然不需要 CRF,因为您不是在预测序列。
  • 由于这一步,我仍然遇到同样的错误:---> 45 X.append([features(untag(sentence), index) for index in range(len(sentence))])36 def untag(tagged_sentences): ---> 37 return [w for w, t in tagged_sentences]。错误是:ValueError:没有足够的值来解包(预期 2,得到 1)。在您建议的更改之后,我将更新问题,以显示我遵循的步骤。感谢您的帮助
猜你喜欢
  • 2017-11-01
  • 2013-06-18
  • 2015-01-17
  • 2019-08-15
  • 2018-12-26
  • 2019-05-13
  • 2020-03-09
  • 2012-12-04
  • 1970-01-01
相关资源
最近更新 更多