简体   繁体   中英

How to change or create new ndarray from list

I have value X of type ndarray with shape: (40000, 2)

The second column of X contains list of 50 numbers

Example:

[17, [1, 2, 3, ...]], 
[39, [44, 45, 45, ...]], ...

I want to convert it to ndarray of shape (40000, 51) :

the first column will be the same

the every element of the list will be in it's own column.

for my example:

[17, 1, 2, 3, ....],
[39, 44, 45, 45, ...]

How can I do it?

np.hstack((arr[:,0].reshape(-1,1), np.array(arr[:,1].tolist())))

Example:

>>> arr
array([[75, list([90, 39, 63])],
       [20, list([82, 92, 22])],
       [80, list([12, 6, 89])],
       [79, list([11, 96, 74])],
       [96, list([26, 37, 65])]], dtype=object)
>>> np.hstack((arr[:,0].reshape(-1,1),np.array(arr[:,1].tolist()))).astype(int)
array([[75, 90, 39, 63],
       [20, 82, 92, 22],
       [80, 12,  6, 89],
       [79, 11, 96, 74],
       [96, 26, 37, 65]])

You can do this for each line of your ndarray, here is an example:

# X = [39, [44, 45, 45, ...]]
newX = numpy.ndarray(shape=(1,51))

new[0] = X[0] # adding the first element

# now the rest of elements
i = 0
for e in X[1] : 
    newX[i] = e
    i = i + 1

You can make this process as a function and apply it in this way:

newArray = numpy.ndarray(shape=(40000,51))

i = 0
for x in oldArray :
    Process(newArray[i],x)
    i=i+1

I defined the source array (with shorter lists in column 1) as:

X = np.array([[17, [1, 2, 3, 4]], [39, [44, 45, 45, 46]]])

To do your task, define the following function:

def myExplode(row):
    tbl = [row[0]]
    tbl.extend(row[1])
    return tbl

Then apply it to each row:

np.apply_along_axis(myExplode, axis=1, arr=X)

The result is:

array([[17,  1,  2,  3,  4],
       [39, 44, 45, 45, 46]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM