user14662416
user14662416

Reputation:

How do I split an Array into 10 individual 2D arrays?

So I have a large array with 10000 cartesian coordinates in.

points_Cartesian = np.random.random((10000, 2))

I want to split the Array into the x coordinates and the y coordinates as well as splitting them into groups of 1000.

Currently, I have this code which splits it into the chunks of 1000 but I can't quite figure out how to break up the x and the y into two separate arrays.

x = [points_Cartesian[x: x + 1000] for x in range(0, len(points_Cartesian), 1000)]

I have a feeling all I am missing is a simple bit of code I have to add but I just can't quite get it right.

Upvotes: 1

Views: 272

Answers (1)

LeoE
LeoE

Reputation: 2083

Yes, you really were missing only a tiny part. You can access the column of a numpy array by:

np_array[row,col]

so all that you need to do is:

x = [points_Cartesian[x: x + 1000, 0] ...]
y = [points_Cartesian[x: x + 1000, 1] ...]

or in total (shortened for print):

import numpy as np

points_Cartesian = np.random.random((10, 2))

print(points_Cartesian)

x = [points_Cartesian[x: x + 2, 0] for x in range(0, len(points_Cartesian), 2)]
y = [points_Cartesian[x: x + 2, 1] for x in range(0, len(points_Cartesian), 2)]

print(x)
print(y)

Out:

[[0.38157813 0.04811649]
 [0.29566672 0.20884901]
 [0.01248476 0.46630594]
 [0.13328297 0.13989798]
 [0.70504099 0.20868818]
 [0.07876386 0.5707934 ]
 [0.04177351 0.82604852]
 [0.76085304 0.04226348]
 [0.33315792 0.81473101]
 [0.30193693 0.24475062]]

[array([0.38157813, 0.29566672]), array([0.01248476, 0.13328297]), array([0.70504099, 0.07876386]), array([0.04177351, 0.76085304]), array([0.33315792, 0.30193693])]
[array([0.04811649, 0.20884901]), array([0.46630594, 0.13989798]), array([0.20868818, 0.5707934 ]), array([0.82604852, 0.04226348]), array([0.81473101, 0.24475062])]

Upvotes: 1

Related Questions