sedeh - 10 months ago 62

Python Question

In my dataset I've close to 200 rows but for a minimal working e.g., let's assume the following array:

`arr = np.array([[1,2,3,4], [5,6,7,8],`

[9,10,11,12], [13,14,15,16],

[17,18,19,20], [21,22,23,24]])

I can take a random sampling of 3 of the rows as follows:

`indexes = np.random.choice(np.arange(arr.shape[0]), int(arr.shape[0]/2), replace=False)`

Using these indexes, I can select my test cases as follows:

`testing = arr[indexes]`

I want to delete the rows at these indexes and I can use the remaining elements for my training set.

From the post here, it seems that

`training = np.delete(arr, indexes)`

I also tried the suggestion here using

`training = arr[indexes.astype(np.bool)]`

`training = arr[indexes.astype(np.bool)]`

testing

Out[101]:

array([[13, 14, 15, 16],

[ 5, 6, 7, 8],

[17, 18, 19, 20]])

training

Out[102]:

array([[ 1, 2, 3, 4],

[ 5, 6, 7, 8],

[ 9, 10, 11, 12]])

Any idea what I am doing wrong? Thanks.

Answer Source

To delete indexed rows from numpy array:

```
arr = np.delete(arr, indexes, axis=0)
```