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,...
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 ...
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 ...
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" ...
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:
...
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
...
14
votes
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 ...
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, ...
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 ...
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:...
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 ...
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
...
11
votes
Accepted
Alternatives to iterrow loops in python pandas dataframes
I’d assume the root cause is my loops in the matrix parts.
Yes, looping is an anti-pattern in pandas, so iterrows should almost always be avoided.
On top of that, ...
10
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 ...
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 ...
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 <...
10
votes
Accepted
Text-mode 2048 in Python (using numpy)
Yay! Modules have docstrings, excellent. And same for classes.
Recommend that you routinely include .idea/ in .gitignore,
so ...
10
votes
Function that returns activation function, as well as its derivative
Accepting a string fn_name is poorly typed ("stringly typed"). From least to most preferable solutions:
Type-hint the function parameter as a ...
9
votes
Pretty printing of the numpy ndarrays
If A.ndim is not in 1, 2, 3, your code tries to return a non-existing string s. It would be ...
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 ...
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 ...
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 ...
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♦
- 13.9k
9
votes
Accepted
Function that returns activation function, as well as its derivative
I like it, this code is beautiful, and it comes with a nice
doctest.
Ship it as-is.
Of course, there's always more to say about any given piece of code.
(Oh, my! ...
9
votes
Accepted
Correctly sum pixel values into bins of angle relative to center
Numerics
np.sqrt(y**2 + x**2) should be replaced with a call to np.linalg.norm on your yx.
...
8
votes
Accepted
Looping over pixels in an image
I think the trick is trying to vectorise this as much as possible:
By the look of it, the code is trying to threshold at 0 and count pixels under 255.
We can change the first part of the loop to:
<...
8
votes
Slice a list or NumPy array into consecutive tuples
I believe that what you are looking for is already available as an itertools recipe; even though pairwise only allow you to ...
8
votes
Accepted
Create a probability list which sums up to one
Binary floating point numbers cannot represent most numbers exactly,
a small error cannot be avoided when computing the sum of probabilities:
...
8
votes
Creating an affinity-matrix between protein and RNA sequences
Boosting performance
The initial approach creates a 2-dimensional list of empty lists beforehand and performs 2 538 096 expensive list.append operations.
To make ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
numpy × 765python × 755
performance × 243
python-3.x × 177
pandas × 85
matplotlib × 53
array × 46
matrix × 45
algorithm × 39
scipy × 39
statistics × 37
image × 33
python-2.x × 32
vectorization × 32
simulation × 28
beginner × 27
machine-learning × 24
numerical-methods × 23
cython × 23
computational-geometry × 21
mathematics × 20
clustering × 19
opencv × 17
time-limit-exceeded × 16
physics × 16