+ 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)

11th Dec 2021, 12:34 AM
Marleen Thaler
Marleen Thaler - avatar
6 Réponses
+ 1
For a start, it would be nice to know the assignment.
11th Dec 2021, 1:25 AM
Solo
Solo - avatar
+ 1
You forgot to round the output to the second decimal place, as required by the task.
17th Dec 2021, 11:58 PM
Solo
Solo - avatar
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]
17th Dec 2021, 5:11 PM
Marleen Thaler
Marleen Thaler - avatar
0
You’re right, thanks :)
19th Dec 2021, 2:25 AM
Marleen Thaler
Marleen Thaler - avatar
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
17th Apr 2022, 10:03 AM
Shubham Mandgaonkar
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))
26th Jul 2022, 9:16 AM
Mohammad Shoush
Mohammad Shoush - avatar