0
In a matrix, or 2-d array X, the averages (or means) of the elements of rows is called row means. Task Given a 2D array, return
what is a correct code
2 ответов
+ 7
Ahmed Allawy ,
assuming this the array (we are using a list):
[
[1,2,3],
[4,5,6],
[7,8,9]
]
to calculate the means of the 3 rows, we need to iterate on the input list. when using a for loop, we get a complete row in the loop variable for each iteration step.
use each of the rows to build the sum and divide it by the length of the row.
as result we finally have 3 numbers, wheras each represents the mean of the respective line
+ 1
The easiest way is like this:
if
x = [[1, 2, 3],
[4, 5, 6, 7, 8]]
row_means for the first row is (1+2+3)/3=2
row_means for the second row is (4+5+6+7+8)/5=6
print([float(sum(x[0]))/len(x[0])]) #first row_means as []
print(sum(x[1])/len(x[1])) # second row_means