Data Science - Average of Rows [Code Coach] | Sololearn: Learn to code for FREE!

+10

# 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

6/16/2020 7:10:54 PM

Yurii Ostapenko

+79

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

+72

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.

+9

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)

+7

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.

+4

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

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

+3

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

+2

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

+2

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

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

+1

import numpy as np rm = [] for i in range(n): for a in input().split(): rm.append(float(a)) a = np.array(rm).reshape((n,p)) c = np.mean(a,axis=1) p = np.around(c,2) print(p)

+1

BTW, they said nothing about astype() before.

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

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.

0

How to make it work without panda. I'm perplexed

0

` n, p = [int(x) for x in input().split()] import numpy as np print(np.array([[float(x) for x in input().split()] for i in range(n)]).mean(axis=1).round(2)) ` Credit to @Pablo Puente for `.round(2)`

0

Data Science - the perfect solution for average of rows n, p = [int(x) for x in input().split()] import numpy as np rm = [] for i in range(n): for a in input().split(): rm.append(float(a)) a = np.array(rm).reshape((n,p)) c = np.mean(a,axis=1) p = np.around(c,2) print(p)

0

I almost didn't use Data Science, my solution: n, p = [int(x) for x in input().split()] lista=[] for x in range(n): suma=0 row = [float(x) for x in input().split()] for y in row: suma+=y lista.append(suma/p) import numpy as np print(np.array(lista).round(2))

0

Ordinary least squares for linear regression. Ordinary least squares (OLS) is a method to estimate the parameters ß in a simple linear regression, XB = y, where X is the feature matrix and y is the dependent variable (or target). by minimizing the sum of the squares of the differences between the observed dependent variable in the given dataset and those predicted by the linear function. Mathematically, the solution is given by the formula in the image, where the superscript T means the transpose of a matrix, and the superscript -I means it is an inverse of a matrix. Task Given a 2D array feature matrix X and a vector y, return the coefficient vector; see the formula. Input Format 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 Output Format An numpy Id decimal. array of values rounded to the second Sample Input 22 0 02 23 Sample Output (2. , 1.5) Can anyone have completed Please help

0

This is my solution! I have a fix when your column input has the error. import numpy as np n, p = [int(x) for x in input().split()] matrix = [] for i in range(n): row = [x for x in input().split()] matrix.append(row) if len(row) != p: # it's doesn't still working raise ErrorIndex # it's doesn't still working matrix_arr = np.array(matrix, dtype=float) mean_matrix = np.mean((matrix_arr), axis=1).round(2) print(mean_matrix)