# Gensim word2vec模型输出1000维ndarray，但最大ndarray维数为32-怎么做？

``````ValueError: maximum supported dimension for an ndarray is 32, found 1000
``````

``````doc = [model[word] for word in text if word in model.vocab]
out = []
n = len(doc[0])
print(n)
print(len(model["hello"]))
print(type(doc[0]))
for i in range(n):
sum = 0
for d in doc:
sum += d[i]
out.append(sum/n)
out = np.ndarray(out)
``````

``````1000
1000
<class 'numpy.ndarray'>
ValueError: maximum supported dimension for an ndarray is 32, found 1000
``````

`ndarray()`函数期望其参数为所构造矩阵形状，因此它试图创建一个新的形状矩阵`(d[0], d[1], ..., d[999])`-然后，该矩阵中的每个单个值都将使用1000-int坐标集进行寻址。而且，实际上`numpy`数组只能具有32个独立的维。

``````result = np.ndarray(len(d))
for i in range(len(d)):
result[i] = d[i]
``````

``````result = np.ndarray(len(d))
result[:] = d  # same result as previous 3-lines w/ loop
``````

``````result = np.array(d)  # same result as previous 2-lines
``````

``````# get a list of available word-vetors
doc = [model[word] for word in text if word in model.vocab]
# average all those vectors
out = np.mean(doc, axis=0)
``````

（不带`axis`参数，它将所有插槽中的所有单个维度值平均在一起，只有一个最终平均值。）

0 条评论