如何利用元组的列的平均 [英] how to take mean of the column of tuple

查看:489
本文介绍了如何利用元组的列的平均的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元组,我转换为numpay阵列

  DT = np.dtype(浮动,浮动)
AR = np.array(VAL,DTYPE = DT)

筛选

  AR = [(0.08181818181818182,0.394023569023569)(0.0,0.0)
 (0.16785714285714287,0.3227678571428571)

我想借此列意味着这个数组(0.08 + 0 + 0.16)

试过这个code

  np.mean(AR,轴= 0)

但它给这个错误

 打印np.mean(AR,轴= 0)
  文件/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py,线路2878,平均
    OUT =出来,keepdims = keepdims)
  文件/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py65行,在_mean
    RET = umr_sum(ARR,轴,DTYPE,出,keepdims)
类型错误:无法执行与灵活型减少


解决方案

 导入numpy的是NPDT = np.dtype(浮动,浮动)
AR = np.array([],DTYPE = DT)AR = [(0.08181818181818182,0.394023569023569),(0.0,0.0),
    (0.16785714285714287,0.3227678571428571)打印(np.mean(AR,轴= 0))

输出

  [0.08322511 0.23893048]

更新

这是我的耻辱!这些线是多余的:

  DT = np.dtype(浮动,浮动)
AR = np.array([],DTYPE = DT)

由于没有下一行, AR 变成一个再分配列表而不是 numpy.ndarray

因此​​,无论你应该只使用

  AR = [(0.08181818181818182,0.394023569023569),(0.0,0.0),
    (0.16785714285714287,0.3227678571428571)打印(np.mean(AR,轴= 0))

或者,你需要:

  AR = np.array(
    [
        (0.08181818181818182,0.394023569023569)
        (0.0,0.0),
        (0.16785714285714287,0.3227678571428571)
    ])打印(类型(AR))
打印(np.mean(AR,轴= 0))

输出

 <类的numpy.ndarray'>
[0.08322511 0.23893048]

I have a tuple which I convert to numpay array

dt=np.dtype('float,float') 
ar=np.array(val,dtype=dt)

Like this

ar=[(0.08181818181818182, 0.394023569023569) (0.0, 0.0)
 (0.16785714285714287, 0.3227678571428571)]

I want to take columns mean of this array ( 0.08+0+0.16)

tried this code

np.mean(ar, axis=0)

but its giving this error

 print np.mean(ar, axis=0)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 2878, in mean
    out=out, keepdims=keepdims)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/_methods.py", line 65, in _mean
    ret = umr_sum(arr, axis, dtype, out, keepdims)
TypeError: cannot perform reduce with flexible type

解决方案

import numpy as np

dt=np.dtype('float,float')
ar=np.array([], dtype=dt)

ar=[(0.08181818181818182, 0.394023569023569), (0.0, 0.0),
    (0.16785714285714287, 0.3227678571428571)]

print(np.mean(ar, axis=0))

OUTPUT:

[ 0.08322511  0.23893048]

UPDATE

Shame on me! These lines are redundant:

dt=np.dtype('float,float')
ar=np.array([], dtype=dt)

Since there is a reassignment on the next line, ar becomes list instead of numpy.ndarray.

So either you should use just

ar=[(0.08181818181818182, 0.394023569023569), (0.0, 0.0),
    (0.16785714285714287, 0.3227678571428571)]

print(np.mean(ar, axis=0))

or, you need:

ar = np.array(
    [
        (0.08181818181818182, 0.394023569023569),
        (0.0, 0.0),
        (0.16785714285714287, 0.3227678571428571)
    ])

print(type(ar))
print(np.mean(ar, axis=0))

OUTPUT:

<class 'numpy.ndarray'>
[ 0.08322511  0.23893048]

这篇关于如何利用元组的列的平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆