+ 1

Trying to understand the problem in DS for Python - Data Manipulation

I need help to see if I understand the problem correctly. The project problem is stated as follows: 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 The given first line of code is as follows: n, p = [int(x) for x in input().split()] Now this is how I understand this problem and given code line - please tell me where I got it wrong: The given line of code is there so one could input any two integers, n and p, that determine the shape of an array. A new line of code is needed to allow input of data into a new list. For example: v = [float(y) for y in input().split()] The next step should be to construct a numpy array with shape (n, p) The next step should be to reshape v to have the shape (n,p) Then it becomes simply a matter of applying arr.mean(axis = 1) to get the desired output. Now I know there is something wrong with the above understanding of the problem because SL is treating my second line v = [float(y) for y in input().split()] to create a one-dimensional array with two values (obviously). If I include a third line of code, say vtwo = [float(z) for z in input().split()] it will create a second array and I could then concatenate the two arrays v and vtwo before reshaping, but the problem with this approach is that it only works if n is 2. Which I could solve with some sort of loop but I am suspecting that my understanding of what n and p are is incorrect. Could you please clarify?? I've been stuck for 3 days now :(

8th Mar 2021, 6:39 AM
Nassim Abed
Nassim Abed - avatar
4 Answers
+ 2
So you handed me the solution and did not answer my question, thereby denying me the chance to solve it once I understand the question. The answer to my question, by deduction from the solution you offered, is that (n, p) is not the shape of the array. The relation between p and the number of input is nowhere to be found in the code itself and is taken care of by the poorly explained background of the SL question. This is not conducive to learning. I should find other ways.
8th Mar 2021, 1:16 PM
Nassim Abed
Nassim Abed - avatar
+ 1
import numpy as np n, p = [int(x) for x in input().split()] list = [] for i in range(n): list.append(input().split()) arr = np.array(list).astype(np.float16).mean(axis=1).round(2) print(arr) try for instead of while Also it should be float and round to 2
8th Mar 2021, 8:03 AM
Sharique Khan
Sharique Khan - avatar
0
You didn't read the post, did you? Here's the incorrect code. Please keep in mind I am more interested in properly understanding the problem than in someone handing me the correct code. I can figure out the correct code if I understand the logic of the question. I am able to code this separately outside SL but the trick here is understanding what the SL problem author had in mind, which I can't figure out. Anyway here's the incorrect code: n, p = [int(x) for x in input().split()] valuelist = [] c = 1 while c <= n*p: v = [float(y) for y in input().split()] valuelist.append(v) c +=1 import numpy as np arr = np.array(valuelist) arrshp = arr.reshape(n, p) answerarr = arrshap.mean(axis = 1) print(answerarr)
8th Mar 2021, 7:53 AM
Nassim Abed
Nassim Abed - avatar
0
you need to paste this in code playground and share the link. People cannot edit your question or answer
8th Mar 2021, 7:55 AM
Sharique Khan
Sharique Khan - avatar