【发布时间】: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