当我运行以下代码时,为了初始化一个二维矩阵:
X_train_word2vec = np.array(X_train_word2vec)
train_x = np.zeros([X_train_word2vec.shape[0], 150])
for i, sentence in enumerate(X_train_word2vec):
for j, word in enumerate(sentence):
train_x[i,j] = model_word2vec.wv[word]
X_train_word2vec
的形状为(12481,)
,而train_x
的形状为(12481, 150)
。我收到此错误:
ValueError: setting an array element with a sequence.
为什么?我想数组的形状有问题,但是我不确定。
更新: 追溯
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-bbd0ab46f676> in <module>()
4 for i, sentence in enumerate(X_train_word2vec):
5 for j, word in enumerate(sentence):
----> 6 train_x[i,j] = model_word2vec.wv[word]
ValueError: setting an array element with a sequence.
更新2:
我发现问题是将数组的分配视为train_x
矩阵的元素,因为似乎我们只能分配int
或float
作为矩阵的元素