zoukankan      html  css  js  c++  java
  • 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上下文管理器
    python文件处理
    python爬虫之路(一)-----requests库详解
    深入理解http(四)----http补充
    深入理解http(三)----http进阶hhtps
    深入理解http(二)------http的缓存机制及原理
    深入理解http(一)
    XML学习之路(二)
    Nginx 编译安装-1.61.1
    LVS-DR 模式
  • 原文地址:https://www.cnblogs.com/xiaoboge/p/9682928.html
Copyright © 2011-2022 走看看