生成0和1的数组:
指定形状生成全为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)创建指定数量的等差数组:
示例代码:
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)指定步长创建等差数组:
示例代码:
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)创建等比数组:
示例代码:
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])