score:3
Accepted answer
Just check the case:
if ROI.any():
mean = I[ROI > 0].mean()
else:
mean = 0
Or in an one liner using ternary:
mean = I[ROI > 0].mean() if ROI.any() else 0
score:0
To remove warning use:
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
mymean = np.mean([i for i, j in zip(I, ROI) if j])
print(mymean)
Credit To: stackoverflow.com
Related Query
- How to compute the mean of a 2D array with ROI?
- How to calculate the mean index in array with NumPy
- How to create the histogram of an array with masked values, in Numpy?
- how to get the neighboring elements in a numpy array with taking boundaries into account?
- How can I efficently pad an RGB numpy array with the median of the image?
- How to group elements of a numpy array with the same value in separate numpy arrays
- Given a 2D Numpy array representing a 2D distribution, how to sample data from this distribution with the aid of Numpy or Scipy functions?
- Numpy: Replace every value in the array with the mean of its adjacent elements
- How to read NetCDF variable float data into a Numpy array with the same precision and scale as the original NetCDF float values?
- How to convert a pandas dataframe into a numpy array with the column names
- How to replace masked values from a numpy array with the average of the values immediately next to the masked value
- How to efficiently send a large numpy array to the cluster with Dask.array
- How to compare 3 numpy arrays elementwise and get the results as the array with maximum values?
- How to combine the data from many data frames into a single data frame with an array as the data values
- how to sort a scipy array with order attribute when it does not have the field names?
- How do we replace multiple rows of data with the mean of those rows for every change in the other columns?
- How do I quickly create a numpy or pandas 2D array with both axis in a range and the values a product?
- How to populate the spacings between elements of an array with constant step, and combine two such arrays with primary and secondary priorities?
- How to compute the kind of distance matrix with vectorization
- How do I element-wise multiply an array of 6 elements with an array of 12 elements with the smaller array repeating?
- How to compare two numpy arrays of strings with the "in" operator to get a boolean array using array broadcasting?
- How to compute the maximum between an array and a number element by element?
- In Python and numpy, How do I replace missing values in an array with the previous element? (Masked array?)
- How to efficiently find the indices a first array values matching with a second array values?
- how to compute the mean of values from different lists
- How to get values in an array using another array with the indexes of the values?
- What is the difference between an array with shape (N,1) and one with shape (N)? And how to convert between the two?
- How to pad a 2d array with variable length per row on the left and right to form a bigger 2d array
- How to get a mean array with numpy
- How to subtract a NumPy array from another one with a condition on the elements of the first one
More Query from same tag
- TypeError: unhashable type on new computer but not old
- How to efficiently create a sparse vector in python?
- What is the cleanest way of finding the indices of all entries in a numpy array that matches a pattern?
- raise RuntimeError(f'Cannot parse version {FULLVERSION}') in install MKL with numpy from WSL
- Make a numpy upper triangular matrix padded with Nan instead of zero
- get a dimensional array from n lists
- ndimage's center_of_mass to calculate the position of a Gaussian peak
- Stacking/concatenated arrays and "None"?
- Covariance matrix for 9 arrays using np.cov
- How to calculate multi-variable nonlinear regression in python?
- python 3.5 numpy+mkl installation error
- Understanding NumPy's einsum
- How to improve computational efficiency of correlation analysis using scipy on big dataframe
- Fast Python plotting library to draw plots directly on 2D numpy array image buffer?
- Can I run a external program with a python loop?