Unpacking a List of Numpy Arrays

NoVa

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])]
Austin

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'>.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

unpacking a split inside a list comprehension

分類Dev

Trouble unpacking list in a customized way

分類Dev

how to make a dtype for list of arrays in numpy python3+

分類Dev

Unpacking "the rest" of the elements in list comprehension - python3to2

分類Dev

Numpy rank 1 arrays

分類Dev

How to combine arrays in numpy?

分類Dev

Defining numpy indexing arrays

分類Dev

Unnesting Numpy Arrays

分類Dev

numpy fill an array with arrays

分類Dev

How to create a pandas dataframe column using a list of numpy nd-arrays?

分類Dev

Numpy: Subtract 2 numpy arrays row wise

分類Dev

python - numpy - summation of numpy arrays of different dimension

分類Dev

How to insert a numpy array to a numpy array of arrays?

分類Dev

Streaming multiple numpy arrays to a file

分類Dev

Matrix multiplication with multiple numpy arrays

分類Dev

Append numpy arrays with different dimensions

分類Dev

Calculating MSE between numpy arrays

分類Dev

Filtering Numpy's array of arrays

分類Dev

Concatenate unique numpy arrays with counters

分類Dev

Modifying a chunk of arrays in Numpy Python

分類Dev

Compare list of arrays for overlap

分類Dev

Violin plot of a list of arrays

分類Dev

Fast numpy covariance for arrays of different shape

分類Dev

Converting rdd of numpy arrays to pyspark dataframe

分類Dev

How to compare numpy arrays ignoring nans?

分類Dev

Merge multidimensional NumPy arrays based on first row

分類Dev

Add numpy arrays as rows into pandas with string as index

分類Dev

How to merge very large numpy arrays?

分類Dev

Looping through 2 numpy arrays and concatenating them