How to delete two related numpy array elements?

Asked

Viewed 150 times

0

My x and my y to plot are:

x0_n125n25matriz= [ 3472.222  3501.157  3530.092  3559.028  3587.963  3616.898  3645.833
  3674.768  3732.639  3761.574  3819.444  3848.379  3877.315  3906.25
  3935.185  3964.12   3993.055  4021.991  4050.926  4079.861  4108.796
  4137.731  4166.667  4195.602  4224.537  4253.472  1938.657  1967.593
  1996.528  2025.463  2054.398  2083.333  2112.268  2141.204  2170.139
  2199.074  2228.009  2459.491  2488.426  2517.361  2546.296  2575.231
  2604.167  2633.102  2662.037  2690.972  2719.907  2777.778  2806.713
  2835.648  2864.583  2893.518  2922.454  2951.389  2980.324  3009.259
  3038.194  3067.13   3125.     3153.935  3182.87   3211.805  3240.741
  3269.676  3298.611  3327.546  3356.481  3385.417  3414.352  3443.287]
y15_n0matriz= [-0.16542411 -0.16646451 -0.15804609 -0.16922809 -0.16509723 -0.17315663
 -1.         -0.16794772 -0.18579247 -0.1910353  -1.         -0.17504794
 -0.17359474 -0.1910353  -0.23110434 -0.21745199 -0.20903377 -0.20411454
 -0.14195609 -0.12418465 -0.02185758 -0.06863125 -0.05559483 -1.
 -0.00618082  0.31784786 -0.03661292 -0.0307183  -0.05644441 -0.08442226
 -0.06980404 -0.10472111 -0.0879587  -0.12047211 -0.1447231  -0.13637791
 -0.17421431 -0.2750271  -0.28981587 -0.29746496 -0.29663548 -0.29411292
 -0.26879031 -1.         -1.         -1.         -1.         -0.26504059
 -0.25901137 -1.         -1.         -1.         -1.         -1.
 -0.23680369 -0.22844707 -0.22335351 -0.21784316 -0.18085159 -0.16489683
 -0.16492188 -0.16724017 -0.16639032 -0.16230818 -0.16707027 -0.16008367
 -0.15638411 -0.17402684 -0.19244248 -0.18580143]

From y, I need to delete these -1 from the matrix and delete tbm in the matrix x the values corresponding to this index of the -1 in y, so as not to give dimension error to plot . I did so:

x0_n125n25valores=[]
for i in x0_n125n25:
    if i!=0:
        x0_n125n25valores.append(i)

x0_n125n25matriz=np.asarray(x0_n125n25valores)
print(x0_n125n25matriz)
print(x0_n125n25matriz.shape)

y15_n0valores = []
for i in y15_n0:
    if i>=-1 and i<1:
        y15_n0valores.append(i)

y15_n0matriz=np.asarray(y15_n0valores)

y15_n0matrizteste=[]                       ############### ERRO NESSA PARTE
for i in range(0,len(y15_n0matriz)):
    if y15_n0matriz[i]>-1.00000000e+00:
        y15_n0matrizteste.append(y15_n0matriz[i])
    if y15_n0matriz[i]==-1.00000000e+00:
        x0_n125n25matriznovo= np.delete(x0_n125n25matriz,i)
y15_n0matriznovo2 = np.asarray(y15_n0matrizteste)
print(y15_n0matriznovo2)
print(y15_n0matriznovo2.shape)

In the print I saw that I deleted -1 correctly from the y matrix but did not delete the corresponding values in x. Does anyone have any idea of pq? I tried differently but I couldn’t:

for i in range(0,len(y15_n0matriz)):
    if y15_n0matriz[i]==-1.00000000e+00:
        y15_n0matriznovo = np.delete(y15_n0matriz,i)
        x0_n125n25matriznovo= np.delete(x0_n125n25matriz,i)

That way you didn’t delete any point. I don’t know why, help!

1 answer

1


First of all: numpy "arrays" are different from Python "lists" - you can’t simply change their size: it doesn’t work del array[i] to delete the index element "i" from an array. As lists are part of the language, and it is important to understand the algorithm for this, instead of just the "magic" of numpy, I approach this mode first.

A good idea in programming is that simple things are simple - so - even if you need to use these complicated variable names at some point, it’s bad to keep putting in "if" and "for" the name "x0_n125n25values".

Fortunately, the concept of functions in programming helps precisely with this - within a function we use generic names - hence you call the function with its complicated name variables. (I would also suggest not to use complicated names to begin if you need to mark the data series with "n12n25" to know that this is right, it is worth putting everything inside a dictionary - but only if it is easier for those who use the code read).

Back to the main problem: I want to traverse two sequences, and where there is a "-1" in the second sequence, delete the element there, and the corresponding element in the main sequence.

In Python, the for always traverses the elements of a sequence - and we can use the call enumerate, in addition to the element, we also have the index. Also, in any language, if you’re going to change the length of an array while iterating on it, you’re going to have problems - so an interesting Pattern is to iterate by annotating the indexes of the elements to be deleted, and a second interaction by deleting those indexes:



def remove_elementos(referencia, espelho, valor=-1):
    remover = []
    for indice, elemento in enumerate(referencia):
        if elemento == valor:
            remover.append(indice)
    for indice in reversed(remover):
        del referencia[indice]
        del espelho[indice]

    return referencia, espelho

And then , just call these functions with their lists containing data - they will be changed inplace, and you don’t even have to worry about assigning the return value.

Once you have understood this algorithm, then yes we can pass the:

Filter using the numpy

With numpy, we have access to several tools that are not part of the Python "core" - mainly the ability to apply operations to each item of an array, based on the contents of another array, or a scalar.

For example, to filter array elements, we can pass another sequence like index of an array (instead of passing a numeric index) - if the passed sequence has "True" and "False" values, only the elements corresponding to "True" are returned - it is important to view this:

In [78]: a = np.array([0,1,2,3,4,5])                                                                                                 

In [79]: a                                                                                                                           
Out[79]: array([0, 1, 2, 3, 4, 5])

In [80]: b = [True, False, True, True, True, True]                                                                                   

In [81]: a[b]                                                                                                                        
Out[81]: array([0, 2, 3, 4, 5])

Note how the output of a[b] deleted the element at position "1" - which is where "False" was in my sequence b (b in this case is a list -both lists and numpy arrays are "sequences" and work).

Then we combine this with the numpy’s ability, when doing any operation of an array with a scalar, it generates another array with the result of the operation. And that the "operation" in the case may be a comparison (==):

In [83]: x = np.array([0, 1, 2, 3, 4, 5])                                                                                            

In [84]: y = np.array([0, 1, -1, 3, 4, -1])                                                                                          

In [85]: filtro = y == -1                                                                                                            

In [86]: filtro                                                                                                                      
Out[86]: array([False, False,  True, False, False,  True])

In [87]: filtro = y != -1                                                                                                            

In [88]: filtro                                                                                                                      
Out[88]: array([ True,  True, False,  True,  True, False])

Ready - knowing these two things, you can filter the elements of your original data. Just remember that the filter operation (the penultimate example above) generates a new array, not done "inplace" - and save the result in the return variable:


filtro = y15_n0matriz != -1
y15_n0matriz = y15_n0matriz[filtro]
x0_n125n25matriz = x0_n125n25matriz[filtro]

Ready!

Browser other questions tagged

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