NoVa
NoVa

Reputation: 367

Unpacking a List of Numpy Arrays

I'm trying to open up a pickle file of a large data set, but am running into an issue with the resultant data type. The result gives a list with an array inside, I need to unpack the list to get the array inside. I think it can be boiled down to the following example. Say I have

x = [array([1,1,1], [1,1,1])]

(type(x) = list)

I want to unpack this list so that it's just the array inside. I.e., I want

y = array([1,1,1], [1,1,1])

(type(y) = numpy.ndarray)

I'm relatively new to Python programming and could use a hand on how to do this easily. Please let me know if I need to clarify my question.

I'm not sure if it matters here, but I'm using Python 3.7.6.


EDIT

Looks like I made a mistake when putting in the array for x. To clarify, this is what I'm getting when I unpack the pickle file.

x = pandas.read_pickle(data_source)
print(x)
> [array([1,1,1],[1,1,1])]

Upvotes: 1

Views: 2794

Answers (2)

arebs
arebs

Reputation: 11

so for this case of :

x = [array([1,1,1], [1,1,1])]
np.concatenate(x).ravel()

this should give you this:

array([1,1,1], [1,1,1])

Upvotes: 1

Austin
Austin

Reputation: 26039

With array, I presume you are having a numpy array and you can't have something like:

np.array([1,1,1], [1,1,1])

which throws a TypeError: data type not understood.

You have to wrap a square bracket from outside like so:

np.array([[1,1,1], [1,1,1]])

To answer your question, you can do:

y = x[0]

which makes y a <class 'numpy.ndarray'>.

Upvotes: 1

Related Questions