Numpy_Snippets
Updated: 2016-09-09
Previous snippets:
None Jan 5, 2015
Documentation Jan 30
Edits May 5
Documentation
NumPy Reference — NumPy v1.9 Manual
Tentative NumPy Tutorial -
for numpy python packages
http://www.lfd.uci.edu/~gohlke/pythonlibs/
other links
http://rintintin.colorado.edu/~wajo8931/docs/jochem_aag2011.pdf
-------------------------------------------------------------------------------------------------
As a companion to the Numpy Lessons series, I have posted within my blog, I have decided to maintain a series of snippets that don't comfortably fit into a coherent lesson. They, like the lessons, will be sequentially numbered with links to the previous ones kept in the top section. Contributions and/or corrections.
All samples assume that the following imports are made. Other required imports will be noted when necessary.
<SPAN class="comment token"># default imports used in all examples whether they are or not</SPAN>
<SPAN class="keyword token">import</SPAN> numpy <SPAN class="keyword token">as</SPAN> np
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
This is a bit of a hodge-podge, but the end result is produce running means for a data set over a 10-year time period.
Simple array creation is shown using two methods, as well as how to convert array contents to specific data types.
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> year_data <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>arange<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2005</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">2015</SPAN><SPAN class="punctuation token">,</SPAN>dtype<SPAN class="operator token">=</SPAN><SPAN class="string token">'int'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># 10 years worth of records from 2005 up to, but not 2015</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> some_data <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>arange<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">,</SPAN>dtype<SPAN class="operator token">=</SPAN><SPAN class="string token">'float'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># some numbers...sequential and floating point in this case</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>zeros<SPAN class="punctuation token">(</SPAN>shape<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>dtype<SPAN class="operator token">=</SPAN><SPAN class="string token">'float'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># create an array of 0's with 10 records</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result<SPAN class="punctuation token">.</SPAN>fill<SPAN class="punctuation token">(</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># provide a null value and fill the zero's with null values</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_list <SPAN class="operator token">=</SPAN> zip<SPAN class="punctuation token">(</SPAN>year_data<SPAN class="punctuation token">,</SPAN>some_data<SPAN class="punctuation token">,</SPAN>result<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># zip the 3 arrays together</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> dt <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>dtype<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'year'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'int'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Some_Data'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'float'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Mean_5year'</SPAN><SPAN class="punctuation token">,</SPAN>np<SPAN class="punctuation token">.</SPAN>float64<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># combined array type</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_array <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN>result_list<SPAN class="punctuation token">,</SPAN>dtype<SPAN class="operator token">=</SPAN>dt<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># produce the final array with the desired data type</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_array
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">2005</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2006</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2007</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2008</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2009</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2010</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2011</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">6.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2012</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">7.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2013</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">8.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2014</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">9.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
dtype<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'year'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<i4'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Some_Data'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Mean_5year'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
The result_array now consists of a three columns, which can be accessed by names using array slicing.
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_array<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'year'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># slicing the year, data and result column values</SPAN>
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">2005</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2006</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2007</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2008</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2009</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2010</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2011</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2012</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2013</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2014</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_array<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Some_Data'</SPAN><SPAN class="punctuation token">]</SPAN>
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">6</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">7</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">8</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">9</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_array<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Mean_5year'</SPAN><SPAN class="punctuation token">]</SPAN>
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
If this array, an ndarray, is converted to a recarray, field access can also be achieved using 'array.field' notation.
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_v2 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>result_array<SPAN class="punctuation token">.</SPAN>view<SPAN class="punctuation token">(</SPAN>np<SPAN class="punctuation token">.</SPAN>recarray<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># convert it to a recarray to permit 'array.field access'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_v2<SPAN class="punctuation token">.</SPAN>year
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">2005</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2006</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2007</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2008</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2009</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2010</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2011</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2012</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2013</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2014</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_v2<SPAN class="punctuation token">.</SPAN>Some_Data
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">6</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">7</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">8</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">9</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_v2<SPAN class="punctuation token">.</SPAN>Mean_5year
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
The remainder of the demonstration basically shows some of the things that can be done with ndarrays and recarrays. As an example, the 5-year running mean will be calculated and the Mean_5year column's null values replaced with valid data. The 'np.convolve' method will be used to determine the running means for no other reason than I hadn't used it before. Since the input data are sequential numbers from 0 to 9, it will be pretty easy to do the mental math to figure out whether the running mean is indeed correct. The steps entail:
- decide upon the mean step to use (eg. N=5),
- run the convolve method on the 'Some_Data' column in the result_v2 recarray,
- pad the resultant array so that the sizes of the running mean calculation array and the column array are equal.
Here it goes...
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> N <SPAN class="operator token">=</SPAN> <SPAN class="number token">5</SPAN> <SPAN class="comment token"># five year running mean step, see the help on convolve</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> rm <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>convolve<SPAN class="punctuation token">(</SPAN>result_v2<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Some_Data'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>np<SPAN class="punctuation token">.</SPAN>ones<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>N<SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">/</SPAN>N<SPAN class="punctuation token">,</SPAN> mode<SPAN class="operator token">=</SPAN><SPAN class="string token">'valid'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># a mouth-full</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> rm <SPAN class="comment token"># however, there are only values for the mid-point year</SPAN>
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">6</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">7</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># so we need to pad by 2 on either end of the output</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> pad_by <SPAN class="operator token">=</SPAN> N<SPAN class="operator token">/</SPAN><SPAN class="number token">2</SPAN> <SPAN class="comment token"># integer division...this has change in python 3.x</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> new_vals <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>pad<SPAN class="punctuation token">(</SPAN>rm<SPAN class="punctuation token">,</SPAN>pad_by<SPAN class="punctuation token">,</SPAN>mode<SPAN class="operator token">=</SPAN><SPAN class="string token">'constant'</SPAN><SPAN class="punctuation token">,</SPAN>constant_values<SPAN class="operator token">=</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># padding the result to new_vals</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> new_vals
array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">6</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">7</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_v2<SPAN class="punctuation token">.</SPAN>Mean_5year <SPAN class="operator token">=</SPAN> new_vals <SPAN class="comment token"># set the new_vals into the correct column</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_v2 <SPAN class="comment token"># voila</SPAN>
rec<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">2005</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2006</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2007</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2008</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2009</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2010</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5.000000000000001</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2011</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">6.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">6.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2012</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">7.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">7.000000000000001</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2013</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">8.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2014</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">9.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
dtype<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'year'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<i4'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Some_Data'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Mean_5year'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
A bit messy with that floating point representation thing appearing for a few numbers....let's clean it up by changing the dtype to limit the number of decimal points in the array showing up in the 'Mean_5year column. This will be done incrementally.
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> x <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>round<SPAN class="punctuation token">(</SPAN>result_v2<SPAN class="punctuation token">.</SPAN>Mean_5year<SPAN class="punctuation token">,</SPAN>decimals<SPAN class="operator token">=</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_v2<SPAN class="punctuation token">.</SPAN>Mean_5year <SPAN class="operator token">=</SPAN> x
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> result_v2
rec<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">2005</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2006</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2007</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2008</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2009</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2010</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2011</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">6.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">6.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2012</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">7.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">7.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">2013</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">8.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2014</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">9.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">999.0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
dtype<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'year'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<i4'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Some_Data'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Mean_5year'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'<f8'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
So these snippets have shown some of the things that can be done with arrays and the subtle but important distinctions between numpy's array, ndarray and recarray forms.