Remove rows from numpy array

1

For example I have array arr1 and I want to take a row range of this array as below, what function can I use?

arr1:
array([[ 1,  1,  1],
       [ 2,  2,  2],
       [ 3,  3,  3],
       [ 4,  4,  4],
       [ 5,  5,  5],
       [ 6,  6,  6],
       [ 7,  7,  7],
       [ 8,  8,  8],
       [ 9,  9,  9],
       [10, 10, 10],
       [11, 11, 11],
       [12, 12, 12],
       [13, 13, 13],
       [14, 14, 14]])

Remove lines 1 through 5 from arr1

array([[ 6,  6,  6],
       [ 7,  7,  7],
       [ 8,  8,  8],
       [ 9,  9,  9],
       [10, 10, 10],
       [11, 11, 11],
       [12, 12, 12],
       [13, 13, 13],
       [14, 14, 14]])

Remove lines 6 through 10 from arr1

array([[ 1,  1,  1],
       [ 2,  2,  2],
       [ 3,  3,  3],
       [ 4,  4,  4],
       [ 5,  5,  5],
       [11, 11, 11],
       [12, 12, 12],
       [13, 13, 13],
       [14, 14, 14]])

Remove rows from 11 to 14 of arr1

array([[ 1,  1,  1],
       [ 2,  2,  2],
       [ 3,  3,  3],
       [ 4,  4,  4],
       [ 5,  5,  5],
       [ 6,  6,  6],
       [ 7,  7,  7],
       [ 8,  8,  8],
       [ 9,  9,  9],
       [10, 10, 10]])
    
asked by anonymous 18.10.2018 / 22:19

4 answers

1

Initialize array:

a = np.array([[ 1, 1, 1], [ 2, 2, 2], [ 3, 3, 3], [ 4, 4, 4], [ 5, 5, 5], [ 6, 6, 6], [ 7, 7, 7], [ 8, 8, 8], [ 9, 9, 9], [10, 10, 10], [11, 11, 11], [12, 12, 12], [13, 13, 13], [14, 14, 14]])

To remove rows from 1 to 5:

para_remover = [i for i in range(0, 5)]

np.delete(a, para_remover, axis=0)

    
18.10.2018 / 22:36
1

TL; DR

import numpy as np
a1 = np.array([[ 1, 1, 1], [ 2, 2, 2], [ 3, 3, 3], [ 4, 4, 4], [ 5, 5, 5], \
[ 6, 6, 6], [ 7, 7, 7], [ 8, 8, 8], [ 9, 9, 9], [10, 10, 10], [11, 11, 11], \
[12, 12, 12], [13, 13, 13], [14, 14, 14]])

# Removendo linhas de 1 a 5
a2 = a1[5:]

# Removendo linhas de 6 a 10
a3 = np.concatenate((a1[:5],a1[10:]), axis=0)

# Removendo linhas de 11 a 14
a4 = a1[:10]

See the result on repl.it .

    
19.10.2018 / 00:00
1

Removing row 1 through 5:

>>> a[5:len(a)]
array([[ 6,  6,  6],
       [ 7,  7,  7],
       [ 8,  8,  8],
       [ 9,  9,  9],
       [10, 10, 10],
       [11, 11, 11],
       [12, 12, 12],
       [13, 13, 13],
       [14, 14, 14]])

Removing line 6 to 10:

>>> np.concatenate((a[0:5], a[10:len(a)]), axis=0)
array([[ 1,  1,  1],
       [ 2,  2,  2],
       [ 3,  3,  3],
       [ 4,  4,  4],
       [ 5,  5,  5],
       [11, 11, 11],
       [12, 12, 12],
       [13, 13, 13],
       [14, 14, 14]])

Removing line 11 through 14:

>>> a[0:10]
array([[ 1,  1,  1],
       [ 2,  2,  2],
       [ 3,  3,  3],
       [ 4,  4,  4],
       [ 5,  5,  5],
       [ 6,  6,  6],
       [ 7,  7,  7],
       [ 8,  8,  8],
       [ 9,  9,  9],
       [10, 10, 10]])

PS: remembering that in Python the indexes of arrays / vectors / lists always start with the number 0

    
19.10.2018 / 00:13
1

I prefer this way:

>>> sua_array = np.array([[ 1, 1, 1], [ 2, 2, 2], [ 3, 3, 3], [ 4, 4, 4], [ 5, 5, 5], \
[ 6, 6, 6], [ 7, 7, 7], [ 8, 8, 8], [ 9, 9, 9], [10, 10, 10], [11, 11, 11], \
[12, 12, 12], [13, 13, 13], [14, 14, 14]])

>>> np.delete(sua_array, np.s_[:5], axis=0) # deleta os 5 primeiros

>>> np.delete(sua_array, np.s_[5:10], axis=0) # deleta do 5 ao 10

etc.

    
19.10.2018 / 06:36