<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scienceoss.com &#187; NumPy</title>
	<atom:link href="http://scienceoss.com/categories/numpy/feed/" rel="self" type="application/rss+xml" />
	<link>http://scienceoss.com</link>
	<description>useful tidbits for using open source software in science</description>
	<lastBuildDate>Wed, 26 May 2010 03:34:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Sort one list by another list</title>
		<link>http://scienceoss.com/sort-one-list-by-another-list/</link>
		<comments>http://scienceoss.com/sort-one-list-by-another-list/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 14:14:26 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[argsort]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://scienceoss.com/sort-one-list-by-another-list/</guid>
		<description><![CDATA[Here are a couple of ways of sorting one list by another list in Python. The first uses plain ol&#8217; Python, and the others use NumPy. In each case imagine we want to sort a list of peoples names by their ages. Method 1 Zip the lists together, making sure that the one to sort [...]]]></description>
			<content:encoded><![CDATA[<p>Here are a couple of ways of sorting one list by another list in Python.  The first uses plain ol&#8217; Python, and the others use NumPy.</p>
<p>In each case imagine we want to sort a list of peoples names by their ages.<span id="more-108"></span></p>
<h3>Method 1</h3>
<p>Zip the lists together, making sure that the one to sort by is passed first to zip().  The result is a list of tuples.  When you sort a list of tuples, it sorts using the first item in each tuple.  Then use the zip* trick to unzip the now sorted tuples into separate variables.</p>
<pre class="brush: python; title: ; notranslate">people = ['Jim', 'Pam', 'Micheal', 'Dwight']
ages = [27, 25, 4, 9]

agesAndPeople = zip(ages, people)
agesAndPeople.sort()
sortedAges, sortedPeople = zip(*agesAndPeople)</pre>
<p>Note that if you want to sort in reverse, you can use <span class="c">agesAndPeople.sort(reverse=True)</span>.</p>
<h3>Method 2</h3>
<p>This method uses NumPy, and you don&#8217;t have to convert the lists into arrays.  The argsort() function doesn&#8217;t return the sorted ages . . . instead, it returns the indices that each item would if it were in an already sorted array (try it to see what I mean).  take() is a way of using useful NumPy indexing on a list.  See the next example for something that might be more straigtforward for Matlab users.</p>
<pre class="brush: python; title: ; notranslate">people = ['Jim', 'Pam', 'Micheal', 'Dwight']
ages = [27, 25, 4, 9]

import numpy
inds = numpy.argsort(ages)
sortedPeople = numpy.take(people, inds)</pre>
<h3>Method 3</h3>
<p>This method also uses NumPy, but first it converts the lists into arrays.  Then it uses the argsort() of one to index into the other.</p>
<pre class="brush: python; title: ; notranslate">people = ['Jim', 'Pam', 'Micheal', 'Dwight']
ages = [27, 25, 4, 9]

import numpy
people = numpy.array(people)
ages = numpy.array(ages)
inds = ages.argsort()
sortedPeople = people[inds]</pre>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/sort-one-list-by-another-list/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Convert None to NaN for use in NumPy arrays</title>
		<link>http://scienceoss.com/convert-none-to-nan-for-use-in-numpy-arrays/</link>
		<comments>http://scienceoss.com/convert-none-to-nan-for-use-in-numpy-arrays/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 14:56:19 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[nan]]></category>
		<category><![CDATA[none]]></category>

		<guid isPermaLink="false">http://scienceoss.com/convert-none-to-nan-for-use-in-numpy-arrays/</guid>
		<description><![CDATA[I originally had the function below to convert values of None into values of NaN, but realized it is much faster and easier to coerce the array type: x.astype(float) Here&#8217;s the slow way to do it: from numpy import array, nan def None2NaN(x): """Converts None objects to nan, for use in NumPy arrays. Returns an [...]]]></description>
			<content:encoded><![CDATA[<p>I originally had the function below to convert values of None into values of NaN, but realized it is much faster and easier to coerce the array type:</p>
<pre class="prettyprint"><code class="code">x.astype(float)</code></pre>
<p>Here&#8217;s the slow way to do it:</p>
<pre class="prettyprint"><code class="code">from numpy import array, nan
def None2NaN(x):
    """Converts None objects to nan, for use in
       NumPy arrays. Returns an array."""
    newlist = []
    for i in x:
        if i is not None:
            newlist.append(i)
        else:
            newlist.append(nan)
    return array(newlist)</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/convert-none-to-nan-for-use-in-numpy-arrays/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8220;Vectorization&#8221; in NumPy</title>
		<link>http://scienceoss.com/vectorization-in-numpy/</link>
		<comments>http://scienceoss.com/vectorization-in-numpy/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 14:10:39 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[boolean]]></category>
		<category><![CDATA[indexing]]></category>
		<category><![CDATA[logical operators]]></category>
		<category><![CDATA[vectorization]]></category>

		<guid isPermaLink="false">http://scienceoss.com/vectorization-in-numpy/</guid>
		<description><![CDATA[How do you get Matlab-like vectorization when using NumPy? The key is using parentheses when using logical operators. Here&#8217;s an example: from numpy import * a = array([1,2,3,4,5]) b = array([5,4,3,2,1]) c = array([1,2,2,2,1]) # ===The right way=== (a3) &#038; (c==2) # array([ False, True, False, False, False], dtype=bool) # ===The wrong way=== a3 &#038; [...]]]></description>
			<content:encoded><![CDATA[<p>How do you get Matlab-like vectorization when using NumPy?  The key is using parentheses when using logical operators.  Here&#8217;s an example:</p>
<pre class="prettyprint"><code class="code">from numpy import *

a = array([1,2,3,4,5])
b = array([5,4,3,2,1])
c = array([1,2,2,2,1])

# ===The right way===

(a<5) &#038; (b>3) &#038; (c==2)
# array([ False,  True, False, False, False], dtype=bool)

# ===The wrong way===

a<5 &#038; b>3 &#038; c==2
# ValueError:
# The truth value of an array with more than
# one element is ambiguous. Use a.any() or a.all()</code></pre>
<p>Alternatively, use NumPy&#8217;s <span class="c">logical_and</span> and <span class="c">logical_or</span> functions . . . but these functions only take two arguments at a time.</p>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/vectorization-in-numpy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NumPy arrays vs Matlab matrices</title>
		<link>http://scienceoss.com/numpy-array-basics/</link>
		<comments>http://scienceoss.com/numpy-array-basics/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 00:38:09 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[NumPy]]></category>

		<guid isPermaLink="false">http://scienceoss.com/?p=54</guid>
		<description><![CDATA[NumPy has the same functionality as Matlab in terms of arrays (maybe a little more) but there are some syntax differences in creating and indexing arrays that confused me at first when switching from Matlab to NumPy. There is a more comprehensive tutorial on NumPy, here I&#8217;m just trying to work out some confusion I [...]]]></description>
			<content:encoded><![CDATA[<p>NumPy has the same functionality as Matlab in terms of arrays (maybe a little more) but there are some syntax differences in creating and indexing arrays that confused me at first when switching from Matlab to NumPy. <span id="more-54"></span></p>
<p>There is a <a href="http://www.scipy.org/Tentative_NumPy_Tutorial">more comprehensive tutorial</a> on NumPy, here I&#8217;m just trying to work out some confusion I had transitioning from Matlab.    More notes on making the transition from Matlab to NumPy are available <a href="http://www.scipy.org/NumPy_for_Matlab_Users">here</a>, or for trying to translate betwee Matlab, R, Python, IDL, and Octave, check out the very useful <a href="http://mathesaurus.sourceforge.net/">Mathesaurus</a>.</p>
<p>In my opinion, Matlab has a cleaner and more intuitive interface for array indexing and manipulation.  Nevertheless, I prefer using Python in everyday use because I prefer the rest of the language to Matlab.</p>
<p>Note: you&#8217;ll need to use the following to gain access to NumPy:</p>
<pre class = "prettyprint"><code class = "code">from numpy import *
</code></pre>
<h2>NumPy indices start at 0</h2>
<p>Matlab starts indexing at 1 while NumPy and Python in general start at 0.  This wasn&#8217;t too hard to remember since the rest of Python zero-indexes.  There are more subtle differences in the indexing, though (see below)</p>
<h2>NumPy needs nested brackets for 2D arrays</h2>
<p>In Matlab you create a row vector like this:</p>
<pre class="prettyprint">
<code class="code">
%MATLAB code
a = [1, 2, 3]  % returns 1 2 3
</code>
</pre>
<p>The NumPy equivalent is a one-row, 2D array.  Note the double brackets (more on this later).</p>
<pre class="prettyprint">
<code class="code">
# Python code
a = array([[1, 2, 3]]) # returns array([[1, 2, 3]])
</code>
</pre>
<p>In Matlab, this is a column vector.</p>
<pre class="prettyprint">
<code class="code">
% MATLAB code
a = [1; 2; 3]
% returns
% 1
% 2
% 3
</code>
</pre>
<p>The NumPy equivalent is a 1-column, 2D array.</p>
<pre class="prettyprint">
<code class="code">
a = array([[1], [2], [3]])</code>
# returns
#array([[1],
#       [2],
#       [3]])
</pre>
<h2>In NumPy there&#8217;s a difference between 1D and 2D arrays</h2>
<p>In contrast to Matlab, NumPy makes a distinction between a 2D array and a 1D array, especially when it comes to indexing. The above NumPy examples have nested sets of brackets, which is what makes them 2D.  Here&#8217;s how you make a 1D NumPy array (note the single set of brackets):</p>
<h3>NumPy 1D vs 2D array</h3>
<pre class="prettyprint">
<code class="code">
### 1D Example ###
a = array([1, 2, 3])  # 1D array
a.shape # (3,)  <-- see? Only one dimension
a[0]  # returns 1 (the first item in a)

### 2D Example 1 ###
b = array([[1,2,3]])  # 2D array (1 x 3)
b.shape # (1,3) <-- two dimensions
b[0] # returns array([1,2,3]) # ALERT! Not what you might expect!
b[0][0]  # returns 1.  Aaah, that's better.

### 2D Example 2 ###
c = array([[1],[2],[3]]) # 2D array (3 x 1)
c.shape  # (3,1) <-- two dimensions
c[0]  # returns array([1])  # ALERT! Not what you might expect!
c[0][0] # returns 1.  That's better.
</code>
</pre>
<p>In Matlab if you provide a single index then you get a single number in return.  It worked that way with the NumPy 1D array.  But NumPy gave the <em>first entire row</em> rather than just the first number for the 2D array, <span class="c">b</span>.  Furthermore, the first row in <span class="c">b</span> is itself a 1D array, which you can verify with <span class="c">b[0].shape</span>.</p>
<h2>A ":" has two meanings in NumPy, and it depends on ","</h2>
<p>For a 2D array you can use ":" to mean "all" just like Matlab.  Using the same 2D array, <span class="c">b</span>, as before:</p>
<pre class = "prettyprint"><code class = "code">
b[0,:]  # array([1, 2, 3])
</code></pre>
<p>The comma is pretty important, it determines whether the ":" means "all" or typical Python meaning of "and the rest".</p>
<pre class = "prettyprint"><code class = "code">
b[0,1:]  # array([2, 3])
</code></pre>
<p>But a 1D array will give you an error if you try to access it with two indices like that (using a comma and a colon).</p>
<pre class = "prettyprint"><code class = "code">
a[0,:]  # <type 'exceptions.IndexError'>: too many indices</code></pre>
<p>But you <em>can</em> use a colon with no comma since ":" can also mean "and everything else after".  Like this:</p>
<pre class = "prettyprint"><code class = "code">a1[0:]  # array([1, 2, 3])</code></pre>
<h2>Extracting single columns turns them into a 1D array</h2>
<p>Here's another tricky bit.  If you pull out a single column from a Matlab matrix, it stays a column.</p>
<pre class = "prettyprint"><code class = "code">
% MATLAB code
z = [1, 2, 3; 4, 5, 6]
zc = z(:,2)
size(zc)  % 2 row, 1 column
</code></pre>
<p>But in NumPy, getting a single column from an array returns a 1D array . . . which you can only access with a single index.</p>
<pre class = "prettyprint"><code class = "code">
# Python code
z = array([[1, 2, 3],[4, 5, 6]])
zc = z[:,1]  #  array([2, 5])
zc.shape  # (2,)  <-- a 1D array
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/numpy-array-basics/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

