numpy

Unexpected behaviour of log1p numpy

Unexpected behaviour of log1p numpy Question: I am using the function numpy.log1p to calculate the value of log(1 + x) for very small complex numbers, and I am getting unexpected results. I would expect that outputs should be practically equal to the function input. While this does not seem to be the case in the …

Total answers: 2

np.unique after np.round unrounds the data

np.unique after np.round unrounds the data Question: This code snippet describes a problem I have been having. For some reason rounded_data seems to be rounded, but once passed in np.unique and np.column_stack the result_array seems to be unrounded, meanwhile the rounded_data is still rounded. rounded_data = data_with_target_label.round(decimals=2) unique_values, counts = np.unique(rounded_data, return_counts=True) result_array = np.column_stack((unique_values, …

Total answers: 1

Python library: optionally support numpy types without depending on numpy

Python library: optionally support numpy types without depending on numpy Question: Context We develop a Python library that contains a function expecting a numlike parameter. We specify this in our signature and make use of python type hints: def cool(value: float | int | List[float | int]) Problem & Goal During runtime, we noticed it’s …

Total answers: 1

peak integration using trapz but not starting from baseline and visualization

peak integration using trapz but not starting from baseline and visualization Question: I am trying to integrate signal peaks using numpy.trapz or scipy.trapz. I do manually select the range of a peak min and max. the problem I am facing is that I think these functions integerates the AUC till the baseline. Sometimes my signal …

Total answers: 1

What's the difference between np.divide(x, y) and x/y in Python3?

What's the difference between np.divide(x, y) and x/y in Python3? Question: I recently found a bug in my code that I was able to fix by replacing np.divide(x, y) with x / y. I was under the impression that np.divide(x, y) was equivalent to x / y (it says as much in the numpy documentation). …

Total answers: 1

Avoiding for-loop in NumPy 1D nearest neighbors

Avoiding for-loop in NumPy 1D nearest neighbors Question: I have the following code in which I get the N nearest neighbors in 1D: import numpy as np def find_nnearest(arr, val, N): idxs = [] for v in val: idx = np.abs(arr – v).argsort()[:N] idxs.append(idx) return np.array(idxs) A = np.arange(10, 20) test = find_nnearest(A, A, 3) …

Total answers: 1

How to generate a 3-d matrix of coordindates numpy.meshgrid

How to generate a 3-d matrix of coordindates numpy.meshgrid Question: I need to generate a matrix, say A, and the element of this matrix, say a_{ij}=(x_i,y_j) represents the coordinate of this point. I need the coordinate information to do further calculations. It seems if I could get this matrix, the following calculation should be more …

Total answers: 3

Find points that are equidistant from 4 points

Find points that are equidistant from 4 points Question: This code needs a very long time to find a point. Is there any better way to find a point that is equidistant from 4 given points? A tolerance of 0.5 is fine. import numpy as np # Define the four 3D points as (x, y, …

Total answers: 3

Faster way to pass a numpy array through a protobuf message

Faster way to pass a numpy array through a protobuf message Question: I have a 921000 x 3 numpy array (921k 3D points, one point per row) that I am trying to pack into a protobuf message and I am running into performance issues. I have control over the protocol and can change it as …

Total answers: 1