Re: [Tutor] finding a maximum between the absolute difference of several columns

2012-02-17 Thread Peter Otten
Andre' Walker-Loud wrote:

 import numpy as np
 Vhel_fdiff3 = np.array([abs(Vmatch3_1 - Vmatch3_2), abs(Vmatch3_1 -
 Vmatch3_3), abs(Vmatch3_3 - Vmatch3_2)])
 your_answer = Vhel_fdiff3.max(axis=0)

or

import numpy as np
a = np.array([Vmatch3_1-Vmatch3_2, Vmatch3_1-Vmatch3_3, Vmatch3_3-
Vmatch3_2])
print np.max(np.abs(a), axis=0)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finding a maximum between the absolute difference of several columns

2012-02-17 Thread Andre' Walker-Loud
 import numpy as np
 Vhel_fdiff3 = np.array([abs(Vmatch3_1 - Vmatch3_2), abs(Vmatch3_1 -
 Vmatch3_3), abs(Vmatch3_3 - Vmatch3_2)])
 your_answer = Vhel_fdiff3.max(axis=0)
 
 or
 
 import numpy as np
 a = np.array([Vmatch3_1-Vmatch3_2, Vmatch3_1-Vmatch3_3, Vmatch3_3-
 Vmatch3_2])
 print np.max(np.abs(a), axis=0)

yes, this one is better than mine.  It is more flexible, leaving more options 
for what you may want to do with it afterwards,


andre
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Elaina Ann Hyde
Hello all,
   I am still scripting away and have reached my next quandry, this one is
much simpler than the last, basically I read in a file with several
columns.
Vmatch3_1=dat[col1]
Vmatch3_2=dat[col2]
Vmatch3_3=dat[col3]
Vdav=5.0
Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))

What I would like this to return is the maximum difference in each case, so
I end up with one column which contains only the largest differences.
now I use this to write the condition:

with_v1_3=(Vhel_fdiff3 = Vdav)

I know the condition works and plots if

 Vhel_fdiff3=(Vmatch3_1 - Vmatch3_2)

for example, and I know this syntax would work if it was numbers instead of
columns.
max(abs(1-2),abs(3-7),abs(2-4))
4

The error is:
---
Traceback (most recent call last):
  File double_plot.py, line 109, in module
Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
-
So it can't handle the fact that it's columns of numbers and not single
numbers, so my question is, can I solve this without doing a loop around
it... use numpy, or some other function instead?
I've been searching around and haven't found any good ways forward so I
thought one of you might know.  Thanks
~Elaina

-- 
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Mark Lawrence

On 17/02/2012 01:04, Elaina Ann Hyde wrote:

Hello all,
I am still scripting away and have reached my next quandry, this one is
much simpler than the last, basically I read in a file with several
columns.
Vmatch3_1=dat[col1]
Vmatch3_2=dat[col2]
Vmatch3_3=dat[col3]
Vdav=5.0
Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))

What I would like this to return is the maximum difference in each case, so
I end up with one column which contains only the largest differences.
now I use this to write the condition:

with_v1_3=(Vhel_fdiff3= Vdav)

I know the condition works and plots if

  Vhel_fdiff3=(Vmatch3_1 - Vmatch3_2)

for example, and I know this syntax would work if it was numbers instead of
columns.

max(abs(1-2),abs(3-7),abs(2-4))
4


The error is:
---
Traceback (most recent call last):
   File double_plot.py, line 109, inmodule
 Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
-
So it can't handle the fact that it's columns of numbers and not single
numbers, so my question is, can I solve this without doing a loop around
it... use numpy, or some other function instead?
I've been searching around and haven't found any good ways forward so I
thought one of you might know.  Thanks
~Elaina




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Throwing the error message into google gave a pile of hits from 
stackoverflow.com, which indicate that the error message is from numpy. 
 See if this can get you going 
http://stackoverflow.com/questions/1322380/gotchas-where-numpy-differs-from-straight-python. 
 The fourth answer down states this is a horrible problem so I'll 
duck out here, sorry :)


--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Elaina Ann Hyde
On Fri, Feb 17, 2012 at 1:41 PM, Rohan Sachdeva rsach...@usc.edu wrote:

 The way I would do this is to put all of the differences in a list then
 take the maximum of the list. So..

 a = Vmatch3_1 - Vmatch3_2
 b = Vmatch3_1 - Vmatch3_3
 c = Vmatch3_3 - Vmatch3_2

 Vhel_fdiff3=max([a,b,c])

 That should work. a,b,c are pretty bad variable names...

 Rohan

 On Thu, Feb 16, 2012 at 5:04 PM, Elaina Ann Hyde elainah...@gmail.comwrote:

 Hello all,
I am still scripting away and have reached my next quandry, this one
 is much simpler than the last, basically I read in a file with several
 columns.
 Vmatch3_1=dat[col1]
 Vmatch3_2=dat[col2]
 Vmatch3_3=dat[col3]
 Vdav=5.0
 Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
 Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
 
 What I would like this to return is the maximum difference in each case,
 so I end up with one column which contains only the largest differences.
 now I use this to write the condition:

 with_v1_3=(Vhel_fdiff3 = Vdav)

 I know the condition works and plots if

  Vhel_fdiff3=(Vmatch3_1 - Vmatch3_2)

 for example, and I know this syntax would work if it was numbers instead
 of columns.
 max(abs(1-2),abs(3-7),abs(2-4))
 4

 The error is:
 ---
 Traceback (most recent call last):
   File double_plot.py, line 109, in module
 Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
 Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
 ValueError: The truth value of an array with more than one element is
 ambiguous. Use a.any() or a.all()
 -
 So it can't handle the fact that it's columns of numbers and not single
 numbers, so my question is, can I solve this without doing a loop around
 it... use numpy, or some other function instead?
 I've been searching around and haven't found any good ways forward so I
 thought one of you might know.  Thanks
 ~Elaina

 --
 PhD Candidate
 Department of Physics and Astronomy
 Faculty of Science
 Macquarie University
 North Ryde, NSW 2109, Australia

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




--
Thanks for the replies so far.  I don't think that Rohan's idea solves the
numbers versus columns issue.  If I run it I get
---
Traceback (most recent call last):
  File double_plot.py, line 112, in module
Vhel_fdiff3=max(a,b,c)
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
-
which is just the same error, I looked at the forums and the link suggested
and I guess maybe my problem is trickier than I first thought!
~Elaina

-- 
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Mark Lawrence

On 17/02/2012 02:53, Elaina Ann Hyde wrote:

On Fri, Feb 17, 2012 at 1:41 PM, Rohan Sachdevarsach...@usc.edu  wrote:


The way I would do this is to put all of the differences in a list then
take the maximum of the list. So..

a = Vmatch3_1 - Vmatch3_2
b = Vmatch3_1 - Vmatch3_3
c = Vmatch3_3 - Vmatch3_2

Vhel_fdiff3=max([a,b,c])

That should work. a,b,c are pretty bad variable names...

Rohan

On Thu, Feb 16, 2012 at 5:04 PM, Elaina Ann Hydeelainah...@gmail.comwrote:


Hello all,
I am still scripting away and have reached my next quandry, this one
is much simpler than the last, basically I read in a file with several
columns.
Vmatch3_1=dat[col1]
Vmatch3_2=dat[col2]
Vmatch3_3=dat[col3]
Vdav=5.0
Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))

What I would like this to return is the maximum difference in each case,
so I end up with one column which contains only the largest differences.
now I use this to write the condition:

with_v1_3=(Vhel_fdiff3= Vdav)

I know the condition works and plots if

  Vhel_fdiff3=(Vmatch3_1 - Vmatch3_2)

for example, and I know this syntax would work if it was numbers instead
of columns.

max(abs(1-2),abs(3-7),abs(2-4))
4


The error is:
---
Traceback (most recent call last):
   File double_plot.py, line 109, inmodule
 Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
-
So it can't handle the fact that it's columns of numbers and not single
numbers, so my question is, can I solve this without doing a loop around
it... use numpy, or some other function instead?
I've been searching around and haven't found any good ways forward so I
thought one of you might know.  Thanks
~Elaina

--
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor






--
Thanks for the replies so far.  I don't think that Rohan's idea solves the
numbers versus columns issue.  If I run it I get
---
Traceback (most recent call last):
   File double_plot.py, line 112, inmodule
 Vhel_fdiff3=max(a,b,c)
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
-
which is just the same error, I looked at the forums and the link suggested
and I guess maybe my problem is trickier than I first thought!
~Elaina

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


I've found this http://comments.gmane.org/gmane.comp.python.tutor/72882.

HTH.

--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Elaina Ann Hyde
On Fri, Feb 17, 2012 at 2:27 PM, Mark Lawrence breamore...@yahoo.co.ukwrote:

 On 17/02/2012 02:53, Elaina Ann Hyde wrote:

 On Fri, Feb 17, 2012 at 1:41 PM, Rohan Sachdevarsach...@usc.edu  wrote:

  The way I would do this is to put all of the differences in a list then
 take the maximum of the list. So..

 a = Vmatch3_1 - Vmatch3_2
 b = Vmatch3_1 - Vmatch3_3
 c = Vmatch3_3 - Vmatch3_2

 Vhel_fdiff3=max([a,b,c])

 That should work. a,b,c are pretty bad variable names...

 Rohan

 On Thu, Feb 16, 2012 at 5:04 PM, Elaina Ann Hydeelainah...@gmail.com**
 wrote:

  Hello all,
I am still scripting away and have reached my next quandry, this one
 is much simpler than the last, basically I read in a file with several
 columns.
 Vmatch3_1=dat[col1]
 Vmatch3_2=dat[col2]
 Vmatch3_3=dat[col3]
 Vdav=5.0
 Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
 Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
 
 What I would like this to return is the maximum difference in each case,
 so I end up with one column which contains only the largest differences.
 now I use this to write the condition:

 with_v1_3=(Vhel_fdiff3= Vdav)

 I know the condition works and plots if

  Vhel_fdiff3=(Vmatch3_1 - Vmatch3_2)

 for example, and I know this syntax would work if it was numbers instead
 of columns.

 max(abs(1-2),abs(3-7),abs(2-4)**)
 4


 The error is:
 ---
 Traceback (most recent call last):
   File double_plot.py, line 109, inmodule
 Vhel_fdiff3=max(abs(Vmatch3_1 - Vmatch3_2),abs(Vmatch3_1 -
 Vmatch3_3),abs(Vmatch3_3 - Vmatch3_2))
 ValueError: The truth value of an array with more than one element is
 ambiguous. Use a.any() or a.all()
 -
 So it can't handle the fact that it's columns of numbers and not single
 numbers, so my question is, can I solve this without doing a loop around
 it... use numpy, or some other function instead?
 I've been searching around and haven't found any good ways forward so I
 thought one of you might know.  Thanks
 ~Elaina

 --
 PhD Candidate
 Department of Physics and Astronomy
 Faculty of Science
 Macquarie University
 North Ryde, NSW 2109, Australia

 __**_
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor




 --
 Thanks for the replies so far.  I don't think that Rohan's idea solves the
 numbers versus columns issue.  If I run it I get
 ---
 Traceback (most recent call last):
   File double_plot.py, line 112, inmodule
 Vhel_fdiff3=max(a,b,c)
 ValueError: The truth value of an array with more than one element is
 ambiguous. Use a.any() or a.all()
 -
 which is just the same error, I looked at the forums and the link
 suggested
 and I guess maybe my problem is trickier than I first thought!
 ~Elaina

 __**_
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor


 I've found this 
 http://comments.gmane.org/**gmane.comp.python.tutor/72882http://comments.gmane.org/gmane.comp.python.tutor/72882
 .

 HTH.

 --
 Cheers.

 Mark Lawrence.


 __**_
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor





Interesting, thanks for all the ideas everyone I did finally get it to
work in a loop form.  Anyone wanting to save themselves lots of confusion,
firstly use .append, and secondly don't forget to convert created lists
back to arrays for numpy, here is the working code to look through columns,
match them, return and make an array to operate on with numpy:
-
Vhel_fdiff3=[]
for i in xrange(len(Vmatch3_1)):
Vhel_fdiff3.append(max([abs(Vmatch3_1[i] -
Vmatch3_2[i]),abs(Vmatch3_1[i] - Vmatch3_3[i]),abs(Vmatch3_3[i] -
Vmatch3_2[i])]))

#print Vhel_fdiff3
#appending writes a list, but numpy which makes the with_ work needs an
array
Vhel_fdiff3=array(Vhel_fdiff3)
-
~Elaina
-- 
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finding a maximum between the absolute difference of several columns

2012-02-16 Thread Andre' Walker-Loud
Hi Elaina,

 Vhel_fdiff3=[]
 for i in xrange(len(Vmatch3_1)):
 Vhel_fdiff3.append(max([abs(Vmatch3_1[i] - Vmatch3_2[i]),abs(Vmatch3_1[i] 
 - Vmatch3_3[i]),abs(Vmatch3_3[i] - Vmatch3_2[i])]))
 
 #print Vhel_fdiff3
 #appending writes a list, but numpy which makes the with_ work needs an array 
 Vhel_fdiff3=array(Vhel_fdiff3)

You can skip making the list and directly make a numpy array.


import numpy as np
...
Vhel_fdiff3 = np.zeros_like(Vmatch3_1)
for i in xrange(len(Vmatch3_1):
Vhel_fdiff3[i] = max(abs(Vmatch3_1[i] - Vmatch3_2[i]),abs(Vmatch3_1[i] 
- Vmatch3_3[i]),abs(Vmatch3_3[i] - Vmatch3_2[i]))


But, more importantly, you can do it all in numpy - have a read about the max 
function

http://scipy.org/Numpy_Example_List#head-7918c09eea00e59ec399064e7d5e1e672d242f60

Given an 2-dimensional array, you can find the max for each row or each column 
by specifying which axis you want to compare against.  Almost surely, the built 
in numpy function will be faster than your loop - even if you precompile it by 
importing your functions in another file.  You can do something like


import numpy as np
Vhel_fdiff3 = np.array([abs(Vmatch3_1 - Vmatch3_2), abs(Vmatch3_1 - Vmatch3_3), 
abs(Vmatch3_3 - Vmatch3_2)]) #create an array of the absolute values of the 
differences of each pair of data
# Vhel_fdiff3 is a 2D array
# the first index runs over the three pairs of differences
# the second index runs over the elements of each pair
# to get the maximum difference, you want to compare over the first axis (0)
your_answer = Vhel_fdiff3.max(axis=0)


but you may not want the abs value of the differences, but to actually keep the 
differences, depending on which one has the maximum abs difference - that would 
require a little more coding.


Andre
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor