• numpy.flatnonzero():


    numpy.flatnonzero():

      该函数输入一个矩阵,返回扁平化后矩阵中非零元素的位置(index)

    这是官方文档给出的用法,非常正规,输入一个矩阵,返回了其中非零元素的位置.

    复制代码
    1 >>> x = np.arange(-2, 3)
    2 >>> x
    3 array([-2, -1,  0,  1,  2])
    4 >>> np.flatnonzero(x)
    5 array([0, 1, 3, 4])
    import numpy as np
    d = np.array([1,2,3,4,4,3,5,3,6])
    haa = np.flatnonzero(d == 3)
    print (haa)
    
    [2 5 7]
    复制代码

    对向量元素的判断d==3返回了一个和向量等长的由0/1组成的矩阵,然后调用函数,返回的位置,就是对应要找的元素的位置。

    复制代码
    # Visualize some examples from the dataset.
    # We show a few examples of training images from each class.
    classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] #类别列表
    num_classes = len(classes) #类别数目
    samples_per_class = 7 # 每个类别采样个数
    for y, cls in enumerate(classes): # 对列表的元素位置和元素进行循环,y表示元素位置(0,num_class),cls元素本身'plane'等
        idxs = np.flatnonzero(y_train == y) #找出标签中y类的位置
        idxs = np.random.choice(idxs, samples_per_class, replace=False) #从中选出我们所需的7个样本
        for i, idx in enumerate(idxs): #对所选的样本的位置和样本所对应的图片在训练集中的位置进行循环
            plt_idx = i * num_classes + y + 1 # 在子图中所占位置的计算
            plt.subplot(samples_per_class, num_classes, plt_idx) # 说明要画的子图的编号
            plt.imshow(X_train[idx].astype('uint8')) # 画图
            plt.axis('off')
            if i == 0:
                plt.title(cls) # 写上标题,也就是类别名
    plt.show() # 显示
    复制代码
  • 相关阅读:
    Python网站目录爬行
    【编程】:记录一些函数
    Slowloris的基本使用
    SET——PowershellAttackVectors
    noi寒假刷题之旅_1.6编程基础之一维数组(15题)
    js进度条
    JS控制滚动条的位置
    svn和GitHub的使用
    tomcat配置项目虚拟路径
    重启服务器,上传的图片被删除
  • 原文地址:https://www.cnblogs.com/xiaoboge/p/9682928.html
Copyright © 2020-2023  润新知