Numpy生成01和范围数组

06 生成01和范围数组

生成0和1的数组

生成0和1的数组:

  • np.ones(shape,dtype)
  • np.ones_like(arr, dtype)
  • np.zeros(shape,dtype)
  • np.zeros_like(arr, dtype)

指定形状生成全为1的数组:

import numpy as np

np.ones([3,3])

输出结果:

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

根据已有数组生成全为0的数组:

import numpy as np

narr = np.ones([3,3])
np.zeros_like(narr)

输出结果:

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

生成固定范围的数组

np.linspace(start,stop,num,endpoint)创建指定数量的等差数组:

  • start:开始值
  • stop:终止值
  • num:等差值,间隔的数量
  • endpoint:是否包含结束值,默认为True

示例代码:

import numpy as np

np.linspace(0,100,10)

返回结果:

array([  0.        ,  11.11111111,  22.22222222,  33.33333333,
        44.44444444,  55.55555556,  66.66666667,  77.77777778,
        88.88888889, 100.        ])

np.arange(start,stop,step,dtype)指定步长创建等差数组:

  • start:开始值
  • stop:结束值
  • step:步长
  • dtype:数据类型

示例代码:

import numpy as np

np.arange(0,100,10)

返回结果:

array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])

np.logspace(start,stop,num)创建等比数组:

  • start:开始值
  • end:结束值
  • num:指定数量

示例代码:

import numpy as np

np.logspace(0,100,10)

返回结果:

array([1.00000000e+000, 1.29154967e+011, 1.66810054e+022, 2.15443469e+033,
       2.78255940e+044, 3.59381366e+055, 4.64158883e+066, 5.99484250e+077,
       7.74263683e+088, 1.00000000e+100])

你可能感兴趣的:(python,numpy,numpy)