+ 13

# Data Science - Average of Rows [Code Coach]

Data Science - Average of Rows 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] _______________ My code: import numpy as np import pandas as pd n, p = [int(x) for x in input().split()] lst = [] for i in range(n): lst.append(input().split()) x = pd.DataFrame(lst).astype(float) y = x[:].sum(axis=1) / 2 y = np.array(y) print(y) the code successfully passes the first 3 test cases, but does not pass 4 and 5. The input data for 4 and 5 test cases is hidden. please help me understand what could be the matter

16th Jun 2020, 7:10 PM
Yurii Ostapenko + 117
My Solution with no Pandas: import numpy as np n, p = [int(x) for x in input().split()] lista = [] for i in range(n): lista.append(input().split()) print(np.array(lista).astype(np.float16).mean(axis=1).round(2)) Please vote :D
3rd Jan 2021, 8:00 PM
Pablo Puente + 107
import numpy as np n, p = [int(x) for x in input().split()] #it taking first two input of n and p x= [] # array for the list for i in range(n): # taking input for each row x.append(input().split()) # taking input and spliting the data into columnwise input arr = np.array(x) # making this numpy array arr = arr.astype(np.float16)#making data type into float mn = arr.mean(axis = 1) # having mean value in rowwise with axis = 1 mean= mn.round(2) #as output need to be two value after point value print(mean) I tried to explain the code as well. Please vote me to encourage me more.
19th Jan 2021, 1:19 PM
1604016_Meherun + 12
Here my solution: import numpy as np n, p = [int(x) for x in input().split()] m = [] for i in range(n): m.append(input().split()) m_array = np.array([m], dtype= 'float') rm_array = m_array.reshape(n, p) resul = [] y = 0 while y < n: resul.append(rm_array [y, :].mean().round(2)) y += 1 resul_array = np.array(resul) print(resul_array)
4th Jan 2021, 12:43 AM
Rodrigo Silva + 9
You are calculating the mean for 2-columns arrays but the number of columns varies. [...and the second indicates the columns of X (p)...] You forgot to round: [Output format: A numpy 1d array of values ROUNDED TO THE SECOND DECIMAL] Hope this helps. Don't doubt to keep inquiring.
16th Jun 2020, 8:28 PM
Kevin ★
+ 8
import numpy as np row, col = [int(x) for x in input().split()] list = [float(j) for i in range(row) for j in input().split()] array = np.array(list).reshape((row, col)) print(array.mean(axis=1).round(2)) # ------------------------------------------------------------- ## for loop solution (more readable) import numpy as np row, col = [int(x) for x in input().split()] list = [] for i in range(row): list += [float(j) for j in input().split()] array = np.array(list).reshape((row, col)) print(array.mean(axis=1).round(2))
6th May 2021, 1:26 PM
Mahdi Kyan Bahrami
+ 6
Does anybody else get the same problem? In test 2, the code result shows two spaces between the first 2 elements, in the expected result it's just 1. It looks like they edited it and no code works now. How to report that
1st Aug 2022, 7:01 AM
+ 4
Hi, here's my solution. Thanks to the community since i used some of your answers to build mine. I found very confusing to have to call "input" again. As the problem is written, i thought that the whole bunch of numbers were given in one shot in the first input. Got stuck for while! n, p = [int(x) for x in input().split()] import numpy as np data = [] for i in range(n): list = [float(x) for x in input().split()] data.append(list) X = np.array(data) means = X.mean(axis=1).round(2) print(means) Thanks!
8th Jun 2021, 12:41 PM
Vincent Chenet
+ 4
I'm upset that this example is 100% unrelated to everything that has been taught in this course so far. Yes it uses Python but not the concepts that were taught in this course. I thought it was just me but none of these solutions use concepts that were taught here.
7th Sep 2022, 10:31 PM
Muslimah248 + 3
"""i used numpy empty method to create an empty array with 0 rows and p columns. looping through n rows, i used numpy append method to insert new inputs and astype method to convert to float along the 0 axis. then used numpy mean method along axis 1 and rounded to 2 digits. """ n, p = [int(x) for x in input().split()] import numpy as np a = np.empty((0,p), float) for i in range(n): a = np.append(a, np.array([input().split()]).astype(float), axis=0) print(a.mean(axis=1).round(2))
10th Jan 2021, 9:09 AM
you are smart. you are brave. + 3
Here was my solution for the problem. This failed the last two test cases. Can anyone explain me why? Thank you. import numpy as np contents = [] while True: try: contents.append(input().strip().split()) except EOFError: break print(np.array(contents[1:]).astype(np.float).mean(axis=1))
14th Apr 2021, 11:14 AM
Tharinda Nimnajith + 3
BTW, they said nothing about astype() before.
19th Jun 2021, 12:32 PM
Sharofiddin + 2
import numpy as np n, p = [int(x) for x in input().split()] x = [] for i in range(n): x += [float(k) for k in input().split()] x = np.array(x).reshape((n, p)) print(np.around(np.mean(x, axis=1),decimals=2))
26th Jan 2022, 11:45 PM
Hamza Yousfi + 1
n, p = [int(x) for x in input().split()] X = [] for i in range(n): X.append([float(x) for x in input().split()]) y = [float(x) for x in input().split()] try this code.... Credit to Amir Mehrabi Jorshari import numpy as np X=np.array(X).reshape(n,p) y=np.array(y) b=np.linalg.pinv(X) @ y.transpose() print(np.around(b,decimals=2))
28th Apr 2021, 4:16 AM
Mardin D. Maloloy-on + 1
My code. Work perfectly import numpy as np row = [] n, p = [int(x) for x in input().split()] while True: try: row.append(input().split()) except EOFError: break row_arr = np.array(row).astype(float) rowMean = row_arr.mean(axis=1) print(rowMean.round(2))
20th Jan 2022, 10:57 AM
Baroka
+ 1
import numpy as np n, p = [int(x) for x in input().split()] arr=[] for i in range (n): arr+= input().split() arr = np.array(arr) arr = arr.astype(np.float16) arr =np.reshape(arr,(n, p)) print (arr.mean(axis=1).round(2)) ## Try not to follow me
10th Aug 2022, 10:34 AM
Abdul Bari Rahmani + 1
import numpy as np n, p = [int(x) for x in input().split()] rowmean = np.zeros(n) for i in range(n): array = [float(j) for j in input().split()] rowmean[i] = np.round(np.mean(array),2) print(rowmean)
26th Sep 2022, 4:09 PM
Anthony Martin + 1
import numpy as np n, p = [int(x) for x in input().split()] data = [] for i in range(0,n): liste = [float(x) for x in input().split()] data.append(liste[0:p]) arr = np.array(data) print(arr.mean(axis=1).round(2))
21st Oct 2022, 7:43 PM
Lionel Mallet + 1
n, p = [int(x) for x in input().split()] import numpy as np new_arr = [] for i in range(n): arr = input().split() lis = [float(j) for j in arr] new_arr.append(lis) arr = np.array(new_arr) print(arr.mean(axis=1).round(2))
13th Jan 2023, 6:51 PM
Theresa Sunday 0
I updated mine according to Kevin's answer and tried this: row_mean.append(round((sum(row)/p), 2)) ...but failed at Case 5. Then got rid of round() and used: np.around(..., 2) ...passed.
23rd Dec 2020, 9:32 AM
Mert 0
How to make it work without panda. I'm perplexed
26th Dec 2020, 8:07 AM
Amit Kumar 