Skip to content Skip to sidebar Skip to footer

How To Create A 2d Numpy Ndarray Using Two List Comprehensions

I tried to create a 2D numpy ndarray using the following code: temp = np.array([[np.mean(w2v[word]) for word in docs if word in w2v] for docs in X[:5]]) temp has a shape of (5,) i

Solution 1:

Your missing np.array in there, it should be:

temp = np.array([np.array([np.mean(w2v[word]) for word in docs if word in w2v] for docs in X[:5])])

Running example:

bob
Out[70]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

tmp = np.array([np.array([x for x in Y]) for Y in bob])

tmp
Out[72]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Post a Comment for "How To Create A 2d Numpy Ndarray Using Two List Comprehensions"