Removing lines from a numpy array

Asked

Viewed 1,713 times

1

For example I have array arr1 and I want to take a line range from that array as below, which function I can 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 rows from 1 to 5 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 rows from 6 to 10 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 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]])

4 answers

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

1

Removing line from 1 to 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 from 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 from 11 to 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

  • 1

    Is my answer. :-(

1

I prefer it 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...

1


Initialize the 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 lines 1 to 5:

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

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

  • 1

    It wouldn’t cost that much?

  • This was the most summary that solved the problem. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.