+ 3
Data Science Average of Rows questions
Regarding the Code challenge in the Data Science course chapter 1 (Data Manipulation) "Average of rows": My code seems to be wrong for the last two hidden test cases, but I can't find the error. Can anyone help me and give me a hint? Here's the code: import numpy as np n, p = [int(x) for x in input().split()] means = [] for i in range(n): list = [float(x) for x in input().split()] means.append(sum(list)/len(list)) arr = np.array(means) np.around(arr, decimals=2) print(arr)
6 Respuestas
+ 1
For a start, it would be nice to know the assignment.
+ 1
You forgot to round the output to the second decimal place, as required by the task.
0
Right, I copied the assignment here: 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 the rowmeans.
Input Format
First line: two integers separated by spaces, the first indicates the rows of matrix X (n) and the second indicates the columns of X (p)
Next n lines: values of the row in X
Output Format
An numpy 1d array of values rounded to the second decimal.
2 2
1.5 1
2 2.9
Sample Output
[1.25 2.45]
0
You’re right, thanks :)
0
import numpy as np
n, p = [int(x) for x in input().split()]
# Initialize matrix
matrix = []
#Entry row elements separated by space
for i in range(n):
matrix.append(input().split())
print(np.array(matrix).astype(np.float16).mean(axis=1).round(2))
#This will work
0
import numpy as np
n, p = [int(x) for x in input().split()]
my_list = []
for i in range(n):
my_list.append(list(float(x) for x in (input().split())))
my_list_ar = np.array(my_list)
my_list_ar.reshape(n, p)
print(np.around((my_list_ar.mean(axis=1)), decimals=2))