Merging a 2D array into a list

Question:

I’m working on a linear regression with Python for my school project. And I want to merge my 7×12 2D array into a 1D list.
Here’s my original array.

y = df["fatalProb"].values.reshape(7,12)
print(y)
[[0.3725 0.4336 0.537  0.392  0.233  0.2892 0.2721 0.2392 0.2281 0.2689
  0.2898 0.2825]
 [0.3112 0.3936 0.3874 0.2793 0.2416 0.275  0.2802 0.2587 0.2583 0.258
  0.2906 0.2927]
 [0.3486 0.3278 0.3836 0.3041 0.2477 0.2734 0.276  0.2903 0.2531 0.2659
  0.2928 0.2896]
 [0.3044 0.4032 0.3665 0.3275 0.2939 0.2882 0.3089 0.2949 0.2547 0.2699
  0.2973 0.2869]
 [0.3488 0.3651 0.4307 0.3361 0.2833 0.3035 0.3051 0.2898 0.2695 0.271
  0.2787 0.3034]
 [0.3559 0.3357 0.4075 0.3428 0.2834 0.3156 0.2952 0.2992 0.2795 0.2806
  0.2905 0.267 ]
 [0.3965 0.3814 0.4735 0.3813 0.3089 0.3105 0.3282 0.3047 0.2834 0.2974
  0.2737 0.2986]]

I wanted my y to be 12-length list.
It’s a list with all the values added that has a same index in each lists.

e.g.:

[[a,b,c], [d,e,f], [g,h,i]] to [a+d+g, b+e+h, c+f+i]

I thought about using list comprehension, but I was not so happy to use:

y = [y[0][j] + y[1][j] + y[2][j] + y[3][j] + y[4][j] + y[5][j] + y[6][j] for j in range(len(y[0]))]
Asked By: MorningSnow

||

Answers:

Check below code based on your e.g "[[a,b,c],[d,e,f],[g,h,i]] to [a+d+g,b+e+h,c+f+i]"

df = pd.DataFrame({'col':[1,2,3], 'col2':[3,4,5], 'col3':[6,7,8]})
list(df.values.sum(axis=1))

Output:

[10, 13, 16]
Answered By: Abhishek

Here’s your list comprehension:

[sum(el) for el in zip(*y)]

Result:

[2.4379, 2.6404, 2.9861999999999997, 2.3631, 1.8918, 2.0553999999999997, 2.0656999999999996, 1.9768, 1.8266, 1.9116999999999997, 2.0134, 2.0206999999999997]
Answered By: ansa

Your y, derived from a dataframe, is 2d; a simpler example:

In [25]: y = np.arange(12).reshape(3,4)    
In [26]: y
Out[26]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

With the sum method, you can add values vertically or horizontally:

In [27]: y.sum(axis=0)
Out[27]: array([12, 15, 18, 21])

In [28]: y.sum(axis=1)
Out[28]: array([ 6, 22, 38]
Answered By: hpaulj

All you need to do after getting y is

y = y.sum(axis=0).tolist()

It will sum your (7,12) shaped matrix along the columns and give you a array of shape (1,12) which can be converted to a list. @hpaulj gave a detailed explaination.

Answered By: MSS
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.