社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python:如何迭代一个n维矩阵?

André Ferreira • 5 年前 • 355 次点击  

我有一个带形状的矩阵(1255,13,13),所以我有13x13=169个元素,对于每个元素,我有一个由255个元素组成的数组。

我想迭代255数组的第四个元素,并计算有多少元素大于0.5。

此代码不起作用,但有助于理解我想要的内容:

out = net.forward()

count1=0
count2=0

for i in out[0]:
    for j in out[2]:
        for a in out[3]:
            for b in out[1]:
                if b[3]> 0.5:

                    count1+=1
                else:
                    count2+=1

最好的方法是什么?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38782
 
355 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Adam Smith
Reply   •   1 楼
Adam Smith    6 年前

如果我理解正确:

def give_counts(out):
    """Count the fourth element of each cell if it is >0.5

    >>> x = [0, 0, 0, 3]  # should show up in counts1
    >>> o = [0, 0, 0, 0]  # should show up in counts2
    >>> out = [[x, o, o],
    ...        [x, x, x],
    ...        [o, o, x]]
    >>> counts1, counts2 = give_counts(out)
    >>> assert counts1 == 5
    >>> assert counts2 == 4
    """

    values = (True if col[3] > 0.5 else False for row in out for col in row)
    counts = collections.Counter(values)
    return counts[True], counts[False]