python - 取 numpy 数组中列的平均值

标签 python arrays numpy

我已经使用 numpy 从 csv 文件中获取数据。 numpy 数组的尺寸为:100*20。我如何取列的平均值(比如 col 3,5,8)并用包含这 3 个 cols 平均值的新列替换它们

如果

   col3 = 1,2,3,4
   col5 = 2,3,4,8
   col8 = 3,4,5,6

然后我想删除这 3 列并插入一个新列,其中每个条目都包含这 3 列中值的平均值

我想插入一个新列:2,3,4,6,删除前 3 列,最终数组的维度为 100*28

是否有任何 numpy 函数可以做到这一点?

最佳答案

a = np.arange(100*30).reshape(100,30) # change this to your own data
cols = [2, 4, 7]                      # columns to calculate averages, i.e. 3,5,8
b = a[:, cols]                        # data of the cols
c = b.mean(axis=1)                    # average
a_no_b = np.delete(a, cols, axis=1)   # data without the cols
a_final = np.c_[a_no_b, c]            # final result

关于python - 取 numpy 数组中列的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45948395/

相关文章:

python - 如何在 Linux 中的键盘上获取箭头键和回车键,使其表现得像 Windows7

javascript - 数组中的值被覆盖

python - numpy 数组过滤和替换

python - xtensor 类型的性能与 NumPy 的简单归约

python - 相当于 lldb 中的 python gdb.execute ('...' )

python - 如何使用python写出内存csv?

python - scipy.optimize.lsq_linear 边界错误

javascript - For循环条件问题

python矩阵乘法: how to handle very large matrices?

python - 在 Python 中重新使用序列作为列表的一部分