# Tag Info

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,...
• 49.3k
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 ...
• 49.3k
Accepted

• 886
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" ...
• 49.3k

### Finding the closest point to a list of points

All your code could be rewritten as: ...
• 361

### 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 ...
• 49.3k

### Generating a 3D Point Cloud

1. Review No docstring. What does this function do? What parameters does it take? What shape must the depth_image parameter be? What does it return? In Python, ...
• 49.3k
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: ...
• 49.3k
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 ...
• 52.9k
Accepted

### Sending a C++ array to Python/NumPy and back

1. Review It's not recommended to use using namespace std; — the problem is that this imports all of the identifiers from std, ...
• 49.3k
Accepted

### Approximate (250 over 100) permutation best fitting certain criteria

1. Analysis In the general case, the problem of finding the sample of words with the "most even" distribution of letters is NP-hard. Here I'm considering a general instance of this problem to be: ...
• 49.3k
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 ...
• 6,532
Accepted

### K-Mean with Numpy

1. Code review There's no documentation. What do your functions do and how am I supposed to call them? There are no test cases. What gives you confidence that the algorithm is correct? The algorithm ...
• 49.3k
Accepted

### Balanced Random Assignment in Python

Just create a list of 18 ones and 18 twos, shuffle it, then reshape to 6x6: ...
• 401
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 ...
• 49.3k

### Processing an image to extract green-screen mask

Use conditional indexing: ...
• 12.2k
Accepted

### Groupby in NumPy

The function scipy.ndimage.find_objects returns exactly the slices that you are looking for: ...
• 49.3k
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 ...
• 293
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, ...
• 7,184
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 ...
• 2,291
Accepted

### Order a list of tuples or a NumPy array in a specific format

This is really a graph problem! Draw a graph with a vertex for every number in your list, and an edge between vertices $a$ and $b$ if there is a pair $(a, b)$ or $(b, a)$ in your list: What ...
• 49.3k
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 ...
• 226
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. ...
• 1,461
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 ...
• 8,970
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 ...
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 ...
• 9,553

### 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 ...
• 543