Hot answers tagged

35 votes
Accepted

Scale Numpy array to certain range

NumPy provides numpy.interp for 1-dimensional linear interpolation. In this case, where you want to map the minimum element of the array to −1 and the maximum to +1,...
Gareth Rees's user avatar
  • 49.6k
32 votes
Accepted

Looking for a way to create a particular matrix in python in a less amount of time

The NumPy Reference should be the first place you look when you have a problem like this. The operations you need are nearly always in there somewhere. And functions that you find while browsing the ...
Gareth Rees's user avatar
  • 49.6k
31 votes

Python decibel meter-accurate?

Indent your code with a PEP8-compliant IDE or linter; it's a perfect mess right now. Move your global code into functions and maybe classes. There are two good use cases for classes here - one for a ...
Reinderien's user avatar
  • 59.1k
29 votes
Accepted

Crop black border of image using NumPy

Vectorization with NumPy When read with cv2.imread or skimage.io.imread or scipy.misc.imread,...
Divakar's user avatar
  • 896
25 votes
Accepted

Remove points in a straight line

A look at the second plot in the post shows that something has gone wrong. There are four points in a line here: This shouldn't be possible, since "points that create a straight line when plotted" ...
Gareth Rees's user avatar
  • 49.6k
23 votes

Finding the closest point to a list of points

All your code could be rewritten as: ...
arekolek's user avatar
  • 371
22 votes

Crop black border of image using NumPy

As you've discovered, looping over individual pixels in Python is very slow. You need to organize your computation so that it uses a series of NumPy (or SciPy, or Scikit-Image, or OpenCV) operations ...
Gareth Rees's user avatar
  • 49.6k
18 votes
Accepted

Remove pixel patch in image which is stored as array

On my computer it takes 1.745 seconds to run the code in the post. There's no need for the array of random indexes to be two-dimensional: ...
Gareth Rees's user avatar
  • 49.6k
18 votes
Accepted

Iterate through two arrays calculating a statistic from indexes of zeros

First, some basics of your implementation: Type-hint your arguments; I had to read and investigate a bunch to deduce that tau is a positive integer ...
Reinderien's user avatar
  • 59.1k
16 votes
Accepted

Comparing pixels against RGB value in NumPy

Your original code can be rewritten as: mask = np.all(img == (255, 127, 63), axis=-1) It is a little cleaner, but not more efficient, as it still has to allocate ...
Jaime's user avatar
  • 6,562
14 votes

Processing an image to extract green-screen mask

Use conditional indexing: ...
aghast's user avatar
  • 12.3k
13 votes
Accepted

Python program that draws the Mandelbrot set fractal

The key thing to remember when working with numerical code is that the CPython interpreter is pretty slow (it trades speed for flexibility) and so you must avoid running in the interpreter whenever ...
Gareth Rees's user avatar
  • 49.6k
13 votes
Accepted

Processing an image to extract green-screen mask

There's a simpler way to create the empty image using numpy.zeros_like: empty_img = numpy.zeros_like(img) As Austin Hastings ...
Dan Mašek's user avatar
12 votes
Accepted

Recursively Save Python Dictionaries to HDF5 Files Using h5py

Here's what I tested. I took out the classmethod stuff to make it easier to read, and simplified names a bit. I'll defer judgement on whether that stuff is needed as part of a larger package or not. ...
hpaulj's user avatar
  • 1,481
12 votes
Accepted

Optimize Performance of Region Checking in List Comprehension

Loops over large arrays are not really a good idea in Python. This is why your original list comprehension is not terribly fast. Your numpy version is loop free, but as far as I know, ...
AlexV's user avatar
  • 7,234
12 votes
Accepted

How to clean the indexes, and ideally not create an additional array

Here is a review of the solution. ^ is xor in Python. It is not for computation of exponentials. When running code outside a method / class, it is a good practice ...
GZ0's user avatar
  • 2,321
11 votes

Generate sample coordinates inside a Polygon

Rejection sampling was proposed in comments on the other answer. The problem with rejection sampling is that the area of a polygon can be an arbitrarily small fraction of its bounding box, for example:...
Gareth Rees's user avatar
  • 49.6k
11 votes
Accepted

Combining two clock tick vectors with NumPy

Everything apart from the for loop in your code is already vectorized which leaves only the for loop to be optimized. You ...
plonser's user avatar
  • 226
11 votes
Accepted

Plot timings for a range of inputs

matplotlib adding stuff to the current figure is because you are not using the OO-interface. It is slightly clunkier, but allows way more freedom In my view, you ...
Maarten Fabré's user avatar
11 votes
Accepted

Table of Tribonacci sequence using NumPy and PANDAS

You’re using the wrong tool for the job. Basically, you do all the computation in Python, use numpy for intermediate storage and ...
301_Moved_Permanently's user avatar
11 votes
Accepted

Solve the phase state between two haplotype blocks using markov transition probabilities

Before we get to the interesting stuff, we should handle some stylistic niggles. Note that PEP 8 is a de-facto style for Python code. First, imports should be sorted ...
Veedrac's user avatar
  • 9,593
11 votes

Remove points in a straight line

You can use Ramer Douglas Peuker algorithm. RDP takes a curve and eliminates points that are close to straight lines. It is distance based. Starting with the two endpoints, it forms a line and picks ...
wooooooooosh's user avatar
10 votes
Accepted

Game of Life with NumPy

That next_state function creates two brand new numpy array. Creating numpy array is slow. Should just update an existing numpy array. Can divide the code into two ...
hamster on wheels's user avatar
10 votes

Modifying Titration Data analysis results

I've never used numpy or matplotlib, so I can only speak to issues of style. You're allowing for far too much nesting here. Your code consists of a giant, dense, deeply nested chunk. As a result, the ...
Carcigenicate's user avatar
10 votes

Iterate through two arrays calculating a statistic from indexes of zeros

for x,y in product(event_index1,event_index2) looks like an efficiency killer. If the lengths of event_index1, event_index2 are <...
vnp's user avatar
  • 55.9k
9 votes
Accepted

Calculating a table of deBroglie wavelengths for various electron energies

First, simplify: factor out \$C^4\$ out of square root: \$\lambda(E_k) = h\left/C\sqrt{(\frac{E_k}{C^2}+m_e)^2-m_e^2}\right.\$ Simplify even more: factor out \$m_e^2\$: \$\lambda(E_k) = h\left/C ...
vnp's user avatar
  • 55.9k
9 votes

Calculating time deltas between rows in a Pandas dataframe

Use the diff(). x['time_delta'] = x.timestamp.diff().fillna(x['time_delta']) This works as below, in a simpler example. You could use the ...
PhasorLaser's user avatar
9 votes
Accepted

Deep Neural Net implementation in Python3

The not so arbitrary Network Your original claim was that your network is "arbitrary". From what I see, I would tend to say that it's not so arbitrary as one might expect. Arbitrary: number of ...
AlexV's user avatar
  • 7,234
9 votes

Generating large testing datasets

There are already tools for choosing elements from a collection with some given probabilities. In the standard library there is random.choices, which takes the ...
Graipher's user avatar
  • 40.9k
9 votes

Looking for the particles that are emitted when a fish is detected in Terraria

However, the entirety of the code is in 1 function and is kind of hard to read. Agreed. And while this is not a major problem for short pieces of code like this, it will turn into a major ...
Mast's user avatar
  • 13.1k

Only top scored, non community-wiki answers of a minimum length are eligible