NumPy is the fundamental package for scientific computing with the programming language Python.

learn more… | top users | synonyms

5
votes
1answer
54 views

Monte Carlo estimation of the Hypergeometric Function

I am trying to implement the algorithm described in the paper Statistical Test for the Comparison of Samples from Mutational Spectra (Adams & Skopek, 1986) DOI: 10.1016/0022-2836(87)90669-3: $$p ...
2
votes
0answers
36 views

K-Mean with Numpy

I have implemented the K-Mean clustering Algorithm in Numpy: ...
5
votes
1answer
91 views

Groupby in NumPy

To avoid the XY problem, here is an example of what I need. Given the sorted integer input array [1 1 1 2 2 3], I would like to produce the following slices, ...
4
votes
1answer
38 views

Stable partition in Numpy

In essence, I need to do a stable partition: All elements of the 1D np.array a that are present in ...
3
votes
1answer
58 views

Where is the bottleneck in my Cython code?

The profile tells me it took ~15s to run, but without telling me more. ...
1
vote
1answer
47 views

Neighbours from point connections

I am working with a mesh of triangles (in 3D, although I doubt it makes a difference). The mesh is given as list of lists, each list containing the indices of the three vertices of a triangle in said ...
3
votes
0answers
30 views

Parallelizing some model fitting code using multiprocessing

I'm trying to refactor some code for fitting parametric models that are defined symbolically using Theano. My goal is for all models to expose a common interface so that, as far as possible, they can ...
2
votes
1answer
47 views

How can I make this implementation of Euler's method more efficient?

I have an implementation of an Euler method for solving N-many 1st order coupled differential equations, but I feel that I did not write it as efficiently as I could, due to lack of programming ...
1
vote
0answers
15 views

Function that transforms input file based on a numpy poly1d function

I have written a function that gets a function as input (acquired from numpy.poly1d). The function has access to the GUI variables ...
3
votes
2answers
82 views

Optimization/speed up of reduction function

I'm working on an implementation of Rainbow tables as part of a project. I understand the main principles behind it had have a working solution. Unfortunately the reduction function portion of my ...
8
votes
1answer
136 views

Interpolate then maximize

I need to optimize $$ \max_{a',\ m'} f(a, m, e, m', a') $$ I have approximated \$f\$ with a grid \$F\$ — it has shape \$(nA, nM, nE, nM, nA)\$. Now, I want to interpolate over the last two ...
3
votes
2answers
73 views

Calculating mesh centers runs very slowly

I have a piece of code that is used to calculate the cell centers in a rectangular mesh. The mesh size is pretty large (650 columns by 1150 rows). The code runs very slowly. I was wondering if I could ...
1
vote
1answer
46 views

Hints to make Sukoku solver more Pythonic (II)

I tried to implement as many changes suggested as possible as well as expanding the code base according to the suggested link. The original question can be found here. It does solve Sudokus gives ...
5
votes
1answer
386 views

Hints to make Sudoku solver more Pythonic

As you will see, I am not very familiar with Python and NumPy but want to learn it. The following code is a very basic Sudoku solver which works fine for simple examples. Although it runs, I still ...
4
votes
1answer
70 views

Fixed point iteration and cobweb plot

I'm using Python to find fixed points of a given function and then draw a cobweb plot to visualize it. Thanks to this question, I have the core of the code written and can accomplish the task, but I ...
3
votes
2answers
90 views

Connect 4 game for AI agents

This Connect 4 game will be used for implementing game-playing AI. Sample players are supplied. One takes user input, and the other plays randomly. Right now it's set for a human player to play ...
4
votes
1answer
161 views

Efficient numpy cosine distance calculation

I want to calculate the nearest cosine neighbors of a vector using the rows of a matrix, and have been testing the performance of a few Python functions for doing this. ...
4
votes
1answer
86 views

Flood fill algorithm

I am implementing a flood fill algorithm using Python and NumPy. I have written the following fill function which works fine: ...
3
votes
1answer
90 views

Calculating the distance between one point, and many others

In my program, I have entities that I call "blobs", because they have a blobby shape. Blobs are polygons. If I have two blobs, then their information array would look like: ...
3
votes
2answers
104 views

Calculating if a point is within a polygon, or outside of it

Here is the function (along with its support functions): ...
5
votes
1answer
88 views

Calculating “element-wise” the angles between two lists of vectors

Let's say you have two lists of vectors: v1s = [a, b, c] v2s = [d, e, f] I am interested in generating the following ...
5
votes
2answers
70 views

Taking wind data and simulating future wind profiles

I am new to programming, and am using Python to take wind data and simulate future wind profiles. The code as written takes a while to execute and I was hoping someone could suggest ways to make my ...
1
vote
0answers
40 views

Cythonized version of FDCT (fast discrete cosine transform) function, ported from Java

This is my Cython code for an FDCT function (invoked here): ...
3
votes
1answer
82 views

Point in a polygon algorithm

I am implementing a Point in polygon algorithm. Inputs: M, N: size of the matrix poly: a ...
3
votes
1answer
56 views

Fast loop to create an array of values

I have a code that creates a 3D array of values from a minimum to a maximum in X and Z with constant Y. Right now I make it in normal Python, and then I transform it in a ...
2
votes
0answers
29 views

plotting many images at once in matplotlib

I am using numpy and matplotlib to do a statistical simulation. The simulation itself is pretty fast thanks to numPy vectorizatio, however the plotting is slow since I still use a for loop. Here is ...
8
votes
2answers
351 views

Possible optimizations for calculating squared euclidean distance

I need to do a few hundred million euclidean distance calculations every day in a Python project. Here is what I started out with: ...
4
votes
2answers
218 views

K-means clustering in Python

The following code uses scikit-learn to carry out K-means clustering where \$K = 4\$, on an example related to wine marketing from the book DataSmart. That book uses excel but I wanted to learn Python ...
2
votes
0answers
70 views

implementation of a new algorithm for sklearn

In the python library sklearn is implemented the algorithm for SparsePCA. I have written the code for a another version of this algorithm that is much faster in some situations. I have not enough ...
5
votes
1answer
379 views

Sieve of Eratosthenes - Standard and Optimized implementation

I am a Java developer who is taking Python for the first time. I'm sure this is not at all elegant since I am thinking more in C syntax. ...
4
votes
1answer
49 views

Calculating minima values for each column of given data

Can anyone help make this perform faster? ...
0
votes
0answers
29 views

optimising python code for visualization waves

solution, i make it few weeks ago, but lost forgotten. any advice how to make code more optimal would be great! file: A_DATA_time_space.py ...
0
votes
1answer
125 views

My Python code (for creation of MxM matrix) is much slower than my Matlab version

I have written the same code in Python (NumPy) and in Matlab, and I tried to use the same structure for both language and follow the same procedure. Now my problem is that when I run my code in Python ...
4
votes
1answer
61 views

Performing a special multiplication on two square matrices

I have a function called mult2 that takes in two square NumPy matrices and returns a matrix that is the result of a special multiplication: ...
6
votes
1answer
60 views

Average of positive and negative part of numpy matrix

I’ve got a matrix of data and I want to take the average of the positive and negative parts of the values. Is there a more Pythonic way of doing it? ...
5
votes
1answer
51 views

Determining the least-accurately known physical constants

So, I thought I'd do this using scipy.constants: ...
3
votes
1answer
53 views

Building a dict/list of lists

I have code which is matching features within regions. The first thing it does it matches features from one region to an entire database of region features. I have a mapping from query feature index ...
12
votes
1answer
120 views

Temperature Interpolation

I want to interpolate temperature in correlation with height. I have temperature data from stations with coordinates and height in this format: ...
1
vote
1answer
221 views

Parse Bloomberg Excel/CSV with Pandas DataFrame

I retrieved Bloomberg data using the Excel API. In the typical fashion, the first row contains tickers in every fourth column, and the second row has the labels Date, PX_LAST, [Empty Column], Date, ...
6
votes
1answer
98 views

Calculating Euclidean norm for each vector in a sparse matrix

Below is a naive algorithm to find nearest neighbours for a point in some n-dimensional space. ...
2
votes
0answers
109 views

numpy array as quasi-hash table

Motivation: I have a large number of large read-only dicts with large string keys mapped to tuples of two floats. They are taking up a large amount of heap space. Proposed solution: a 3xn numpy ...
4
votes
1answer
62 views

Pythonic way to manipulate arrays: a case study

I just spent almost one day to figure out the following code, which analyzes the output data from material science programs called VASP and Bader. As a Python beginner, I earnestly ask some aces to ...
5
votes
1answer
277 views

How can I optimize this very simple piece of “Game of Life” code by taking advantage of NumPy's functionality?

It was suggested to me that I should post this Stack Overflow post over here. Here is the code as it stands right now: ...
13
votes
1answer
169 views

Finding a zero crossing in a matrix

I am trying create an algorithm for finding the zero crossing (check that the signs of all the entries around the entry of interest are not the same) in a two dimensional matrix, as part of ...
4
votes
2answers
205 views
10
votes
2answers
222 views

Calculating the joint probability of n events from a sample sequence of occurrences

I'm writing an algorithm to take in a sample list of sequences of events, calculate 1-step transitional probabilities from the sequences, forward or in reverse, then calculate the joint probability of ...
4
votes
1answer
97 views

KMeans in the shortest and most readable format

I'm learning Python (coming from Java) so I decided to write KMeans as a practice for the language. However I want to see how could one improve the code and making it shorter and yet readable. I ...
5
votes
1answer
127 views

Improve performance of Gaussian kernel function evaluation

I need to improve the performance of a function that calculates the integral of a two-dimensional kernel density estimate (obtained using the function stats.gaussian_kde) where the domain of ...
2
votes
1answer
306 views

Speedup cython dictionary counter

I wrote a simple cython script to optimize the collections.Counter of a dictionary counter and the python zip implementation (the main input is a list of tuples). Is there a way to speed it up? ...
10
votes
2answers
121 views

Improve performance of math function applied to arrays

This block of code is already somewhat optimized thanks to some answers given over at Stack Overflow. In the last question I made (Improve performance of function without parallelization) the code was ...