python - 二维 numpy 数组中具有重复第一个元素的平均条目

标签 python numpy

我有一个看起来像这样的数组

  arr = np.array([[0, 1], [0, 2], [1, 3], [1, 3], [1, 4], [2, 3]])

我想取具有相同第一个元素的“条目”的平均值,即我的输出应该是

  [ [0, avg(1,2)] , [1, avg(3, 3, 4)], [2, 3] ]

执行此操作的最佳方法是什么?

最佳答案

这是一个使用 np.unique 的 NumPythonic 解决方案和 np.bincount对于第一列并不总是排序的一般情况 -

unqa,ID,counts = np.unique(arr[:,0],return_inverse=True,return_counts=True)
out = np.column_stack(( unqa , np.bincount(ID,arr[:,1])/counts ))

sample 运行-

In [4]: arr
Out[4]: 
array([[5, 1],
       [5, 2],
       [1, 3],
       [1, 3],
       [5, 4],
       [2, 3]])

In [5]: unqa,ID,counts = np.unique(arr[:,0],return_inverse=True,return_counts=True)
   ...: out = np.column_stack(( unqa , np.bincount(ID,arr[:,1])/counts ))
   ...: 

In [6]: out
Out[6]: 
array([[ 1.        ,  3.        ],
       [ 2.        ,  3.        ],
       [ 5.        ,  2.33333333]])

关于python - 二维 numpy 数组中具有重复第一个元素的平均条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33668125/

相关文章:

python - 无法在 virtualenv 中的 Windows 上安装 Django

python - 为什么 Numpy.array 比获取子列表的内置列表慢

python - 机器学习的数据分离

python - 如何使用元组数组查询 pandas DataFrame?

python - python中2个列表的线性回归

python - python中多维搜索的最佳算法

python - NLTK 无法打开文件(UnicodeDecoreError)

python - 复制文件,保留权限和所有者

java - 无法从cgi运行java命令

python - 在 NumPy 中转置存储在一维数组中的矩阵的最快方法?