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]])

 

原文地址:https://www.cnblogs.com/Ting-light/p/9210279.html