Insert values into an array with dimensions defined from a loop

Asked

Viewed 58 times

0

Good afternoon, I am trying to save values in the form of an array from different text files, using a loop. I need the values obtained from the first iteration to be stored in the first line of the array, the values obtained from the second iteration in the second array and so on... I tried this by initially creating an array of zeros and then replacing the zeros with the respective values of each iteration. However, once the loop is finished, only the values of the last iteration are stored, that is, I have an array of zeros less in the last line. Something I’m doing wrong but I haven’t figured it out yet, can anyone help me? I leave here an excerpt of the code.

nfile = 0
while nfile < 2:
    a = np.loadtxt('day_'+str(nfile)+'.txt', delimiter=",")
    amax = max(abs(a[:,0]))
    bmax = max(abs(a[:,1]))

    X = np.zeros((2, 2))
    X[nfile] = (amax, bmax)

    nfile = nfile + 1

print(X)

The output should be:

array([[1.0404, 1.53  ],
       [1.4827, 0.73  ]])

This is the output I got:

array([[0.    , 0.    ],
       [1.4827, 0.73  ]])

2 answers

0


The problem is that you are recreating the array every iteration within the loop:

X = np.zeros((2, 2))

That stretch needs to be before the while.

0

Many of the problems presented here on the site involving numpy and pandas can be solved in reading the data, thus avoiding unnecessary processing. So sometimes Aps are asked for code and data samples because one detail makes the difference.

import numpy as np
#Simula um conjunto de arquivos para teste...
from io import StringIO 

d = {
    "day_0.txt": StringIO("1,0\n1.0404,-1\n0,1.53"),
    "day_1.txt": StringIO("1,0.73\n1.4827,0\n1,0")
}
#...fim da simulação.


X = np.zeros((2, 2))
for n in range(0,2):
    X[n] = tuple(map(max, abs(np.loadtxt(d['day_'+str(n)+'.txt'], delimiter=",", unpack=True))))

print(X)

Test in ideone.

To get to that code first we have to fix the example:

nfile = 0
X = np.zeros((2, 2))      # Muda o posicionamento da inicialização do array que conterá os resultados.
while nfile < 2:
    a = np.loadtxt('day_'+str(nfile)+'.txt', delimiter=",")
    amax = max(abs(a[:,0]))
    bmax = max(abs(a[:,1]))

    #X = np.zeros((2, 2))  
    X[nfile] = (amax, bmax)

    nfile = nfile + 1

print(X)

Note that you make two calls from abs() to get the absolute values of all the elements in the file which can be simplified to a single call.

nfile = 0
X = np.zeros((2, 2))      
while nfile < 2:
    #Faz uma única chamada de abs
    a = abs(np.loadtxt('day_'+str(nfile)+'.txt', delimiter=","))
    amax = max(a[:,0]))       #Removida a chamada local de abs()
    bmax = max(a[:,1]))       #Removida a chamada local de abs()

    X[nfile] = (amax, bmax)

    nfile = nfile + 1

print(X)

Then use the parameter unpack=True in 'np.loadtxt()' to unpack columns into two variables so slices amax = max(a[:,0])) and bmax = max(a[:,1])).

nfile = 0
X = np.zeros((2, 2))      
while nfile < 2:
    #Desenpacota as colunas em a e b.
    a, b = abs(np.loadtxt('day_'+str(nfile)+'.txt', delimiter=",", unpack=True))
    #amax = max(a)       #Remove código redundante.
    #bmax = max(b)       #Remove código redundante.

    X[nfile] = (max(a), max(b))

    nfile = nfile + 1

print(X)

Instead of making two calls from max() apply make a map() and manages a tupla with the results.

nfile = 0
X = np.zeros((2, 2))      
while nfile < 2:
    #Aplica max a colunas desempacotas gerando uma tupla e salvando X.
    X[nfile] = tuple(map(max, abs(np.loadtxt('day_'+str(nfile)+'.txt', delimiter=",", unpack=True))))      

    #X[nfile] = (max(a), max(a))     #Removido código desnescessário.

    nfile = nfile + 1

print(X)

And finally simplify the file reading method.

import numpy as np

X = np.zeros((2, 2))      
for nfile in range(0,2):
    X[nfile] = tuple(map(max, abs(np.loadtxt('day_'+str(nfile)+'.txt', delimiter=",", unpack=True))))          

print(X)

Browser other questions tagged

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