我想将矩阵(transpose_test_feature
)与列表(log_train_probs_nonspam_words
)相乘。 Matrix的大小为(2500 * 260),列表大小为2500项。我想把它们相乘。
但我有这种类型错误:
TypeError:不能将序列乘以类型' float'的非int。
有没有人可以快速帮助我?
multiply_nonspam_test = []
for i in range(0, len(transpose_test_feature)):
x = log_train_probs_nonspam_words[i] *transpose_test_feature[i]
multiply_nonspam_test.append(x)
print multiply_nonspam_test
输入:
log_train_probs_nonspam_words
(样本):
[-5.259440519499674, -5.5014525551182665, -6.226860597896433, -4.000908730923304, -6.970438632083271, -6.082121752251521, -6.7823864005803305, -4.22184233658671, -5.805134968916488, -6.28594951426644, -5.396092039460441, -4.935057080197465, -6.321351441317356, -6.054147900209116, -7.698677132454486, -6.71339352909338, -5.403260528939053, -5.454932539483374, -5.837924791739479, -9.239122173401634, -6.405908829345418, -8.83365706529347, -6.9200077784563785, -5.406864136442351, -6.089239220020385, -5.794439679799741, -5.6556032349455245, -7.8528278122817445, -4.863365151741348, -7.3673199965000435, -5.588463932107897, -7.293212024346322, -6.509093065580649, -5.605931625148287, -4.93280736046345, -6.674172815940098, -7.663585812643216, -5.918893854273146, -6.7542155236136345, -7.534374081163209, -6.6241623953654365, -5.095987447010102, -8.140509884733525, -6.2350910970329485, -5.287878454820207]
transpose_test_feature
(原来它是2500 * 260矩阵,我给出了矩阵的第一行):
[0, 1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 3, 1, 0, 2, 3, 0, 0, 2, 2, 0, 5, 1, 3, 0, 0, 3, 3, 0, 0, 1, 0, 1, 1, 1, 3, 0, 0, 3, 4, 1, 8, 0, 0, 0, 1, 3, 2, 0, 2, 2, 0, 0, 0, 1, 2, 0, 3, 2, 1, 0, 0, 1, 0, 3, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 2, 1, 1, 0, 18, 0, 10, 13, 3, 0, 2, 1, 7, 7, 0, 11, 8, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 9, 0, 4, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 42, 11, 5, 0, 19, 3, 0, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 3, 1, 8, 0, 0, 2, 3, 14, 0, 2, 0, 13, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 20, 2, 29, 29, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0]
答案 0 :(得分:0)
您将列表乘以浮点数。您应该将矩阵的每一行与单词相乘。这样的事情会起作用。
multiply_nonspam_test = []
for row in transpose_test_feature:
multiply_nonspam_test.append([x*y for x,y in zip(row, log_train_probs_nonspam_words)])
print multiply_nonspam_test
答案 1 :(得分:0)
您可以使用NumPy来加快速度:
import numpy as np
a1 = np.array(log_train_probs_nonspam_words)
a1 = a1.reshape(a1.shape[0], 1)
a2 = np.array(transpose_test_feature)
multiply_nonspam_test = a1 * a2