[英]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)使用 Numpy,我想将其转换为一个多维数组以获得每个练习的平均成绩(grade_list 中的第一项是指练习编号 1)
The output should look like this: [[1. 2.] [91.75 84.5]]
output 应如下所示:
[[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.
练习#1 的成绩为 91.75,#2 的成绩为 84.5。
How I can convert it using numpy?如何使用 numpy 进行转换? I have read about NumPy axis parameter but not sure how to put it all together.
我已阅读有关NumPy 轴参数的信息,但不知道如何将它们放在一起。
Axis 0 is the first nesting level (the two lists), axis 1 is the second level (four grades per entry in axis 0).轴 0 是第一个嵌套级别(两个列表),轴 1 是第二级(轴 0 中每个条目四个等级)。 You want to compute the mean along axis 1, so that axis 0 remains.
您想要计算沿轴 1 的平均值,以便保留轴 0。 So the mean grades are
所以平均成绩是
mean_grades = np.mean(grade_list, axis=1)
. 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):然后将两个列表堆叠在另一个嵌套列表中,将其包装在 numpy 数组中并将类型设置为浮点(您的练习是字符串):
result = np.array([excercise_list, mean_grades]).astype(float)
. result = np.array([excercise_list, mean_grades]).astype(float)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.