Top-rated recipes tagged "numpy"http://code.activestate.com/recipes/tags/numpy/top/2016-03-19T12:57:38-07:00ActiveState Code RecipesFastest way to list all primes below N in python (Python)
2010-07-27T01:57:20-07:00robert.william.hankshttp://code.activestate.com/recipes/users/4174481/http://code.activestate.com/recipes/577331-fastest-way-to-list-all-primes-below-n-in-python/
<p style="color: grey">
Python
recipe 577331
by <a href="/recipes/users/4174481/">robert.william.hanks</a>
(<a href="/recipes/tags/fast/">fast</a>, <a href="/recipes/tags/fastest/">fastest</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/python/">python</a>).
Revision 6.
</p>
<p>I was looking for a fast way to list all primes below n,
so far i came up to this with the numpy solution the fastest.
It does primes up to 10e6 in 15ms in my old machine,
and it is capable of reaching 10e9. </p>
Saving a numpy array to png image using fitz / MuPDF (PyMuPDF) (Python)
2016-03-19T12:57:38-07:00Harald Liederhttp://code.activestate.com/recipes/users/4191581/http://code.activestate.com/recipes/580630-saving-a-numpy-array-to-png-image-using-fitz-mupdf/
<p style="color: grey">
Python
recipe 580630
by <a href="/recipes/users/4191581/">Harald Lieder</a>
(<a href="/recipes/tags/fitz/">fitz</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/mupdf/">mupdf</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/png/">png</a>, <a href="/recipes/tags/pymupdf/">pymupdf</a>).
Revision 2.
</p>
<p>If a pixel array is defined in numpy, it can be saved as a PNG image VERY fast if you use PyMuPDF.</p>
Monte Carlo Engine : Stochastic Interest Rate Predictor (Python)
2014-04-27T16:42:29-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578867-monte-carlo-engine-stochastic-interest-rate-predic/
<p style="color: grey">
Python
recipe 578867
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/carlo/">carlo</a>, <a href="/recipes/tags/interest/">interest</a>, <a href="/recipes/tags/model/">model</a>, <a href="/recipes/tags/monte/">monte</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/rate/">rate</a>).
Revision 2.
</p>
<p>This is a simple Monte Carlo Engine model, based on some ideas from the Quantopian folks. The idea is that the model and the engine are separate. I have integrated a simple example from an earlier post. The idea was to try and build some game theory simulations using simple models that were separate from the Engine. </p>
Conway's Game of Life In Python (Python)
2013-06-13T15:33:41-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578559-conways-game-of-life-in-python/
<p style="color: grey">
Python
recipe 578559
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/life/">life</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/of/">of</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>Conway's Game of Life In Python</p>
Coordinates of numpy array from index and shape (Python)
2012-10-23T19:51:55-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578302-coordinates-of-numpy-array-from-index-and-shape/
<p style="color: grey">
Python
recipe 578302
by <a href="/recipes/users/4181290/">Garrett</a>
(<a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/coordinates/">coordinates</a>, <a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>returns the coordinates of a numpy array given the index and the shape. A first_index_et function is given as example code</p>
How to read millions of hexadecimal numbers into a numpy array quickly (Python)
2012-06-27T06:03:40-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578177-how-to-read-millions-of-hexadecimal-numbers-into-a/
<p style="color: grey">
Python
recipe 578177
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
(<a href="/recipes/tags/numpy/">numpy</a>).
</p>
<p>The numpy.fromfile() function supports binary formats or decimal text. How do you read millions of hexadecimal numbers quickly?</p>
Control your mouse with the Microsoft Kinect using OpenKinect (Python)
2012-03-19T21:28:08-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/578082-control-your-mouse-with-the-microsoft-kinect-using/
<p style="color: grey">
Python
recipe 578082
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/computer/">computer</a>, <a href="/recipes/tags/control/">control</a>, <a href="/recipes/tags/freenect/">freenect</a>, <a href="/recipes/tags/kinect/">kinect</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/microsoft/">microsoft</a>, <a href="/recipes/tags/mouse/">mouse</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/openkinect/">openkinect</a>, <a href="/recipes/tags/vision/">vision</a>, <a href="/recipes/tags/xlib/">xlib</a>).
</p>
<p>This is a simple code that uses the Kinect in order to simulate the mouse on the screen. In order for this code to work you must have a Linux machine, the freenect module from OpenKinect, NumPy, and Xlib. The last three can be downloaded and installed by running sudo apt-get install {PROGRAM NAME HERE}. Freenect needs to be downloaded and installed for python using the terminal and there are good instructions here how to do it <a href="http://openkinect.org/wiki/Getting_Started" rel="nofollow">http://openkinect.org/wiki/Getting_Started</a>. You will also need to install the python wrapper. This can be done by following the instructions on this site: <a href="http://openkinect.org/wiki/Python_Wrapper" rel="nofollow">http://openkinect.org/wiki/Python_Wrapper</a>. To run the code you must first cd into the directory in which the code resides and then in the terminal run the command sudo python {THE NAME YOU STORED THE CODE BELOW AS}.py. This should run the code. The code needs superuser privileges. Also if you wonder why the mouse will stop tracking your hand, there are two reasons. 1) This code is not super sophisticated, all it does is find the minimum point in the depth image and scales the coordinates to the size of the screen and moves the mouse there so if your stomach is the closest thing to the screen it will track it and place the mouse there. The other reason the mouse might stop tracking is because there is a counter in the that will only let it run 10000 iterations. This is just a safety precaution in case you are not able to control the mouse and the computer freaks out. The last thing you need to know to run this code is that the main_mouse() function takes to arguments, the first is the x dimension of your screen and the second is the y dimension. THE CODE WILL NOT FUNCTION PROPERLY ON MULTIDISPLAY COMPUTERS. Thank You</p>
Libreenect (OpenKinect) Minimum Value Joystick With Display - Kinect Demo Using OpenKinect SDK (Python)
2012-03-18T18:52:42-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/578080-libreenect-openkinect-minimum-value-joystick-with-/
<p style="color: grey">
Python
recipe 578080
by <a href="/recipes/users/4179768/">Alexander James Wallar</a>
(<a href="/recipes/tags/3d/">3d</a>, <a href="/recipes/tags/computer_vision/">computer_vision</a>, <a href="/recipes/tags/depth/">depth</a>, <a href="/recipes/tags/joystick/">joystick</a>, <a href="/recipes/tags/kinect/">kinect</a>, <a href="/recipes/tags/libfreenect/">libfreenect</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/opencv/">opencv</a>, <a href="/recipes/tags/programming/">programming</a>, <a href="/recipes/tags/pygame/">pygame</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/virtual_joystick/">virtual_joystick</a>).
Revision 3.
</p>
<p>This demo requires you to be using a Linux machine and to have libfreenect installed with the python wrapper. This demo also requires you to have opencv, numpy, and pygame. They can all be installed using sudo apt-get install {PROGRAM NAME HERE}. Here are instructions on installing libfreenect: <a href="http://openkinect.org/wiki/Getting_Started" rel="nofollow">http://openkinect.org/wiki/Getting_Started</a>. Okay, so what this demo does is finds the minimum point on the depth image and uses the index of this minimum point to create a four button joystick that codes for 'A', 'B', 'X', 'Y'. It also uses the minimum point and the RGB image to put a circle on the minimum point on the screen. So basically a dot will follow your hand on the RGB image. If the minimum value is larger than 600 (arbitrary units), the color of the dot will change from red to purple and the dot will remain stationary. Also, if the Kinect is not properly opened the first time, unplug it and plug it back in and run in the terminal sudo freenect-glview. After try running the program again. One more thing, this code requires super user privileges so run it through the terminal. Here is how to do this for linux n00bs.</p>
<ol>
<li>Change your directory the the directory the code is in (use cd {PATH})</li>
<li>Run the code using sudo python {FILENAME}</li>
<li>Don't forget to add the the .py at the end</li>
</ol>
Fast Sorting of n dimensional array by first dimension (Python)
2012-03-14T15:14:11-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578074-fast-sorting-of-n-dimensional-array-by-first-dimen/
<p style="color: grey">
Python
recipe 578074
by <a href="/recipes/users/4181290/">Garrett</a>
(<a href="/recipes/tags/argsort/">argsort</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sorting/">sorting</a>).
</p>
<p>I have looked far and wide for code for fast sorting of n dimensional arrays by the first element, for example if I had the array: <br />
ray = [[1,2,3,7,5][10,11,12,13,14]] </p>
<p>I would want it to come out as
ray = [[1,2,3,5,7][10,11,12,14,13]]</p>
<p>There are several ways to do this. One is
zipped = zip(<em>ray)
zipped.sort()
ray = zip(</em>zipped)</p>
<p>but this is extremely slow. Numpy has a much faster way to do it, but it wasn't immediately apparent.</p>
<p>if the above were a numpy array you could simply do the following:
indexes = numpy.argsort(ray[0])
for n in xrange(len(ray))
ray[n] = ray[n][indexes]</p>
<p>I did a time test of the two methods below.</p>
Fast Indexing functions (greater than, less than, equal to, and not equal to) (Python)
2012-03-13T16:21:36-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578071-fast-indexing-functions-greater-than-less-than-equ/
<p style="color: grey">
Python
recipe 578071
by <a href="/recipes/users/4181290/">Garrett</a>
(<a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/list_comprehension/">list_comprehension</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>).
Revision 2.
</p>
<p>Oftentimes you want to find the index of a list-like object. Numpy arrays, for example, do not have a index member function. These get the job done quickly.</p>
<p>Note: these do not raise exceptions, instead they return -1 on error. You should change that if you want different behavior.</p>
Conversion of PIL Image and numpy array (Python)
2011-03-05T03:47:56-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577591-conversion-of-pil-image-and-numpy-array/
<p style="color: grey">
Python
recipe 577591
by <a href="/recipes/users/4168519/">Shao-chuan Wang</a>
(<a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/pil/">pil</a>).
Revision 2.
</p>
<p>The utility function that converts PIL image to numpy array and vice versa.</p>
A fast & memory-wise prime number generator up to N (Python)
2010-08-13T18:28:00-07:00robert.william.hankshttp://code.activestate.com/recipes/users/4174481/http://code.activestate.com/recipes/577357-a-fast-memory-wise-prime-number-generator-up-to-n/
<p style="color: grey">
Python
recipe 577357
by <a href="/recipes/users/4174481/">robert.william.hanks</a>
(<a href="/recipes/tags/fast/">fast</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/prime_generator/">prime_generator</a>, <a href="/recipes/tags/prime_number/">prime_number</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Using python 2.6 & numpy.
This code was first posted <a href="http://stackoverflow.com/questions/2897297/speed-up-bitstring-bit-operations-in-python/2908512#2908512">here</a></p>
Incremental PCA (Python)
2010-05-01T18:04:39-07:00Micha Kalfonhttp://code.activestate.com/recipes/users/4173851/http://code.activestate.com/recipes/577213-incremental-pca/
<p style="color: grey">
Python
recipe 577213
by <a href="/recipes/users/4173851/">Micha Kalfon</a>
(<a href="/recipes/tags/incremental/">incremental</a>, <a href="/recipes/tags/klt/">klt</a>, <a href="/recipes/tags/learning/">learning</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/pca/">pca</a>, <a href="/recipes/tags/scipy/">scipy</a>, <a href="/recipes/tags/svd/">svd</a>, <a href="/recipes/tags/vision/">vision</a>).
</p>
<p>An incremental PCA algorithm in python. </p>
<p>Incremental PCA allows computing an approximation of the principal components on large data sets as observations are given sequentially.</p>
Calculations on huge (memmap-)arrays (Python)
2009-05-05T10:19:37-07:00d.schlabinghttp://code.activestate.com/recipes/users/4168903/http://code.activestate.com/recipes/576739-calculations-on-huge-memmap-arrays/
<p style="color: grey">
Python
recipe 576739
by <a href="/recipes/users/4168903/">d.schlabing</a>
(<a href="/recipes/tags/memmap/">memmap</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>This is a way of doing calculations on whole arrays that might be to big to fit into memory.</p>
Poor man's mgrid (Python)
2009-01-21T15:37:13-08:00David Lamberthttp://code.activestate.com/recipes/users/4167420/http://code.activestate.com/recipes/576625-poor-mans-mgrid/
<p style="color: grey">
Python
recipe 576625
by <a href="/recipes/users/4167420/">David Lambert</a>
(<a href="/recipes/tags/index/">index</a>, <a href="/recipes/tags/index_tricks/">index_tricks</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/mgrid/">mgrid</a>, <a href="/recipes/tags/numerical/">numerical</a>, <a href="/recipes/tags/numerical_methods/">numerical_methods</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/scipy/">scipy</a>, <a href="/recipes/tags/trick/">trick</a>).
</p>
<p>Python 3 code.
scipy.mgrid is a useful!
This short implementation comes without installing numpy.</p>
Locate and see the biggest cross in a random matrix (Python)
2013-09-13T13:31:03-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578657-locate-and-see-the-biggest-cross-in-a-random-matri/
<p style="color: grey">
Python
recipe 578657
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/search/">search</a>).
</p>
<p>Interesting search space problem.</p>
Hartree Fock SCF (Python)
2013-04-30T09:09:20-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578515-hartree-fock-scf/
<p style="color: grey">
Python
recipe 578515
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/physics/">physics</a>).
Revision 4.
</p>
<p>Quantum Chemistry technique to calculate various interesting operators for HeH.</p>
pauli spin matricies (Python)
2008-08-26T01:52:06-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/576458-pauli-spin-matricies/
<p style="color: grey">
Python
recipe 576458
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/kron/">kron</a>, <a href="/recipes/tags/ladder/">ladder</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/pauli/">pauli</a>, <a href="/recipes/tags/physics/">physics</a>, <a href="/recipes/tags/spin/">spin</a>).
</p>
<p>simple spin investigation in python</p>
Infix operators for numpy arrays (Python)
2010-06-07T05:57:07-07:00John Schulmanhttp://code.activestate.com/recipes/users/4171677/http://code.activestate.com/recipes/577201-infix-operators-for-numpy-arrays/
<p style="color: grey">
Python
recipe 577201
by <a href="/recipes/users/4171677/">John Schulman</a>
(<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/infix/">infix</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/multiplication/">multiplication</a>, <a href="/recipes/tags/numpy/">numpy</a>).
Revision 3.
</p>
<p>This recipe adapts the infix operator trick from <a href="http://code.activestate.com/recipes/384122-infix-operators/" rel="nofollow">http://code.activestate.com/recipes/384122-infix-operators/</a> to give the appropriate behavior with numpy arrays, so you can write A *dot* B for np.dot(A,B)</p>
<p>UPDATE
A solution to the dot problem was recently added to the numpy trunk: the dot method was added to the ndarray class so you can write a.dot(b). See <a href="http://projects.scipy.org/numpy/ticket/1456" rel="nofollow">http://projects.scipy.org/numpy/ticket/1456</a></p>