Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106587

Re: numpy arrays

Newsgroups comp.lang.python
Date 2016-04-06 09:26 -0700
References <3774dc9b-f9d3-462b-bbe4-41b8b2244db7@googlegroups.com> <mailman.43.1458729342.2244.python-list@python.org> <pan.2016.03.23.13.45.27.770000@nowhere.invalid>
Message-ID <02464a1b-401f-4830-bf00-c4df4e95fee4@googlegroups.com> (permalink)
Subject Re: numpy arrays
From Heli <hemla21@gmail.com>

Show all headers | View raw


Thanks for your replies. I have a question in regard with my previous question. I have a file that contains x,y,z and a value for that coordinate on each line. Here I am giving an example of the file using a numpy array called f. 

f=np.array([[1,1,1,1],
            [1,1,2,2], 
            [1,1,3,3],
            [1,2,1,4],
            [1,2,2,5],
            [1,2,3,6],
            [1,3,1,7],
            [1,3,2,8],
            [1,3,3,9],
            [2,1,1,10],
            [2,1,2,11],
            [2,1,3,12],
            [2,2,1,13],
            [2,2,2,14],
            [2,2,3,15],
            [2,3,1,16],
            [2,3,2,17],
            [2,3,3,18],
            [3,1,1,19],
            [3,1,2,20],
            [3,1,3,21],
            [3,2,1,22],
            [3,2,2,23],
            [3,2,3,24],
            [3,3,1,25],
            [3,3,2,26],
            [3,3,3,27],
            ])

then after tranposing f, I get the x,y and z coordinates:
f_tranpose=f.T
x=np.sort(np.unique(f_tranpose[0]))
y=np.sort(np.unique(f_tranpose[1]))
z=np.sort(np.unique(f_tranpose[2]))

Then I will create a 3D array to put the values inside. The only way I see to do this is the following:
arr_size=x.size
val2=np.empty([3, 3,3])

for sub_arr in f:
    idx = (np.abs(x-sub_arr[0])).argmin()
    idy = (np.abs(y-sub_arr[1])).argmin()
    idz = (np.abs(z-sub_arr[2])).argmin()
    val2[idx,idy,idz]=sub_arr[3]

I know that in the example above I could simple reshape f_tranpose[3] to a three by three by three array, but in my real example the coordinates are not in order and the only way I see to do this is by looping over the whole file which takes a lot of time. 

I would appreciate any workarounds to make this quicker. 

Thanks,

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

numpy arrays Heli <hemla21@gmail.com> - 2016-03-23 03:06 -0700
  Re: numpy arrays Steven D'Aprano <steve@pearwood.info> - 2016-03-23 21:32 +1100
  Re: numpy arrays Chris Angelico <rosuav@gmail.com> - 2016-03-23 21:35 +1100
    Re: numpy arrays Nobody <nobody@nowhere.invalid> - 2016-03-23 13:45 +0000
      Re: numpy arrays Heli <hemla21@gmail.com> - 2016-04-06 09:26 -0700
        Re: numpy arrays Heli <hemla21@gmail.com> - 2016-04-07 07:31 -0700
          Re: numpy arrays Heli <hemla21@gmail.com> - 2016-04-11 02:17 -0700
          Re: numpy arrays Heli <hemla21@gmail.com> - 2016-04-11 02:32 -0700
  Re: numpy arrays Manolo Martínez <manolo@austrohungaro.com> - 2016-03-23 11:26 +0100
  Re: numpy arrays Heli <hemla21@gmail.com> - 2016-03-23 03:47 -0700
  Re: numpy arrays Simon Ward <simon+python@bleah.co.uk> - 2016-03-23 10:47 +0000

csiph-web