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)