0

NUMPY: 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] ''' n, p = [int(x) for x in input().split()] list = [] new_list = [] for i in range (n): list.append(input().split()) import numpy as np arr = np.array(list) mean = arr.mean(axis = 1) print (mean) #This code generates error as input data is a string. Explain ways to convert all strings into int/float before appending it to list or to change the data type of all the items in an array.

3rd Jan 2021, 12:31 PM
CHANDAN ROY
CHANDAN ROY - avatar
15 Respostas
+ 9
n, p = [int(x) for x in input().split()] list = [] for i in range(n): list.append([float(j) for j in input().split()]) import numpy as np arr = np.array(list, dtype='f') mean = arr.mean(axis=1) print(mean.round(2))
9th Apr 2021, 7:16 AM
Ruturaj Damoshan
Ruturaj Damoshan - avatar
+ 5
noteve Your solution is wonderful. ā˜ŗļø However, you just need to add the round method to the 'mean' variable assignment or to the print line to pass the spec criteria. Eg: mean = arr.mean(axis=1).round(2)
3rd Jul 2022, 9:55 AM
Matt šŸŒ
Matt šŸŒ - avatar
+ 4
# Try this: n, p = [int(x) for x in input().split()] list = [] for i in range (n): list.append([float(j) for j in input().split()]) import numpy as np arr = np.array(list) mean = arr.mean(axis = 1) print (mean) # Update me if it still not solved. Thanks!!
3rd Jan 2021, 2:00 PM
noteve
noteve - avatar
+ 3
Working 5/5: n, p = [int(x) for x in input().split()] l=list() for i in range(n): l.append([float(j) for j in input().split()]) import numpy as np arr=np.array(l) arrz=arr.reshape(n,p) mn=np.mean(arrz,axis=1) print(np.round(mn,decimals=2)) Any queries feel free to dm
10th Aug 2021, 8:28 AM
HARSHITH JK
HARSHITH JK - avatar
+ 3
#Solution - import numpy as np rows, columns = [int(x) for x in input().split()] matrix = [] for row in range(rows): matrix.append(input().split()) matrix = list(np.float_(matrix)) #print(matrix) average = np.average(matrix, axis=1) print(average.round(2))
6th Oct 2021, 9:50 AM
Abhishek Tiwari
Abhishek Tiwari - avatar
+ 1
@AJ I tried all solutions in sololearn and it always fails me in testcases any help would be appreciated
3rd Aug 2022, 12:53 PM
Mohamed Salem
Mohamed Salem - avatar
+ 1
The major challenge here is to understand what you are doing, and this test makes this terrible. So, my suggestion is to solve this by the following approach: 1. Make the code understandable by a human (test it in your playground), so you can understand how the tricky input works, but the human messages in the input() calls will break when you copy the code to the test; 2. With your code running ok, go to the test, remove the human messages, and submit the code. FIRST PART - Make the code understandable by a human (test it in your playground): # This code will work in your playground but will break in the test. import numpy as np rows, columns = [int(x) for x in input('Enter 2 int dimensions n and p (separated by spaces): ').split()] matrix = [] for row in range(rows): matrix.append(input(f'Enter {columns} values for {row+1} (separated by spaces): ').split()) matrix = list(np.float_(matrix)) average = np.average(matrix, axis=1) print(average.round(2)) SECOND PART - With your code running ok, go to the test, remove the human messages, and submit the code. # This code will work in will NOT break in the test. import numpy as np rows, columns = [int(x) for x in input().split()] matrix = [] for row in range(rows): matrix.append(input().split()) matrix = list(np.float_(matrix)) average = np.average(matrix, axis=1) print(average.round(2)) Submit and celebrate.
21st Nov 2022, 1:29 PM
Pedro Carneiro Jr
Pedro Carneiro Jr - avatar
+ 1
Here goes the path for the solution in a manner that will help you understand it step-by-step. Errata: in my last answer here, when I mentioned "the playground", I wanted to say "your PC". So here we go... The major challenge in this test is to first understand what you are doing by running the code in your PC/notebook, because the neither the test interface nor the Playground interface will help with it. So, my suggestion is to solve this by the following approach: 1. Make the code understandable by a human (test it in YOUR PC OR NOTEBOOK), so you can understand how the tricky input works, but the human messages in the input() calls will break when you copy the code to the test or will not be understandable in the playground; 2. With your code running ok IN YOUR COMPUTER, go to the test, remove the human messages, and submit the code. FIRST PART - Make the code understandable by a human (test it in YOUR COMPUTER): # This code will work in YOUR COMPUTER but will break in the test. import numpy as np rows, columns = [int(x) for x in input('Enter 2 int dimensions n and p (separated by spaces): ').split()] matrix = [] for row in range(rows): matrix.append(input(f'Enter {columns} values for {row+1} (separated by spaces): ').split()) matrix = list(np.float_(matrix)) average = np.average(matrix, axis=1) print(average.round(2)) SECOND PART - With your code running ok, go to the test, remove the human messages, and submit the code. # This code will work in will NOT break in the test. import numpy as np rows, columns = [int(x) for x in input().split()] matrix = [] for row in range(rows): matrix.append(input().split()) matrix = list(np.float_(matrix)) average = np.average(matrix, axis=1) print(average.round(2)) Submit and celebrate.
21st Nov 2022, 1:39 PM
Pedro Carneiro Jr
Pedro Carneiro Jr - avatar
0
CHANDAN ROY Sent solution in DM.
3rd Jan 2021, 1:53 PM
AĶ¢J
AĶ¢J - avatar
0
Task Given a 2D array feature matrix X and a vector y, return the coefficient vector; see the formula. First line: two integers separated by spaces, the first indicates the rows of the feature matrix X (n) and the second indicates the columns of X (p) Next n lines: values of the row in the feature matrix Last line: p values of target y MY CODE import numpy as np rows, columns = [int(x) for x in input().split()] matrix = [] for row in range(rows): matrix.append(input().split()) matrix = list(np.float_(matrix)) #print(matrix) average = np.average(matrix, axis=1) print(average.round(2)) Error: Not satisfied any Test case
8th Oct 2021, 1:40 AM
Fahim UD Din
Fahim UD Din - avatar
0
import numpy as np arr = np.array([input().split() for x in range(n)]).astype(float) mean = arr.mean(axis=1).round(2) print(mean)
18th Nov 2021, 11:04 PM
Mateo GonzƔlez Bufi
Mateo GonzƔlez Bufi - avatar
0
import numpy as np arr=np.array([[1.5,1],[2,2.9]]) print(arr.mean(axis=1))
12th Jan 2022, 12:47 PM
Neha Surana
0
import numpy as np import re n, p = [int(x) for x in input().split()] lista = [] for i in range(n): lista.append(input().split()) sol = str(np.array(lista).astype(np.float16).mean(axis=1).round(2)) removeSpaces = re.sub("\s\s+", ' ', sol) print(removeSpaces)
3rd Aug 2022, 1:49 PM
Rajesh Das
Rajesh Das - avatar
0
Rajesh Das unfortunately your solution failed test case 3&4
3rd Aug 2022, 2:23 PM
Mohamed Salem
Mohamed Salem - avatar
- 1
I Am AJ ! Rafique Sir, help!!
3rd Jan 2021, 1:10 PM
CHANDAN ROY
CHANDAN ROY - avatar