zoukankan      html  css  js  c++  java
  • numpy 小示例

    import numpy as np 

    生成 3*4 的由  0 组成的二维数组

    >>> np.zeros((3,4))

    array([[0., 0., 0., 0.],
    [0., 0., 0., 0.],
    [0., 0., 0., 0.]])

    生成  2*3*4   的由  1 组成的三维数组

    >>>np.ones((2,3,4),dtype=np.int16)

    array([[[1, 1, 1, 1],
    [1, 1, 1, 1],
    [1, 1, 1, 1]],

    [[1, 1, 1, 1],
    [1, 1, 1, 1],
    [1, 1, 1, 1]]], dtype=int16)

    生成 10 ~25 间的 间隔为5的数列

    >>>np.arange(10,25,5)

    array([10, 15, 20])

    生成 0~2间的等差数列,元素个数 为 9 

    >>>np.linspace(0,2,9)

    array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.25, 1.5 , 1.75, 2.  ])

    以给定数 7 ,生成 指定形状2*2的矩阵 

    >>> np.full((2,2),7)
    array([[7, 7],
    [7, 7]])

    生成对角矩阵, 长与宽相等 ,默认中间对角线(左上到右下)全是1,其余为0

    >>np.eye(2)

    array([[1., 0.],
    [0., 1.]])

    生成数值在1以内的指定形状 2*2 的随机矩阵

    >>> np.random.random((2,2))
    array([[0.36248815, 0.60881839],
    [0.10805521, 0.92618553]])

     

    生成指定形状的空数组,数组内数据,可能是随机的

    empty, unlike zeros, does not set the array values to zero, and may therefore be marginally faster. On the other hand, it requires the user to manually set all the values in the array, and should be used with caution.

    >>np.empty((3,2))

    array([[1.39069238e-309, 1.39069238e-309],
    [1.39069238e-309, 1.39069238e-309],
    [1.39069238e-309, 1.39069238e-309]])

     

  • 相关阅读:
    eval()函数的使用
    Ajax的GET,POST方法传输数据和接收返回数据
    使用目录
    安装目录
    c 文件输入和输出
    【C++王桂林】
    【C++】虚函数表vtable理解
    【C++】内存管理内幕阅读笔记
    git提交中的索引错误问题
    pydicom编辑、删除DICOM文件中病人相关信息
  • 原文地址:https://www.cnblogs.com/Ting-light/p/9210279.html
Copyright © 2011-2022 走看看