Appending to and removing from numpy arrays

2020-06-10 Thread Urs Thuermann
Hi, is it possible to append data to or remove data from numpy arrays like Python lists? I have some code where I currently use lists and often do things like a.append(elem) a += b del a[:-n] I am thinking of changing to numpy arrays but it seems I cannot modify numpy arrays like

Re: building numpy arrays with regular structure

2016-12-17 Thread Peter Otten
Seb wrote: > Is there an easier way to write a numpy array with a regular structure? > For example, an array with [0, 1] along the diagnal of one of the array > dimensions, and zero elsewhere: > > zz = np.array([[[0, 1], [0, 0], [0, 0]], >[[0, 0], [0, 1], [0, 0]], >

building numpy arrays with regular structure

2016-12-16 Thread Seb
Hello, Is there an easier way to write a numpy array with a regular structure? For example, an array with [0, 1] along the diagnal of one of the array dimensions, and zero elsewhere: zz = np.array([[[0, 1], [0, 0], [0, 0]], [[0, 0], [0, 1], [0, 0]], [[0, 0], [0, 0],

Re: Getting number of neighbours for a 3d numpy arrays

2016-07-12 Thread Nobody
Some common ways to handle the boundary condition: 1. Generate clamped indices, test for validity and substitute invalid entries with an "identity" element. E.g. ijk = np.mgrid[:a,:b,:c] i,j,k = ijk i0,j0,k0 = np.maximum(0,ijk-1) i1,j1,k1 = np.minimum(np.array(a,b,c).T-1,ijk+1) n1 = (i>

Re: Getting number of neighbours for a 3d numpy arrays

2016-07-12 Thread Peter Otten
Heli wrote: > I have a 3d numpy array containing true/false values for each i,j,k. The > size of the array is a*b*c. > > for each cell with indices i,j,k; I will need to check all its neighbours > and calculate the number of neighbour cells with true values. > > A cell with index i,j,k has the f

Getting number of neighbours for a 3d numpy arrays

2016-07-12 Thread Heli
Hi, I have a 3d numpy array containing true/false values for each i,j,k. The size of the array is a*b*c. for each cell with indices i,j,k; I will need to check all its neighbours and calculate the number of neighbour cells with true values. A cell with index i,j,k has the following neighbou

Re: numpy arrays

2016-04-11 Thread Heli
As you said, this did the trick. sortedVal=np.array(val[ind]).reshape((xcoord.size,ycoord.size,zcoord.size)) Only val[ind] instead of val[ind,:] as val is 1D. Thanks Oscar, -- https://mail.python.org/mailman/listinfo/python-list

Re: numpy arrays

2016-04-11 Thread Heli
Thanks Oscar, In my case this did the trick. sortedVal=np.array(val[ind]).reshape((xcoord.size,ycoord.size,zcoord.size)) -- https://mail.python.org/mailman/listinfo/python-list

Re: numpy arrays

2016-04-07 Thread Oscar Benjamin
On 7 April 2016 at 15:31, Heli wrote: > > Thanks a lot Oscar, > > The lexsort you suggested was the way to go. Glad to hear it. > import h5py > import numpy as np > f=np.loadtxt(inputFile,delimiter=None) > xcoord=np.sort(np.unique(f[:,0])) > ycoord=np.sort(np.unique(f[:,1])) > zcoord=np.sort(np.

Re: numpy arrays

2016-04-07 Thread Heli
Thanks a lot Oscar, The lexsort you suggested was the way to go. import h5py import numpy as np f=np.loadtxt(inputFile,delimiter=None) xcoord=np.sort(np.unique(f[:,0])) ycoord=np.sort(np.unique(f[:,1])) zcoord=np.sort(np.unique(f[:,2])) x=f[:,0] y=f[:,1] z=f[:,2] val=f[:,3] ind = np.lexsort(

Re: numpy arrays

2016-04-07 Thread Oscar Benjamin
On 6 April 2016 at 17:26, Heli wrote: > > 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([[

Re: numpy arrays

2016-04-06 Thread Heli
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

Re: numpy arrays

2016-03-23 Thread Nobody
> What you want is called *transposing* the array: > > http://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html > > That should be a sufficiently fast operation. Transposing itself is fast, as it just swaps the strides and dimensions without touching the data (i.e. it returns a n

Re: numpy arrays

2016-03-23 Thread Simon Ward
On 23 March 2016 10:06:56 GMT+00:00, Heli wrote: >Hi, > >I have a 2D numpy array like this: > >[[1,2,3,4], > [1,2,3,4], > [1,2,3,4] > [1,2,3,4]] > >Is there any fast way to convert this array to > >[[1,1,1,1], > [2,2,2,2] > [3,3,3,3] > [4,4,4,4]] Use the transpose() method: http://docs.scipy

Re: numpy arrays

2016-03-23 Thread Heli
On Wednesday, March 23, 2016 at 11:07:27 AM UTC+1, Heli wrote: > Hi, > > I have a 2D numpy array like this: > > [[1,2,3,4], > [1,2,3,4], > [1,2,3,4] > [1,2,3,4]] > > Is there any fast way to convert this array to > > [[1,1,1,1], > [2,2,2,2] > [3,3,3,3] > [4,4,4,4]] > > In general I wo

Re: numpy arrays

2016-03-23 Thread Chris Angelico
On Wed, Mar 23, 2016 at 9:06 PM, Heli wrote: > I have a 2D numpy array like this: > > [[1,2,3,4], > [1,2,3,4], > [1,2,3,4] > [1,2,3,4]] > > Is there any fast way to convert this array to > > [[1,1,1,1], > [2,2,2,2] > [3,3,3,3] > [4,4,4,4]] What you want is called *transposing* the array: h

Re: numpy arrays

2016-03-23 Thread Manolo Martínez
On 03/23/16 at 03:06am, Heli wrote: > I have a 2D numpy array like this: > > [[1,2,3,4], > [1,2,3,4], > [1,2,3,4] > [1,2,3,4]] > > Is there any fast way to convert this array to > > [[1,1,1,1], > [2,2,2,2] > [3,3,3,3] > [4,4,4,4]] You don't mean just transposing your original array, as

Re: numpy arrays

2016-03-23 Thread Steven D'Aprano
On Wed, 23 Mar 2016 09:06 pm, Heli wrote: > Hi, > > I have a 2D numpy array like this: > > [[1,2,3,4], > [1,2,3,4], > [1,2,3,4] > [1,2,3,4]] > > Is there any fast way to convert this array to > > [[1,1,1,1], > [2,2,2,2] > [3,3,3,3] > [4,4,4,4]] Mathematically, this is called the "tran

numpy arrays

2016-03-23 Thread Heli
Hi, I have a 2D numpy array like this: [[1,2,3,4], [1,2,3,4], [1,2,3,4] [1,2,3,4]] Is there any fast way to convert this array to [[1,1,1,1], [2,2,2,2] [3,3,3,3] [4,4,4,4]] In general I would need to retrieve every nth element of the interior arrays in to single arrays. I know I can

Re: lists vs. NumPy arrays for sets of dates and strings

2014-06-10 Thread Peter Otten
; convenient to work with than lists of lists. What are the advantages and > disadvantages of storing the symbols [A,B] and dates > [2014-01-01,2014-01-02] as lists vs. NumPy arrays? If you don't mind the numpy dependency I can't see any disadvantages. You might also have a look at

Re: lists vs. NumPy arrays for sets of dates and strings

2014-06-09 Thread Denis McMahon
nvenient to work with than lists of lists. What are the advantages and > disadvantages of storing the symbols [A,B] and dates > [2014-01-01,2014-01-02] as lists vs. NumPy arrays? You could also use a dictionary of either lists or tuples or even NumPy arrays keyed on the date. $ python Python

lists vs. NumPy arrays for sets of dates and strings

2014-06-09 Thread beliavsky
storing the symbols [A,B] and dates [2014-01-01,2014-01-02] as lists vs. NumPy arrays? -- https://mail.python.org/mailman/listinfo/python-list

Re: Sum operation in numpy arrays

2013-05-02 Thread Steven D'Aprano
On Thu, 02 May 2013 02:10:11 -0700, Ana Dionísio wrote: > Hello! I have several numpy arrays in my script and i want to add them. > For example: > > a=[1,2,3,4,5] > b=[1,1,1,1,1] > c=[1,0,1,0,1] These are not numpy arrays, they are lists of ints. Based on the error

Re: Sum operation in numpy arrays

2013-05-02 Thread Jens Thoms Toerring
Ana Dionísio wrote: > Hello! I have several numpy arrays in my script and i want to add them. For > example: > a=[1,2,3,4,5] > b=[1,1,1,1,1] > c=[1,0,1,0,1] > for i in range(5): > d[i]=a[i]+b[i]+c[i] > print d > [3,3,5,5,7] > I did it like that

Sum operation in numpy arrays

2013-05-02 Thread Ana Dionísio
Hello! I have several numpy arrays in my script and i want to add them. For example: a=[1,2,3,4,5] b=[1,1,1,1,1] c=[1,0,1,0,1] for i in range(5): d[i]=a[i]+b[i]+c[i] print d [3,3,5,5,7] I did it like that but I get an error: "TypeError: unsupported operand type(s) for +: '

Re: Faster way to map numpy arrays

2012-06-26 Thread Oscar Benjamin
in indices: ... print inds, af[inds], af[inds].sum() ... [1 7 8] [2 8 9] 19 [0 1] [1 2] 3 [8 4] [9 5] 14 Knowing the most efficient way depends on a number of things. The first would be whether or not the operation you're describing is repeated. If, for example, you keep doing this for the sa

Re: Faster way to map numpy arrays

2012-06-25 Thread Saurabh Kabra
Thanks guys I implemented a numpy array with fancy indices and got rid of the list and the loops. The time to do the mapping improved ~10x. As a matter of fact, the number of elements in array A to be summed and mapped was different for each element in B (which was the reason I was using lists). B

Re: Faster way to map numpy arrays

2012-06-25 Thread Oscar Benjamin
On 25 June 2012 08:24, Stefan Behnel wrote: > Saurabh Kabra, 25.06.2012 05:37: > > I have written a script to map a 2D numpy array(A) onto another array(B) > of > > different dimension. more than one element (of array A) are summed and > > mapped to each element of array B. To achieve this I cre

Re: Faster way to map numpy arrays

2012-06-25 Thread Stefan Behnel
Saurabh Kabra, 25.06.2012 05:37: > I have written a script to map a 2D numpy array(A) onto another array(B) of > different dimension. more than one element (of array A) are summed and > mapped to each element of array B. To achieve this I create a list where I > store the index of array A to be ma

Faster way to map numpy arrays

2012-06-24 Thread Saurabh Kabra
I have written a script to map a 2D numpy array(A) onto another array(B) of different dimension. more than one element (of array A) are summed and mapped to each element of array B. To achieve this I create a list where I store the index of array A to be mapped to array B. The list is the dimensio

Re: numpy arrays to python compatible arrays

2010-06-12 Thread Javier Montoya
; some numpy float arrays (obtained from np.fromfile and np.cov > > > functions) and would like to convert them to simple python arrays. > > > I was wondering which is the best way to do that? Is there any > > > function to do that? > > > HiJavier, > > Si

Re: numpy arrays to python compatible arrays

2010-06-10 Thread Martin
; functions) and would like to convert them to simple python arrays. > > I was wondering which is the best way to do that? Is there any > > function to do that? > > Hi Javier, > Since you are new to Python I'll ask whether you want to convert Numpy   > arrays to Pytho

Re: numpy arrays to python compatible arrays

2010-06-10 Thread Philip Semanchuk
s the best way to do that? Is there any function to do that? Hi Javier, Since you are new to Python I'll ask whether you want to convert Numpy arrays to Python arrays (as you stated) or Python lists. Python lists are used very frequently; Python arrays see very little use outside of numpy

numpy arrays to python compatible arrays

2010-06-10 Thread Javier Montoya
Dear all, I'm new to python and have been working with the numpy package. I have some numpy float arrays (obtained from np.fromfile and np.cov functions) and would like to convert them to simple python arrays. I was wondering which is the best way to do that? Is there any function to do that? Bes

Re: Concatenating images (numpy arrays), but they look like HSV images

2009-07-10 Thread Sebastian Schabe
Robert Kern schrieb: Probably, you need to use zeros(..., dtype=uint8). When you use dtype=int, that will result in dtype=int arrays. I suspect that matplotlib is then interpreting that to mean that you want it to treat the input as scalar data (which it will pass through a colormap) rather th

Re: Concatenating images (numpy arrays), but they look like HSV images

2009-07-09 Thread Robert Kern
On 2009-07-09 12:34, Sebastian Schabe wrote: Hello everybody, I want to concatenate 2 numpy array which in fact are RGB images: def concat_images(im1,im2): rows1 = im1.shape[0] rows2 = im2.shape[0] if rows1 < rows2: im1 = concatenate((im1,zeros((rows2-rows1,im1.shape[1],3), int)), axis=0) elif

Concatenating images (numpy arrays), but they look like HSV images

2009-07-09 Thread Sebastian Schabe
Hello everybody, I want to concatenate 2 numpy array which in fact are RGB images: def concat_images(im1,im2): rows1 = im1.shape[0] rows2 = im2.shape[0] if rows1 < rows2: im1 = concatenate((im1,zeros((rows2-rows1,im1.shape[1],3), int)), axis=0) elif rows1 > rows2: im2 = concat

Re: On the indexing order in (numpy) arrays

2008-10-09 Thread Robert Kern
Almar Klein wrote: Basically, we want a[i][j] == a[i,j]. Since there is no literal syntax for numpy arrays, we need to be able to convert from a sequence of sequences to an array. The indexing needs to correspond between the two. Thanks for the reply. I guess that explains

Re: On the indexing order in (numpy) arrays

2008-10-09 Thread Almar Klein
> Basically, we want a[i][j] == a[i,j]. Since there is no literal syntax for > numpy arrays, we need to be able to convert from a sequence of sequences to > an array. The indexing needs to correspond between the two. > Thanks for the reply. I guess that explains the *why*... Ado

Re: On the indexing order in (numpy) arrays

2008-10-09 Thread Terry Reedy
Almar Klein wrote: Hi, I was wondering... Say we have a np.ndarray A of two dimensions (a grayscale image for example). If we want to access x:2, y:3, we have to do A[3,2]. Why is the order of x and y reversed? Because images are stored by rows, not by columns. So column 3, row 2, is row

Re: On the indexing order in (numpy) arrays

2008-10-09 Thread Robert Kern
are often used this way. (In Matlab the data is actually stored last-dimensions-first too.) Basically, we want a[i][j] == a[i,j]. Since there is no literal syntax for numpy arrays, we need to be able to convert from a sequence of sequences to an array. The indexing needs to correspond between

On the indexing order in (numpy) arrays

2008-10-09 Thread Almar Klein
Hi, I was wondering... Say we have a np.ndarray A of two dimensions (a grayscale image for example). If we want to access x:2, y:3, we have to do A[3,2]. Why is the order of x and y reversed? This is reversed in Matlab too, because Matlab is a matrix package and matrix are often used this way. (I

Re: NumPy arrays that use memory allocated from other libraries or tools

2008-09-11 Thread Travis Oliphant
sturlamolden wrote: On Sep 10, 6:39 am, Travis Oliphant <[EMAIL PROTECTED]> wrote: I wanted to point anybody interested to a blog post that describes a useful pattern for having a NumPy array that points to the memory created by a different memory manager than the standard one used by NumPy.

Re: NumPy arrays that use memory allocated from other libraries or tools

2008-09-10 Thread sturlamolden
On Sep 10, 6:39 am, Travis Oliphant <[EMAIL PROTECTED]> wrote: > I wanted to point anybody interested to a blog post that describes a > useful pattern for having a NumPy array that points to the memory > created by a different memory manager than the standard one used by > NumPy. Here is somethi

NumPy arrays that use memory allocated from other libraries or tools

2008-09-09 Thread Travis Oliphant
I wanted to point anybody interested to a blog post that describes a useful pattern for having a NumPy array that points to the memory created by a different memory manager than the standard one used by NumPy. The pattern shows how to create a NumPy array that points to previously allocate

Re: accuracy issues with numpy arrays?

2008-04-29 Thread Robert Kern
You will want to ask numpy questions on the numpy mailing list: http://www.scipy.org/Mailing_Lists I need a little more time to mull on your problem to give you an actual answer, but I hope I can do that over there instead of here. -- Robert Kern "I have come to believe that the whole worl

accuracy issues with numpy arrays?

2008-04-29 Thread eli
Hi, I'm writing a quick script to import a fits (astronomy) image that has very low values for each pixel. Mostly on the order of 10^-9. I have written a python script that attempts to take low values and put them in integer format. I basically do this by taking the mean of the 1000 lowest pixel v

Re: Slicing wrapped numpy arrays

2008-01-13 Thread Robert Kern
Martin Manns wrote: > On Sun, 13 Jan 2008 16:03:16 -0600 > Robert Kern <[EMAIL PROTECTED]> wrote: > >> Martin Manns wrote: >>> Hi, >>> >>> I have created a class that wraps a numpy array of custom objects. I >>> would like to be able to slice respective objects (without copying >>> the array if po

Re: Slicing wrapped numpy arrays

2008-01-13 Thread Martin Manns
On Sun, 13 Jan 2008 16:03:16 -0600 Robert Kern <[EMAIL PROTECTED]> wrote: > Martin Manns wrote: > > Hi, > > > > I have created a class that wraps a numpy array of custom objects. I > > would like to be able to slice respective objects (without copying > > the array if possible). > > > > I have b

Re: Slicing wrapped numpy arrays

2008-01-13 Thread Robert Kern
Martin Manns wrote: > Hi, > > I have created a class that wraps a numpy array of custom objects. I > would like to be able to slice respective objects (without copying the > array if possible). > > I have browsed the doc and found some hints at __getitem__. However, I > still do not grasp how to

Slicing wrapped numpy arrays

2008-01-13 Thread Martin Manns
Hi, I have created a class that wraps a numpy array of custom objects. I would like to be able to slice respective objects (without copying the array if possible). I have browsed the doc and found some hints at __getitem__. However, I still do not grasp how to do it. How do I implement __getitem

Re: concatenating numpy arrays

2006-10-31 Thread Travis E. Oliphant
Rolf Wester wrote: > Hi, > > I want to concatenate two numpy arrays with shape (n1,n2) and (n1,n3) > into a single array with shape (n1,n2+n3). I guess there is an elegant > way to do this but I couldn't figure it out. So any help is very much > appreciated. > Suppo

concatenating numpy arrays

2006-10-31 Thread Rolf Wester
Hi, I want to concatenate two numpy arrays with shape (n1,n2) and (n1,n3) into a single array with shape (n1,n2+n3). I guess there is an elegant way to do this but I couldn't figure it out. So any help is very much appreciated. Regards Rolf -- http://mail.python.org/mailman/listinfo/p

Re: Update on Memory problem with NumPy arrays

2006-06-21 Thread sonjaa
I've been in contact with Travis O, and he said it was fixed in the SVN. thanks for the suggestions, I'll try them out now. best Sonja Filip Wasilewski wrote: > sonjaa wrote: > > Hi > > > > last week I posted a problem with running out of memory when changing &g

Re: Update on Memory problem with NumPy arrays

2006-06-21 Thread Filip Wasilewski
sonjaa wrote: > Hi > > last week I posted a problem with running out of memory when changing > values in NumPy arrays. Since then I have tried many different > approaches and > work-arounds but to no avail. [...] Based on the numpy-discussion this seems to be fixed in the SVN no

Re: Update on Memory problem with NumPy arrays

2006-06-21 Thread Fredrik Lundh
sonjaa wrote: > Also, are there other python methods/extensions that can create > multi-deminsional arrays? if this example is typical for the code you're writing, you might as well use nested Python lists: def make_array(width, height, value): out = [] for y in range(hei

Re: Update on Memory problem with NumPy arrays

2006-06-21 Thread Robert Kern
sonjaa wrote: > Hi > > last week I posted a problem with running out of memory when changing > values in NumPy arrays. Since then I have tried many different > approaches and > work-arounds but to no avail. > > I was able to reduce the code (see below) to its smallest siz

Update on Memory problem with NumPy arrays

2006-06-21 Thread sonjaa
Hi last week I posted a problem with running out of memory when changing values in NumPy arrays. Since then I have tried many different approaches and work-arounds but to no avail. I was able to reduce the code (see below) to its smallest size and still have the problem, albeit at a slower rate