Home > Back-end >  numpy delete not deleting the rows from arrays
numpy delete not deleting the rows from arrays

Time:08-17

I am trying to perform the following: I generate 4 4 by 4 arrays with random values: scan_plus_1 ... scan_plus_4

Next I want to remove rows from each of the previously generated arrays using the numpy.delete funtion. Rows should be deleted using the Offset_Left_Pos array; for instance, for scan_plus_1 rows starting from 0 to 2 should be removed, for scan_plus_2 rows starting from 0 to 1 should be reomoved and so on.

The code is removing rows but not the rows I want it to remove i.e. the first 0 to Offset_Left_Pos[n]rows.

Could you please let me know what is it that I am doing wrong here, or if you have a better solution for this problem?

Thanks in advance.

import numpy as np

Offsets_Left_Pos=[2,1,1,2]

scanlines_pos=[8, 6, 7, 3]

 

Range_SL=range(0,len(scanlines_pos),1)

 

for sl_count in Range_SL:

    #globals()["scan_plus_"   str(sl_count   1)] = np.zeros((4, 4))

    globals()["scan_plus_"   str(sl_count   1)] = np.random.rand(4,4)

 

for sl_count in Range_SL:

 

    globals()["Xscan_plus_"   str(sl_count   1)] = eval("np.delete(" "scan_plus_"   str(sl_count   1) ",[0, Offsets_Left_Pos[sl_count]],axis=0)")  # Delete first offsets components of array

    #globals()["Xscan_plus_"   str(sl_count   1)] = np.delete(eval("scan_plus_"   str(sl_count   1)),(0,Offsets_Left_Pos[sl_count]),axis=0)  # Delete first offsets components of array

 

 

print("scan_plus_1")

print(scan_plus_1)

 

print("Xscan_plus_1")

print(Xscan_plus_1)

CodePudding user response:

Please:

  • Do not use globals() to generate variables names, use lists instead
  • Do not use eval
  • Do use snake_case convention

Regarding your error, it is because you didn't provide all the indices you wanted to deleted, but only the minimum and maximum indices. You should provide a range using np.arange.

Edit: Another, better solution, is just to index the array. Indeed arr[start_idx:] will effectively delete all rows before start_idx.

from pprint import pprint
import numpy as np

offsets_left_pos = [2, 1, 1, 2]
scanlines_pos = [8, 6, 7, 3]

range_sl = range(len(scanlines_pos))

scan_plus = []
for sl_count in range_sl:
    scan_plus_sl_count = np.arange(16).reshape(4, 4)
    scan_plus.append(scan_plus_sl_count)

Xscan_plus = []
for sl_count in range_sl:
    # idxs_to_delete = np.arange(offsets_left_pos[sl_count])
    # Xscan_plus_sl_count = np.delete(scan_plus[sl_count], idxs_to_delete, axis=0)
    # Better for this simple case
    Xscan_plus_sl_count = scan_plus[sl_count][offsets_left_pos[sl_count] : ]
    Xscan_plus.append(Xscan_plus_sl_count)

print("scan_plus")
pprint(scan_plus)

print("\nXscan_plus")
pprint(Xscan_plus)

Prints:

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

Xscan_plus
[array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]]),
 array([[ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]]),
 array([[ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]]),
 array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]])]

CodePudding user response:

I have found a way to solve the issue:

By generating an extract array:

import numpy as np

 

Offsets_Left_Pos=[2,1,1,2]

scanlines_pos=[8, 6, 7, 3]

 

Range_SL=range(0,len(scanlines_pos),1)

 

for sl_count in Range_SL:

    #globals()["scan_plus_"   str(sl_count   1)] = np.zeros((4, 4))

    globals()["scan_plus_"   str(sl_count   1)] = np.random.rand(5,5)

 

for sl_count in Range_SL:

 

    extract=list(range(Offsets_Left_Pos[sl_count]))

    globals()["Xscan_plus_"   str(sl_count   1)] = np.delete(eval("scan_plus_"   str(sl_count   1)),(extract),axis=0)  # Delete first offsets components of array

 

print("scan_plus_1")

print(scan_plus_1)

 

print("Xscan_plus_1")

print(Xscan_plus_1)

 

print("scan_plus_2")

print(scan_plus_2)

 

print("Xscan_plus_2")

print(Xscan_plus_2)

  • Related