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

learn more… | top users | synonyms

5
votes
1answer
60 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
66 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
39 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
368 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 ...
3
votes
2answers
76 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
110 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
72 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
70 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
87 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
72 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
65 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
31 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
76 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
50 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 ...
1
vote
0answers
23 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
310 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
132 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
64 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
367 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
48 views

Calculating minima values for each column of given data

Can anyone help make this perform faster? ...
0
votes
0answers
28 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
123 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
58 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
47 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
49 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
113 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: ...
6
votes
1answer
76 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
77 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
55 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
272 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
149 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
195 views
10
votes
2answers
176 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
90 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
118 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
249 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
117 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 ...
6
votes
1answer
116 views

What is the most pythonic way to solve a differential equation using the Euler method?

For example, with this differential equation, how can I do it in a pythonic way? dx/dt=f(x,t) I have a way to do it, but I think this is not the most pythonic ...
6
votes
2answers
405 views

Python, numpy, optimize module to handle big file

With the help of some people here and there, I wrote the module below which works very well if the file targets.csv is not too big. As soon as this file exceeds ...
6
votes
1answer
304 views

Faster computation of barycentric coordinates for many points

I'm just starting to understand the Python syntax and I created a module that does what I wanted, but really slow. Here are the stats of cProfile, top 10 ordered by ...
3
votes
1answer
119 views

Optimizing numpy gmean calculation

I'm trying to come up with an alternative to the gmean implementation in scipy, because it is awkwardly slow. To that end I've ...
5
votes
2answers
133 views

Defensive programming type-checking

I have issues with dynamically typed languages, and I tend to worry about type a lot. Numpy has different behaviour depending on if something is a matrix or a plain ndarray, or a list. I didn't ...
3
votes
2answers
124 views

How can I make my ant simulation faster?

I was not sure if I should post about 200 lines here. I want to make this ant simulation faster. The bottleneck is at Ant.checkdistancebetweenantsandfood(). It ...
2
votes
0answers
3k views

Reading columns and rows in a .csv file

I have some data in a .csv file, which looks roughly like this: ...
6
votes
2answers
3k views

Fastest way to iterate over Numpy array

I wrote a function to calculate the gamma coefficient of a clustering. The bottleneck is the comparison of values from dist_withing to ...
3
votes
1answer
168 views

Help improve my my python code for Exponential Smoothing Sumitted to Statsmodel

I just submitted this pull request to statsmodels (a Python toolkit for statistical and econometric modeling). It adds support for exponential smoothing of time ...
0
votes
1answer
364 views

Fastest way for working with itertools.combinations

I need to speed up the function below: ...
1
vote
2answers
72 views

Elegantly avoid columns containing NaNs?

The next bit of code does exactly what i want it to do, but it is 'rather' unelegant and unpythonic, so the question is quite simple: how can this code be changed into something more elegant ...
4
votes
1answer
63 views

Organization of complex empirical equations

I am working on coding up a set of models that are based on empirical models. These models tend to be quite complicated. In some cases, the logic used by the equations makes it impossible to compute ...