【问题标题】:Why does NLP feature matrix have two columns?为什么 NLP 特征矩阵有两列?
【发布时间】:2019-10-23 22:01:46
【问题描述】:

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

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) 我假设这是扩展的 cmets 数组(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)我不知道为什么预测有两列。为什么没有?

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

【问题讨论】:

  • 代码的哪一行抛出了错误?您可以添加部分 CSV 文件吗?

标签: python machine-learning scikit-learn nlp kaggle


【解决方案1】:

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

x_val_count.shape - (1250, 8411) 表示有 1250 个样本和 8411 个特征(其中 8411 是您的词汇量)。但是,出于效率原因,scikit-learn 的矢量化器以稀疏矩阵(非零特征的索引)的形式存储数据。这是因为特征列中有很多 0(一个文档——在你的情况下是一个 Quora 问题——几乎没有词汇表中 1% 的单词)。如果要将其转换为常规矩阵,只需调用x_val_count.toarray(),但是,您可能会耗尽内存,因为那将是一个巨大的矩阵。输出

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

可以理解为“文档 0 中有 3 个单词,每个单词出现一次。”如果您想知道这些词是什么,可以在 count.vocabulary_ 字典中查找它们,其中词是键,索引(1057、4920...)是值。

对于您关于predictions.shape - (1250, 2) 的第二个问题,您将获得 2 列,因为您调用了 LogisticRegression 的 predict_proba(),它返回每个类的概率(在您的情况下为 2 个类)。如果你只想要预测标签,你应该打电话给predict()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-21
    • 2022-11-08
    • 1970-01-01
    • 2017-08-23
    • 2013-09-25
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多