Re: [Numpy-discussion] python array

2014-03-13 Thread Brett Olsen
The difference appears to be that the boolean selection pulls out all data values <= 0.5 whether or not they are masked, and then carries over the appropriate masks to the new array. So r2010 and bt contain identical unmasked values but different numbers of masked values. Because the initial fill

Re: [Numpy-discussion] Robust Sorting of Points

2013-10-28 Thread Brett Olsen
Here's some code implementing the "replace similar values with an arbitrarily chosen one" (in this case the smallest of the similar values). I didn't see any way to do this cleverly with strides, so I just did a simple loop. It's about 100 times slower in pure Python, or a bit under 10 times slow

Re: [Numpy-discussion] Stick (line segments) percolation algorithm - graph theory?

2013-08-26 Thread Brett Olsen
I can see a couple opportunities for improvements in your algorithm. Running your code on a single experiment, I get about 2.9 seconds to run. I get this down to about 1.0 seconds by (1) exploiting the symmetry of the M matrix and (2) avoiding the costly inner loop over k in favor of array operati

Re: [Numpy-discussion] Optimize removing nan-values of dataset

2013-08-14 Thread Brett Olsen
The example data/method you've provided doesn't do what you describe. E.g., in your example data you have several 2x2 blocks of NaNs. According to your description, these should not be replaced (as they all have a neighbor that is also a NaN). Your example method, however, replaces them - in fac

Re: [Numpy-discussion] Smart way to do this?

2013-02-22 Thread Brett Olsen
a = np.ones(30) idx = np.array([2, 3, 2]) a += 2 * np.bincount(idx, minlength=len(a)) >>> a array([ 1., 1., 5., 3., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) As for speed: def loop(a, idx):

Re: [Numpy-discussion] Is there a more efficient way to do this?

2012-08-08 Thread Brett Olsen
On Wed, Aug 8, 2012 at 9:19 AM, Laszlo Nagy wrote: > Is there a more efficient way to calculate the "slices" array below? > > I do not want to make copies of DATA, because it can be huge. The > argsort is fast enough. I just need to create slices for different > dimensions. The above code works, b

Re: [Numpy-discussion] numpy array in networkx graph?

2012-06-12 Thread Brett Olsen
This seems to work: import networkx as nx import pylab import numpy as N M = N.random.random((10, 10)) G = nx.Graph(M) node_colors = [] for i in xrange(len(M)): if M[i,0] < 0.5: node_colors.append('white') else: node_colors.append('blue') nx.draw(G, node_color=node_colors) pylab.show(

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Brett Olsen
> Another issue to watch out for is if the array is empty.  Technically > speaking, that should be True, but some of the solutions offered so far > would fail in this case. Similarly, NaNs or Infs could cause problems: they should signal as False, but several of the solutions would return True.

Re: [Numpy-discussion] Forbidden charcter in the "names" argument of genfromtxt?

2012-02-20 Thread Brett Olsen
On Sat, Feb 18, 2012 at 8:12 PM, Adam Hughes wrote: > Hey everyone, > > I have timeseries data in which the column label is simply a filename from > which the original data was taken.  Here's some sample data: > > name1.txt  name2.txt  name3.txt > 32  34    953 > 32

Re: [Numpy-discussion] (no subject)

2012-02-06 Thread Brett Olsen
The namespace is different. If you want to use numpy.sin(), for example, you would use: import numpy as np np.sin(angle) or from numpy import * sin(angle) I generally prefer the first option because then I don't need to worry about multiple imports writing on top of each other (i.e., having te

Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Brett Olsen
On Mon, Jan 30, 2012 at 11:31 AM, Ted To wrote: > On 01/30/2012 12:13 PM, Brett Olsen wrote: >> On Mon, Jan 30, 2012 at 10:57 AM, Ted To wrote: >>> Sure thing.  To keep it simple suppose I have just a two dimensional >>> array (time,output): >>> [(1,2),(2,3

Re: [Numpy-discussion] Addressing arrays

2012-01-30 Thread Brett Olsen
On Mon, Jan 30, 2012 at 10:57 AM, Ted To wrote: > Sure thing.  To keep it simple suppose I have just a two dimensional > array (time,output): > [(1,2),(2,3),(3,4)] > I would like to look at all values of output for which, for example time==2. > > My actual application has a six dimensional array a

Re: [Numpy-discussion] numpy.percentile multiple arrays

2012-01-24 Thread Brett Olsen
On Tue, Jan 24, 2012 at 6:22 PM, questions anon wrote: > I need some help understanding how to loop through many arrays to calculate > the 95th percentile. > I can easily do this by using numpy.concatenate to make one big array and > then finding the 95th percentile using numpy.percentile but this

Re: [Numpy-discussion] How to output array with indexes to a text file?

2011-08-26 Thread Brett Olsen
On Thu, Aug 25, 2011 at 2:10 PM, Paul Menzel wrote: > is there an easy way to also save the indexes of an array (columns, rows > or both) when outputting it to a text file. For saving an array to a > file I only found `savetxt()` [1] which does not seem to have such an > option. Adding indexes man

Re: [Numpy-discussion] Finding many ways to incorrectly create a numpy array. Please advice

2011-08-02 Thread Brett Olsen
On Tue, Aug 2, 2011 at 9:44 AM, Jeremy Conlin wrote: > I am trying to create a numpy array from some text I'm reading from a > file. Ideally, I'd like to create a structured array with the first > element as an int and the remaining as floats. I'm currently > unsuccessful in my attempts. I've copi

Re: [Numpy-discussion] Fill a particular value in the place of number satisfying certain condition by another number in an array.

2011-08-01 Thread Brett Olsen
This method is probably simpler: In [1]: import numpy as N In [2]: A = N.random.random_integers(-10, 10, 25).reshape((5, 5)) In [3]: A Out[3]: array([[ -5, 9, 1, 9, -2], [ -8, 0, 9, 7, -10], [ 2, -3, -1, 5, -7], [ 0, -2, -2, 9, 1], [ -7, -9,

Re: [Numpy-discussion] Alternative to boolean array

2011-07-20 Thread Brett Olsen
On Tue, Jul 19, 2011 at 11:08 AM, Robert Kern wrote: > On Tue, Jul 19, 2011 at 07:38, Andrea Cimatoribus > wrote: >> Dear all, >> I would like to avoid the use of a boolean array (mask) in the following >> statement: >> >> mask = (A != 0.) >> B   = A[mask] >> >> in order to be able to move th

Re: [Numpy-discussion] Beginner's question

2011-04-20 Thread Brett Olsen
On Sat, Apr 16, 2011 at 2:08 PM, Laszlo Nagy wrote: > import numpy as np > import numpy.random as rnd > > def dim_weight(X): >     weights = X[0] >     volumes = X[1]*X[2]*X[3] >     res = np.empty(len(volumes), dtype=np.double) >     for i,v in enumerate(volumes): >         if v>5184: >          

Re: [Numpy-discussion] slicing / indexing question

2010-09-21 Thread Brett Olsen
On Tue, Sep 21, 2010 at 6:20 PM, Timothy W. Hilton wrote: > Hello, > > I have an indexing problem which I suspect has a simple solution, but > I've not been able to piece together various threads I've read on this > list to solve. > > I have an 80x1200x1200 nd.array of floats this_par.  I have a >

Re: [Numpy-discussion] Two questions on indexing

2010-09-15 Thread Brett Olsen
On Wed, Sep 15, 2010 at 4:38 PM, Mark Fenner wrote: > A separate question.  Suppose I have a slice for indexing that looks like: > > [:, :, 2, :, 5] > > How can I get an indexing slice for all OTHER dimension values besides > those specified.  Conceptually, something like: > > [:, :, all but 2, :,

Re: [Numpy-discussion] scan array to extract min-max values (with if condition)

2010-09-11 Thread Brett Olsen
On Sat, Sep 11, 2010 at 4:46 PM, Massimo Di Stefano wrote: > Thanks Pierre, > > i tried it and all works fine and fast. > > my apologize :-( > > i used a wrong "if" statment to represent my needs > > if mydata[i,0] < E or mydata[i,0] > W or mydata[i,1] < N or mydata[i,1] > S : > > ^^ totally wrong

Re: [Numpy-discussion] scan array to extract min-max values (with if condition)

2010-09-11 Thread Brett Olsen
On Sat, Sep 11, 2010 at 7:45 AM, Massimo Di Stefano wrote: > Hello All, > > i need to extract data from an array, that are inside a > rectangle area defined as : > > N, S, E, W = 234560.94503118, 234482.56929822, 921336.53116178, 921185.3779625 > > the data are in a csv (comma delimited text file,

[Numpy-discussion] Boolean arrays

2010-08-27 Thread Brett Olsen
27;ve come up with a couple possible methods, but they all seem to be inefficient or kludges: >>> valid = N.array(("a", "c")) >>> (ar == valid[0]) | (ar == valid[1]) array([ True, False, True, False, False, True, False, True, True], dtype=bool) &g