NumPy is the fundamental package for scientific computing with the programming language Python.
2
votes
0answers
20 views
“back substitution” method for solving linear system
I'm using numpy to write the "back substitution" method for solving linear system where "A" is a nonsingular upper triangular matrix.
...
1
vote
1answer
50 views
Latent Dirichlet Allocation in Python
I've recently finished writing a "simple-as-possible" LDA code in Python.
The theory from which I've developed my code can be found in the book Computer Vision by Simon Prince, free (courtesy of ...
2
votes
1answer
46 views
Applying a formula to 2D numpy arrays row-wise
The user has two 2D input arrays A and B, and a given matrix S. He wants to apply a
...
2
votes
1answer
33 views
Extracting an arbitrary element from each row of a np.array
I have a large numpy array of shape (n,m). I need to extract one element from each row, and I have another array of shape (n,) that gives the column index of the element I need. The following code ...
3
votes
0answers
45 views
ID3 Decision Tree in python
I've been working my way through Pedro Domingos' machine learning course videos (although the course is not currently active). His first homework assignment starts with coding up a decision tree ...
1
vote
3answers
31 views
Convert scalar arguments to arrays, and check that lengths are equal
I've got a Python function that takes two arguments, lat and lon. These arguments can be either scalar values (...
6
votes
1answer
72 views
Fastest possible Cython for Black-Scholes algorithm
I started with a pure python implementation, and have been trying to get the performance as close to native C as possible using numpy, numexpr, and cython. Here is the the numpy version that I ...
5
votes
1answer
35 views
Slicing time spans into calendar months
I have apparently correct code that still runs for weeks on my data (tens of millions of rows). I show the entire code for reference (and maybe other gains to be made), but the key operation is in the ...
2
votes
1answer
38 views
Create symmetrical matrix from list of values
I need to make a matrix (in the form of a numpy array) by taking a list of
parameters of length N and returning an array of dimensions N+1 x N+1 where the off-diagonals are symmetric and each triangle ...
4
votes
3answers
141 views
Kadane's Algorithm for 2D array with known boundaries
I asked this question first on StackOverflow but I didn't get an answer and was advised to try here too. So here we go.
I have implemented Kadane's algorithm for a 2D array in Python 2 with known ...
4
votes
5answers
55 views
Create matrices of a certain format
I need to create matrices that follow a specific format: the matrix is a square NxN matrix and the off-diagonals all have the same value (param) and the diagonals ...
11
votes
1answer
295 views
Balanced Random Assignment in Python
It has been a very long time since I've used Python.
What I'm looking for: I would like to create a 6-by-6 random matrix where each component is either 1 or 2, so that there are 18 ones and 18 twos.
...
11
votes
1answer
97 views
Racetrack plotter
My Racetrack is just that. A Racetrack. You can't race it (yet) because I had trouble with collision detection, but I wanted to share it anyway.
It creates a base polygon by using ...
2
votes
1answer
38 views
3
votes
0answers
43 views
2D disease problem modeling [closed]
I'm modeling a disease problem where each individual in a 2D landscape has a transmissibility described by a (radial basis) kernel function. My goal is to convolve the kernel with the population ...
3
votes
2answers
91 views
Duplicate the previous input if zero or not a number
I have the following code which duplicates the previous input if any entry is less than zero or NaN, except the first row of a matrix.
Is there any other efficient way to do this without using ...
6
votes
3answers
149 views
Numerical Python code to generate artificial data from a time series process
I'm writing code to generate artificial data from a bivariate time series process, i.e. a vector autoregression. This is my first foray into numerical Python, and it seemed like a good place to start.
...
8
votes
1answer
142 views
A big “Game of Life”
Our quest: Create a big simulation for Conway's Game of Life, and record the entire simulation history.
Current Approach: Cython is used for an iterate method. The ...
1
vote
1answer
35 views
Similar manipulations on several numpy arrays
I have n numpy arrays which all contain time series data for t periods. It is not an option to instead have one ...
10
votes
2answers
117 views
Numerics for a game theory calculation using expected utility
I am trying to replicate Bruce B. de Mesquita's (BDM) results on political game theory for prediction. Based on where actors stand on issues, their capabilities, salience, BDM's method attempts to ...
3
votes
1answer
146 views
Imputing values with non-negative matrix factorization
X is a DataFrame w/ about 90% missing values and around 10% actual values. My goal is to use nmf in a successive imputation loop to predict the actual values I have ...
4
votes
1answer
197 views
Order a list of tuples or a NumPy array in a specific format
I have a list of tuples or a NumPy array (it can be either of them as the variable comes as a list and will end up being a NumPy array) and I want to order them in a specific way (that I am not able ...
5
votes
1answer
138 views
Comparing FFTs to deduplicate IVR recordings
I've got a Python script that traverses two file trees, checking all .wav files for duplication.
I'm still an undergrad, and have never worked with audio before. I'm not entirely sure this is going ...
2
votes
2answers
204 views
Converting a 1D Array of Strings to a 2D Array in Python
I was solving this question on a site that gives you a 1d array called grid
...
1
vote
1answer
65 views
3
votes
1answer
76 views
Reimplementing numpy.genfromtxt in Fortran for Python
I've found that the function genfromtxt from numpy in Python is very slow.
Therefore I decided to wrap a subroutine with f2py to read my data. The data is a ...
4
votes
3answers
166 views
Rating tennis players in a database, taking days to run
I have this project in data analysis for creating a ranking of tennis players. Currently, it takes more than 6 days to run on my computer.
Can you review the code and see where's the problem?
...
7
votes
1answer
69 views
Weighted phase Laplacian formula
We have the following equation (weighted phase Laplacian):
\$c_{i,j} = U(i,j)\Delta^{^x}_{i,j} - U(i-l, j)\Delta^{x}_{i-1,j} + V(i,j)\Delta^{y}_{i,j} - V(i,j-1)\Delta^{y}_{i,j-1}\$
Where
\$U(i,j) = ...
2
votes
1answer
151 views
Plotting non continuous functions and a real scatter example with Bokeh
I've written a python 3 code using Bokeh. With this code you can learn howto to plot math functions and a scatter plot with regression linear functions in a webpage. The target is how to plot ...
5
votes
1answer
62 views
Creating a flood from a seed point in an array
I have created a python script that takes a 2D NumPy array of elevation values and given a seed value and an elevation it will grow a flood from that seed point. It does this by checking all adjacent ...
11
votes
1answer
652 views
Sending a C++ array to Python/NumPy and back
I am going to send a C++ array to a Python function as NumPy array and get back another NumPy array. After consulting with NumPy documentation and some other threads and tweaking the code, the code is ...
3
votes
2answers
61 views
Calculating doubling times from data points
In the code below, noisy data points with unique errors are created. From this, an exponential function is fitted to the data points, and then doubling times (10 unit windows) are calculated.
I'm ...
5
votes
1answer
88 views
Python factory method with easy registry
My aim is to define a set of classes, each providing methods for comparing a particular type of file. My idea is to use some kind of factory method to instantiate the class based upon a string, which ...
2
votes
2answers
97 views
Computation of population patterns in simple agent-based model
I've modeled the population changes for a group of agents that inhabit a 2D lattice (with wrapped boundaries) made up of M x M number of grids. At each time step:
...
8
votes
2answers
199 views
Approximate (250 over 100) permutation best fitting certain criteria
Given a list of 250 words of 4 letters each, what is the fastest way, in Python, to find a subsample of 100 words (drawn without replacement) so that the distribution of letters across the whole ...
5
votes
2answers
202 views
Efficient element-wise function computation in Python
I have the following optimization problem:
Given two np.arrays X,Y and a function K I ...
3
votes
1answer
145 views
NumPy array filter optimisation
I've got a filter that I'd like to work out how to optimise. The most basic implementation, using loops is like the following:
...
6
votes
1answer
132 views
Statistics about gaps in DNA sequences
Noobie to Numba here, I'm trying to get faster code from existing function but the result is not faster. 10 times faster would be heaven, but I know nothing about optimization.
This is code about ...
3
votes
1answer
132 views
Instantiating a kind of NumPy array
I am creating a class object which must inherit from numpy ndarrays. I perform an isinstance check and an ...
3
votes
3answers
137 views
Computation of inter-grid distance array for 2D lattice
I have a 2D lattice with U × U number of grids. My goal is to create an array – of shape U^2 × U^2 – where each entry denotes the least-distance between each grid and any other grid.
Currently, for ...
2
votes
1answer
158 views
Lattice lotka-volterra
Can anyone comment on the efficiency of my code? I am new with Python using NumPy/SciPy and I am trying to program a lattice biological model where a certain (one and only one) species (represented by ...
1
vote
2answers
50 views
Scaling and incrementing non-zero elements of a NumPy matrix
I have a NumPy matrix C and want create a copy of it cPrime, which has some operation of the original matrix to all non-zero ...
3
votes
1answer
190 views
Labeling alternating max/mins based on a user defined delta
Given a set of values (points on a graph) find all the max/min points such that
there is no max is followed by a max (i.e. the max and min points alternate)
there is some minimum amount of change ...
5
votes
1answer
175 views
Iterative equation solver in Python
In order to solve a equation where the left hand side appears under an integral on the right hand side:
$$
B(p^2) = C\int_0^{p^2}f_1\left(B(q^2),q^2\right)\mathrm{d}q^2 + C\int_{p^2}^{\Lambda^2} ...
3
votes
1answer
94 views
Vectorized numpy version of arange with multiple start stop
Given a single start and stop, numpy.arange is a good solution for building a NumPy array of evenly spaced values. However, given an array of start and an array of ...
4
votes
1answer
247 views
Triangle rasterization using a scanline algorithm for numpy array indexing
I need to access those elements of a large numpy array that lie in a small triangular area. The brute force solutions of using ...
8
votes
2answers
382 views
Predator Prey Simulation
Below is a simple random walk predator prey simulation that is optimized to the best of my abilities. I would love to hear about any improvements that can made.
...
2
votes
1answer
52 views
NumPy eliminate double loop
How can I vectorize this code snippet and eliminate this double loop? Even with "only" 1001 elements in a this takes almost 30s.
...
4
votes
2answers
123 views
Averaging lists of values with duplicate keys
I have gene expression data that I represent as a list of genes and a list of lists of values. I average the expression data for any genes with the same name.
For example:
...
5
votes
2answers
450 views
Opening and writing multiple files using minimum script
Can it possible to make this script short, so that multiple data and multiple output file can be managed efficiently?
...