Popular recipes tagged "meta:requires=numpy"http://code.activestate.com/recipes/tags/meta:requires=numpy/2016-09-22T12:25:30-07:00ActiveState Code RecipesSimple Matlab/Ocave like arrays conversion to numpy.arrays in python interpreter (Python) 2016-09-22T12:25:30-07:00Przemyslaw Podczasihttp://code.activestate.com/recipes/users/4179716/http://code.activestate.com/recipes/580700-simple-matlabocave-like-arrays-conversion-to-numpy/ <p style="color: grey"> Python recipe 580700 by <a href="/recipes/users/4179716/">Przemyslaw Podczasi</a> (<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/interpreter/">interpreter</a>, <a href="/recipes/tags/matlab/">matlab</a>). </p> <p>Matlab/Octave syntax for 1D/2D arrays is more packed and doesn't require putting extra ',' and extra '[', ']' between dimensions. For this I wrote a parser that intercepts python interpreter and using numpy functionality parses Matlab's style arrays 1D and 2D into numpy.arrays.</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> least square fitting (Python) 2015-10-03T15:32:28-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/579106-least-square-fitting/ <p style="color: grey"> Python recipe 579106 by <a href="/recipes/users/4184815/">yota</a> (<a href="/recipes/tags/fitting/">fitting</a>). Revision 2. </p> <p>a generic python code to fit points to a given curve, was made for a paraboloid, but can be easily expanded to many kind of curves</p> Delaunay triangulation (Python) 2015-02-08T15:24:11-08:00Alexander Pletzerhttp://code.activestate.com/recipes/users/4190754/http://code.activestate.com/recipes/579021-delaunay-triangulation/ <p style="color: grey"> Python recipe 579021 by <a href="/recipes/users/4190754/">Alexander Pletzer</a> (<a href="/recipes/tags/delaunay/">delaunay</a>, <a href="/recipes/tags/edge/">edge</a>, <a href="/recipes/tags/point/">point</a>, <a href="/recipes/tags/set/">set</a>, <a href="/recipes/tags/tessellation/">tessellation</a>, <a href="/recipes/tags/triangles/">triangles</a>, <a href="/recipes/tags/triangulation/">triangulation</a>, <a href="/recipes/tags/vertices/">vertices</a>). </p> <p>Given a set of points, this code will construct a Delaunay triangulation.</p> Python A* Pathfinding (With Binary Heap) (Python) 2014-08-06T10:03:01-07:00Christian Careagahttp://code.activestate.com/recipes/users/4186639/http://code.activestate.com/recipes/578919-python-a-pathfinding-with-binary-heap/ <p style="color: grey"> Python recipe 578919 by <a href="/recipes/users/4186639/">Christian Careaga</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/binary_search/">binary_search</a>). Revision 3. </p> <p>Implementation of the A-star Pathfinding algorithm in Python, using Binary heap to sort the open list</p> twierdzenie Taylora (Python) 2015-01-23T10:20:57-08:00Paula Baranowskahttp://code.activestate.com/recipes/users/4191523/http://code.activestate.com/recipes/579011-twierdzenie-taylora/ <p style="color: grey"> Python recipe 579011 by <a href="/recipes/users/4191523/">Paula Baranowska</a> . </p> <p>modul 1 kolokwium</p> oscylator (Python) 2015-01-23T10:19:26-08:00Paula Baranowskahttp://code.activestate.com/recipes/users/4191523/http://code.activestate.com/recipes/579010-oscylator/ <p style="color: grey"> Python recipe 579010 by <a href="/recipes/users/4191523/">Paula Baranowska</a> . </p> <p>modul 3 kolokwium</p> Teach your computer a few tricks (Python) 2014-09-11T06:25:14-07:00Alexander Pletzerhttp://code.activestate.com/recipes/users/4190754/http://code.activestate.com/recipes/578932-teach-your-computer-a-few-tricks/ <p style="color: grey"> Python recipe 578932 by <a href="/recipes/users/4190754/">Alexander Pletzer</a> (<a href="/recipes/tags/anl/">anl</a>, <a href="/recipes/tags/back/">back</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/propagation/">propagation</a>). </p> <p>Following is an artifical neural network program that takes any number of inputs and any number of hidden layers, and spits out an output. It applies back propagation with regularization to minimize the cost function. A gradient descent algorithm tries to find the minimum of the cost function in the landscape of weights. </p> Simple model to predict UK Interest Rates (Python) 2014-06-13T14:29:36-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578891-simple-model-to-predict-uk-interest-rates/ <p style="color: grey"> Python recipe 578891 by <a href="/recipes/users/4166679/">alexander baker</a> . </p> <p>A common model used in the financial industry for modelling the short rate (think overnight rate, but actually an infinitesimally short amount of time) is the Vasicek model. # Although it is unlikely to perfectly fit the yield curve, it has some nice properties that make it a good model to work with. The dynamics of the Vasicek model are describe below.</p> <p>In this model, the parameters are constants, and the random motion is generated by the Q measure Brownian motion . An important property of the Vasicek model is that the interest rate is mean reverting to , and the tendency to revert is controlled by . Also, this process is a diffusion process, hence Markovian, which will lead to some nice closed form formulas. Finally, the future value of the interest rate is normally distributed with the distribution .</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> 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> Monte Carlo Engine : How to find the optimised wager for next bet, following a recent loss. (Python) 2014-04-28T08:41:49-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578869-monte-carlo-engine-how-to-find-the-optimised-wager/ <p style="color: grey"> Python recipe 578869 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/optimise/">optimise</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tree/">tree</a>). </p> <p>Simple Engine to help understand how to best wager your next bet, given that you just made a loss. The engine uses the modified Powell method to optimise the weight to apply to your wager on the next position.</p> <p>{'My Simple Heads And Tails Model': &lt;BackTest.Simulation object at 0x0583D410&gt;} participants [100] survivors [90.0%] losers [10.0%] weight [0.073858] solving for r: [ 0.07385806] simulations 100, trials 100 starting pot 1000 calling initialise {'My Simple Heads And Tails Model': &lt;BackTest.Simulation object at 0x0583D430&gt;} participants [100] survivors [86.0%] losers [14.0%] weight [0.072220] solving for r: [ 0.07221954] Optimization terminated successfully. Current function value: 8.000000 Iterations: 2 Function evaluations: 30 highest survivability following loss, multiply wager by 7.2949 %</p> <h5 id="">.</h5> <p>Ran 2 tests in 25.545s</p> <p>OK</p> Monte Carlo Engine : Simple Head Tail Model (Python) 2014-04-27T17:25:25-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578868-monte-carlo-engine-simple-head-tail-model/ <p style="color: grey"> Python recipe 578868 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/carlo/">carlo</a>, <a href="/recipes/tags/monte/">monte</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/strategy/">strategy</a>). Revision 2. </p> <p>Wanted to implement something that shows how the simple framework can be used to investigate a basic heads and tail game. The betting strategy can be varied simply by changing the logic in the ontrail() method. This currently tests the double or quits strategy. Run it to see how many </p> Hierarchical Clustering Heatmap Python (Python) 2014-02-19T11:09:36-08:00xapplehttp://code.activestate.com/recipes/users/4189256/http://code.activestate.com/recipes/578834-hierarchical-clustering-heatmap-python/ <p style="color: grey"> Python recipe 578834 by <a href="/recipes/users/4189256/">xapple</a> (<a href="/recipes/tags/clustering/">clustering</a>, <a href="/recipes/tags/heatmap/">heatmap</a>, <a href="/recipes/tags/hierarchical/">hierarchical</a>, <a href="/recipes/tags/microarray/">microarray</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/rnaseq/">rnaseq</a>). Revision 2. </p> <p>A python class that performs hierarchical clustering and displays a heatmap using scipy and matplotlib. Improved to be require only as input a pandas DataFrame.</p> Multidimensional Pareto front (Python) 2012-10-13T23:26:12-07:00Jamie Bullhttp://code.activestate.com/recipes/users/4183059/http://code.activestate.com/recipes/578287-multidimensional-pareto-front/ <p style="color: grey"> Python recipe 578287 by <a href="/recipes/users/4183059/">Jamie Bull</a> (<a href="/recipes/tags/engineering/">engineering</a>, <a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/optimal/">optimal</a>, <a href="/recipes/tags/pareto/">pareto</a>, <a href="/recipes/tags/pareto_front/">pareto_front</a>, <a href="/recipes/tags/pareto_frontier/">pareto_frontier</a>, <a href="/recipes/tags/pareto_set/">pareto_set</a>). Revision 2. </p> <p>Method for finding non-dominated options in multi-dimensional data.</p> Find the largest run of Heads or Tails with tolerance (Python) 2013-12-04T10:55:51-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578779-find-the-largest-run-of-heads-or-tails-with-tolera/ <p style="color: grey"> Python recipe 578779 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/interview/">interview</a>, <a href="/recipes/tags/question/">question</a>). </p> <ul> <li>You need to find the </li> </ul> 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> Running median, mean and mode (Python) 2013-03-06T17:33:32-08:00Virgil Stokeshttp://code.activestate.com/recipes/users/4183795/http://code.activestate.com/recipes/578480-running-median-mean-and-mode/ <p style="color: grey"> Python recipe 578480 by <a href="/recipes/users/4183795/">Virgil Stokes</a> (<a href="/recipes/tags/mean/">mean</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/mode/">mode</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/running/">running</a>). </p> <p>The following is a small contribution that I hope can be useful to Python programmers for the calculation of the running median, mean and mode. It is important to note that all the "running" calculations are done for full windows. Here is a simple example:</p> <p>y = [1, 2, 3, 3, 1, 4], with a sliding window of size = 3 for the running estimations, means = 2, 2.6667, 2.3333, 2.6667 medians = 2, 3, 3, 3 modes = 1, 3, 3, 1</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> A simple KD Tree example with custom Euclidean distance ball query. (Python) 2013-01-24T10:28:24-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578434-a-simple-kd-tree-example-with-custom-euclidean-dis/ <p style="color: grey"> Python recipe 578434 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>In computer science, a k-d tree (short for k-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space. k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches). k-d trees are a special case of binary space partitioning trees.</p> <p><a href="http://en.wikipedia.org/wiki/K-d_tree" rel="nofollow">http://en.wikipedia.org/wiki/K-d_tree</a></p>