0

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,) instead of expected (5,x).

Also temps's data structure is like: array([list([.....],...)])

It seems that the inner list is not converted to ndarray.

1
  • Most likely the inner lists differ in length. It can make a 2d array from that, so it just makes a 1d containing their pointers. In effect an array equivalent of the original list. Commented Nov 18, 2018 at 7:31

1 Answer 1

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]])
Sign up to request clarification or add additional context in comments.

1 Comment

Note that as @hpaulj commented, np can't really handle not squared 2d matrices, in which case you will have an array of np arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.