score:0
Accepted answer
Three options:
- Something similar to list comprehension using NumPy's
.item
. itertools.starmap
np.broadcast
Timing is below. Option 3 seems to be by far the fastest route.
from itertools import starmap
np.random.seed(123)
target = np.random.randn(2, 2)
roundto = np.arange(1, 5, dtype=np.int16).reshape((2, 2)) # must be type int
def method1():
return (np.array([round(target.item(i), roundto.item(j))
for i, j in zip(range(target.size),
range(roundto.size))])
.reshape(target.shape))
def method2():
return np.array(list(starmap(round, zip(target.flatten(),
roundto.flatten())))).reshape(target.shape)
def method3():
b = np.broadcast(target, roundto)
out = np.empty(b.shape)
out.flat = [round(u,v) for (u,v) in b]
return out
from timeit import timeit
timeit(method1, number=100)
Out[50]: 0.003252145578553467
timeit(method2, number=100)
Out[51]: 0.002063405777064986
timeit(method3, number=100)
Out[52]: 0.0009481473990007316
print('method 3 is %0.2f x faster than method 2' %
(timeit(method2, number=100) / timeit(method3, number=100)))
method 3 is 2.91 x faster than method 2
Credit To: stackoverflow.com
Related Query
- Rounding elements in numpy array to the number of the same index in another array
- Replacing value in a numpy array using as an index another value from the same array
- padding one numpy array to achieve the same number os columns of another numpy array
- How to assign all non-zero elements in each numpy column to a value in an array whose size is the same as the number of columns?
- How to bind elements of the same index in numpy array in python
- how to index into a numpy array using another array of the same size
- How to count the number of true elements in a NumPy bool array
- numpy array concatenate: "ValueError: all the input arrays must have same number of dimensions"
- Indexes of elements in NumPy array that satisfy conditions on the value and the index
- Find maximum of each row in a numpy array and the corresponding element in another array of the same size
- How to group elements of a numpy array with the same value in separate numpy arrays
- Select elements from a numpy array based on values in another array that is not an index array
- Why the elements of numpy array not same as themselves?
- Flood fill NumPy Array `numpy.ndarray`, i. e. assign new value to element and change neighboring elements of the same value, too
- Filling zeros in numpy array that are between non-zero elements with the same value
- Fastest method for determining if 2 (vertically or horizontally) adjacent elements of a numpy array have the same value
- repeat elements of an array according to other array with the same length? like repelem for numpy
- How to optimize a numpy loop that sums values from an array which is indexed by another array where values equal the loop index
- How can I replace recurring values in a numpy array by the index of the unique value from another array?
- check two arrays for matching elements given the same index and return number of matches
- Using elements of numpy array as index of another numpy array
- How to subtract a NumPy array from another one with a condition on the elements of the first one
- Append to the numpy array another numpy array as array, not it's elements
- Finding the index of elements in an array/list based on another list or array
- How does one index a numpy array using another numpy array that holds the indices?
- Numpy 1D array - find indices of boundaries of subsequences of the same number
- Vectorized Update numpy array using another numpy array elements as index
- I need to find the location of a value in one numpy array and use it to refer to a value in the same location of another numpy array
- How to pair matrices that are approximately the same in another numpy array
- Repeating numpy array elements according to another index array
More Query from same tag
- Numpy: check if 1-d array is sub-array of another
- Store calculated filter banks into spectrogram image with Python
- how to index into a numpy array using another array of the same size
- probability density function from histogram in python to fit another histrogram
- "Not Enough Image Data" Error when Converting CV2 Numpy Array into RGB Image
- Convert Y-m-d H:M:S format to int in python
- How to pad a 2d array with variable length per row on the left and right to form a bigger 2d array
- Representing a list of strings as a numpy array of their ascii codes
- Speed up turn probabilities into binary features
- Scalar comparison with np.nan in python
- Passing multiple arguments to Python script using sys.argv[1] and numpy array. IndexError: list index out of range
- Selecting the last element of a list inside a pandas dataframe
- Combine matrices - numpy
- Numpy matrix inversion gives wrong results for order greater than 18
- Elegant way to mask out intervals between events containing nan in numpy/pandas