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

learn more… | top users | synonyms

4
votes
1answer
39 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
28 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
42 views

Determining the least-accurately known physical constants

So, I thought I'd do this using scipy.constants: ...
2
votes
0answers
33 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. So I have a mapping from query feature ...
12
votes
1answer
83 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
28 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. ...
1
vote
0answers
20 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
46 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
233 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
94 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
153 views
10
votes
2answers
83 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
57 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
87 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
124 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
106 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
102 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
186 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
173 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
92 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 ...
4
votes
2answers
116 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
109 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
958 views

Reading columns and rows in a .csv file

I have some data in a .csv file, which looks roughly like this: ...
4
votes
2answers
1k 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
109 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
205 views

Fastest way for working with itertools.combinations

I need to speed up the function below: ...
1
vote
2answers
69 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
56 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 ...
1
vote
1answer
100 views

Downsampling n-dimensional data from bins in one dimension

So I'm writing some code to perform a quite specific task given a large numpy array with N rows and 3 columns representing points in 3D. The 3D points are to be binned along one of the dimensions ...
0
votes
1answer
55 views

How to reduce runtime of gene data processing program?

This program takes a huge data set as input, processes it, calculates and then writes the output to an array. Most calculations may be quite simple, such as summation. In the input file, there are ...
1
vote
2answers
40 views

How can this code be made more Pythonic?

This next part of code does exactly what I want it to do. dem_rows and dem_cols contain float values for a number of things i ...
1
vote
3answers
285 views

NumPy Scipy optimization

I have an optimization issue and I'm not sure if I can improve the overall speed of my function. The function draw_w is my actual implementation that gives me the ...
4
votes
1answer
197 views

Optimizing NumPy code

I'm try to implement SSIM method. This method has already been implemented in Python (source code), but my goal is implement it with using only Python and NumPy. Also my goal is to using this method ...
4
votes
1answer
187 views

Optimizing numpy/python for speed

This is my first real python script after "Hello, World!" I'd really appreciate feedback to make it faster and more efficient. The purpose of this script is to calculate the nonlinear reflection ...
2
votes
1answer
95 views

What would be the optimal way to address these arrays

I think the arrays here are slowing down my code due to incorrect usage. There is a lot of stuff going on here, it's understandable if nobody responds. Just looking for some pointers. Thanks. ...
4
votes
2answers
874 views

Is this the fastest way to find the closest point to a list of points using numpy?

I'm trying to find the closest point (Euclidean distance) from a user inputed point to a list of 50,000 points that I have. Note that the list of points changes all the time. and the closest distance ...
3
votes
0answers
48 views

Summing 2D NumPy array by multiple lables

Each element of the array is a coordinate (x, y). Each coordinate has two labels. Goal: sum the elements that have the same two labels. How can this be faster? ...
2
votes
1answer
87 views

Eliminate for loops in numpy implementation

I have the following dataset in numpy ...
5
votes
2answers
441 views

Critiques on a trivially easy to use Python CSV class

I have been working on a project where I needed to analyze multiple, large datasets contained inside many CSV files at the same time. I am not a programmer but an engineer so I did a lot of searching ...
2
votes
1answer
369 views

Using numpy polynomial module - is there a better way?

I'm working on a project where I need to solve for one of the roots of a quartic polymonial many, many times. Is there a better, i.e., faster way to do this? Should I write my own C-library? The ...
2
votes
1answer
67 views

How can I speed up this correlation function Python

I am writing a specialized version of the cross correlation function as used in neuroscience. The function below is supposed to take a time series data and ask how ...
3
votes
1answer
349 views

Are these list-comprehensions written the fastest possible way?

This is a simple repeatable question regarding the usage of Python3's comprehensions: Could the Python3 syntax be used in another way to speed up the process, further so the gap between Python3 and ...
2
votes
1answer
142 views

Performance problems with 1D cellular automata experiment in Python with Numpy

I'm an experienced programmer but relatively new to Python (a month or so) so it's quite likely that I've made some gauche errors. I very much appreciate your time spent taking a look at my code. My ...
0
votes
1answer
135 views

Figuring out the structure of a shape tuple

I have some code that uses a multidimensional look-up table (LUT) to do interpolation. The typical application is a color space conversion, where 3D inputs (RGB) are converted to 4D (CMYK), but the ...
2
votes
3answers
315 views

A pythonic way of de-interleaving a list (i.e. data from a generator), into multiple lists

I've recently discovered the wonders of the Python world, and am quickly learning. Coming from Windows/C#/.NET, I find it refreshing working in ...
2
votes
0answers
214 views

Is there better way to read from a file and connect all data in one big data than to use generators?

Is there better way to read from a file and connect all data in one big data than to use generators? At the moment, I do the following: use generators to read data from files. use numpy to pack all ...
3
votes
1answer
198 views

How can I make this code faster using Numpy?

I have been reading some Numpy guides but can't seem to figure it out. my TA told me I should be able to speed up my code by using a numpy array instead of a for loop in the following segment of code. ...
4
votes
0answers
341 views

Implementing the CutHill-McKee Algorithm

I am very much interested in the Reverse Cuthil McKee Algorithm. I have seen Fortran and C or C++ implementations of it, and I decided that it would be a nice exercise to implement it in Python. I ...
4
votes
1answer
196 views

Optimizing Python / Numpy Code

So I'm implementing a version of the mean shift image processing algorithm for color segmentation in python/numpy. I've written a pure numpy version of the actual mean shifting per pixel (which I ...
6
votes
2answers
746 views

Python / Numpy running 15x slower than MATLAB - am I using Numpy effeciently?

Here is some code I wrote in Python / Numpy that I pretty much directly translated from MATLAB code. When I run the code in MATLAB on my machine, it takes roughly 17 seconds. When I run the code in ...