为什么 NLP 特征矩阵有两列?

Why does NLP feature matrix have two columns?

我正在尝试 Quora 虚伪问题分类竞赛(延迟提交),但有一个我无法弄清楚的奇怪错误。这是我的代码(相关部分):

def loss(predict, observed):
  a = predict*observed
  b = predict+observed
  return 2*(a/b)

train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')

train = train.iloc[0:5000, :]
test = test.iloc[0:1000, :]

qid = test['qid']

train = train.drop('qid', axis=1)
test = test.drop('qid', axis=1)

x_train, x_val, y_train, y_val = train_test_split(train['question_text'], train['target'])

count = CountVectorizer(stop_words='english', ngram_range=(1,1), min_df=1, #tokenizer=LemmaTokenizer()
                       )
tfidf = TfidfVectorizer(stop_words='english', ngram_range=(1,1), min_df=1, #tokenizer=LemmaTokenizer()
                       )

count.fit(list(x_train), list(x_val))
x_train_count = count.transform(x_train)
x_val_count = count.transform(x_val)

logistic = LogisticRegression()
logistic.fit(x_train_count, y_train)
predictions = logistic.predict_proba(x_val_count)
print("loss: %0.3f " %loss(predictions, y_val))

当我 运行 它时,我得到这个错误:

ValueError: operands could not be broadcast together with shapes (1250,2) (1250,)

我知道为什么会报错了:因为我不能直接将两个数组相乘。但这里有一些没有意义的维度:

x_val_count.shape - (1250, 8411) 我假设这是数字形式的扩展注释数组(1250 个测试示例)。但是打印数组的开头是这样的:

  (0, 1057) 1
  (0, 4920) 1
  (0, 5563) 1
  (1, 2894) 1
  (1, 3403) 1
  (2, 3311) 1
  (3, 1386) 1
  (3, 1646) 1
  (4, 3207) 1
  (4, 3330) 1
  (4, 6111) 1
  (5, 2346) 1
  (5, 4148) 1
  (5, 4441) 1
  (5, 5223) 1
  (5, 5316) 1
  (5, 5378) 1
  (5, 5565) 2
  (5, 7571) 1
  (6, 746)  2
  (6, 983)  1
  (6, 985)  1
  (6, 3182) 1
  (6, 3455) 1
  (6, 4636) 1

看起来它有两列。为什么会出现这种差异?

predictions.shape - (1250, 2)我不知道为什么预测有两列。为什么不是一个?

我希望如果我知道更多,我就能解决这个问题。但是有人知道我该如何解决这个问题吗?

这里有几个问题,所以我会尽力一一回答。

x_val_count.shape - (1250, 8411) 表示有 1250 个样本和 8411 个特征(其中 8411 是您的词汇量)。但是,出于效率原因,scikit-learn 的矢量化器以稀疏矩阵(non-zero 特征的索引)的形式存储数据。这是因为特征列中有很多 0(文档 - 在您的情况下是 Quora 问题 - 词汇表中几乎没有 1% 的单词)。如果你想把它转换成一个规则的矩阵,你可以简单地调用 x_val_count.toarray(),但是,你可能 运行 内存不足,因为那将是一个巨大的矩阵。输出

(0, 1057) 1
(0, 4920) 1
(0, 5563) 1

可以读作"Document 0 has 3 words in it, each occurring once." 如果你想知道这些词是什么,你可以在count.vocabulary_词典中查找它们,其中词是键,索引(1057, 4920 , ...), 是值。

关于你关于 predictions.shape - (1250, 2) 的第二个问题,你得到了 2 列,因为你调用了 LogisticRegression 的 predict_proba(),每个 class 的 returns 概率(在你的情况下 - 2 classes)。如果你只想要预测标签,你应该调用 predict().