简体   繁体   中英

NumPy - formatting two arrays to one multi-dimensional array

I have the following values:

grade_list = [[99 73 97 98] [98 71 70 99]]
excercise_list = ['1' '2']

Using Numpy, I want to convert it to one multidimensional array to have the average grade for each exercise (the first item in grade_list refers to the exercise number 1)

The output should look like this: [[1. 2.] [91.75 84.5]] [[1. 2.] [91.75 84.5]]

Which means Avg. grade for exercise #1 - 91.75, and 84.5 for #2.

How I can convert it using numpy? I have read about NumPy axis parameter but not sure how to put it all together.

Axis 0 is the first nesting level (the two lists), axis 1 is the second level (four grades per entry in axis 0). You want to compute the mean along axis 1, so that axis 0 remains. So the mean grades are

mean_grades = np.mean(grade_list, axis=1) .

Then you stack the two lists in another nested list, wrap that in a numpy array and set the type to float (your excercises are strings):

result = np.array([excercise_list, mean_grades]).astype(float) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM