Get element wise average of multiple arrays [duplicate] - python

This question already has answers here:
Calculate mean across dimension in a 2D array
(3 answers)
Closed 12 months ago.
I have a numpy.ndarray that is made up of other arrays all of the same length. For example:
array_1 = [[3,5,6.2],
[2,4.1,10],
[3,3.5,4]]
How can I get averages element wise. So as to return a single array where each element is the average of the respective elements across all the sub arrays in array_1? So it would return:
averaged_array = [2.66, 4.2, 6.733]

to obtain the average for each list in the matrix:
averaged_array = np.array(array_1).mean(axis=1)
to obtain the average for each column:
averaged_array = np.array(array_1).mean(axis=0)

Related

Find the maximum number of consecutive times value in array is less than threshold [duplicate]

This question already has answers here:
Consecutive events below threshold
(3 answers)
Closed 1 year ago.
I have the following numpy array
array([0.66594665, 0.33003433, NaN, 0.42567293, 0.48161913, 0.30000838, 0.13639367, 0.84300475, 0.19029748, NaN])
I would like to find the number of consecutive times the values in the array are less than 0.5. Is there a way to do this without using a for loop? In this example, the answer is 4 for the following sub-sequence: 0.42567293, 0.48161913, 0.30000838, 0.13639367
import numpy as np
# Create a numpy array
arr = np.array([0.66594665, 0.33003433, np.nan, 0.42567293, 0.48161913, 0.30000838, 0.13639367, 0.84300475, 0.19029748, np.nan])
# Create a numpy array with consecutive values less than 0.5
arr_less_than_0_5 = np.where(arr < 0.5)[0]
# Print the array
print(arr_less_than_0_5)
# Get the number of consecutive times the values in a numpy array are less than 0.5
print(len(arr_less_than_0_5))
The question was to give the number of consecutive times the values are less than 0.5.
It was not asked to print the specific values.
So this answers your question

Getting the top N values and their coordinates from a 2D Numpy array [duplicate]

This question already has answers here:
Efficient way to take the minimum/maximum n values and indices from a matrix using NumPy
(3 answers)
Closed 2 years ago.
I have a 2D numpy array "bigrams" of shape (851, 851) with float values inside. I want to get the top ten values from this array and I want their coordinates.
I know that np.amax(bigrams) can return the single highest value, so that's basically what I want but then for the top ten.
As a numpy-noob, I wrote some code using a loop to get the top values per row and then using np.where() to get the coordinates, but i feel there must be a smarter way to solve this..
You can flatten and use argsort.
idxs = np.argsort(bigrams.ravel())[-10:]
rows, cols = idxs//851, idxs%851
print(bigrams[rows,cols])
An alternative would be to do a partial sorting with argpartition.
partition = np.argpartition(bigrams.ravel(),-10)[-10:]
max_ten = bigrams[partition//851,partition%851]
You will get the top ten values and their coordinates, but they won't be sorted. You can sort this smaller array of ten values later if you want.

How to change value of remainder of a row in a numpy array once a certain condition is met? [duplicate]

This question already has answers here:
Can NumPy take care that an array is (nonstrictly) increasing along one axis?
(2 answers)
Closed 3 years ago.
I have a 2d numpy array of the form:
array = [[0,0,0,1,0], [0,1,0,0,0], [1,0,0,0,0]]
I'd like to go to each of the rows, iterate over the entries until the value 1 is found, then replace every subsequent value in that row to a 1. The output would then look like:
array = [[0,0,0,1,1], [0,1,1,1,1], [1,1,1,1,1]]
My actual data set is very large, so I was wondering if there is a specialized numpy function that does something like this, or if there's an obvious way to do it that I'm missing.
Thanks!
You can use apply.
import numpy as np
array = np.array([[0,0,0,1,0], [0,1,0,0,0], [1,0,0,0,0]])
def myfunc(l):
i = 0
while(l[i]!=1):
i+=1
return([0]*i+[1]*(len(l)-i))
print(np.apply_along_axis(myfunc, 1, array))

Python: Selecting every Nth row of a matrix [duplicate]

This question already has answers here:
Pythonic way to return list of every nth item in a larger list
(9 answers)
Closed 4 years ago.
does anyone know how to select multiple rows of a matrix to form a new one - e.g. I would like to select EVERY 3rd row of a matrix and build a new matrix with these rows.
Many thanks for your help,
Nicolas
An example using numpys ndarray to create a matrix using 10 rows and 3 columns as an example
import numpy as np
matrix = np.ndarray(shape=(10,3))
rows = np.shape(matrix)[0] #number of rows
columns = np.shape(matrix)[1] #number of columns
l = range(rows)[0::3] #indexes of each third element including the first element
new_matrix = np.ndarray(shape=(len(l),columns)) #Your new matrix
for i in range(len(l)):
new_matrix[i] = matrix[l[i]] #adding each third row from matrix to new_matrix

applying function to vector wise to a matrix [duplicate]

This question already has answers here:
numpy subtract every row of matrix by vector
(3 answers)
Closed 5 years ago.
I have a 4 x 2 matrix, i.e. a numpy vector(of length 4) of numpy vectors of length two. For example a = [[1,1],[1,2],[3,5],[8,3]]
I want to subtract the vector b = [3,6] from each row.
I tried to do the following:
np.vectorize(lamda x: x-b)(a)
but i get the error ValueError:
setting an array element with a sequence.
Can somebody explain me why and how to do this the right way?
first convert them to numpy array and then subtract b from a:
a = np.asarray(a)
b = np.asarray(b)
print a - b

Categories