接上篇文章,本章主要说明ndarray的快速创建对象 创建ndarray对象除了使用np.array还有一下几种方式快速创建。
1. 创建空的ndrray对象,因为没有赋值,所以会随机生成一些值。
>>> np.empty((4,4))
array([[ 0.00000000e+000, 0.00000000e+000, -4.94065646e-323,
0.00000000e+000],
[ 2.12199579e-314, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[ 1.77229088e-310, 3.50977866e+064, 0.00000000e+000,
0.00000000e+000],
[ nan, nan, 3.50977942e+064,
0.00000000e+000]])
>>> np.empty((4,))
array([ 0.00000000e+000, -1.73059404e-077, 9.88131292e-324,
2.78134232e-309])
- 指定类型: dtype='int'或者'uint'等
>>> np.empty((4,4),dtype='int')
array([[ 0, 0, -9223372036854775798,
0],
[ 4294967296, 0, 0,
0],
[ 35871566856192, 5572452859464646656, 0,
0],
[ -1, -140187915007369, 5572452860762084442,
0]])
>>> np.empty((4,4),dtype='uint')
array([[ 0, 0, 180366274849603603,
4402738160],
[ 4390252648, 17045276415608740984, 4402742864,
4390152352],
[ 0, 0, 0,
0],
[ 0, 0, 0,
0]], dtype=uint64)
2. 生成全为0的ndarray对象(类似全为0的行列式):
>>> np.zeros((4,4),dtype='uint')
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=uint64)
>>> np.zeros((4,4),dtype='int')
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
3. 全为1的ndarray对象,(类似全为0的行列式):
>>> np.ones((4,4),dtype='int')
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
>>> np.ones((4,4),dtype='uint')
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]], dtype=uint64)
4. 生成对角线上有值的ndarray对象:
>>> np.eye(4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
>>> np.eye(4,dtype='int')
array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
5. 通过已有数组列表创建ndarray对象,类似于np.array()
- 使用np.asarray(),创建普通ndarray对象
>>> list = [1,2,3,4,5]
>>> dt = np.asarray(list)
>>> print(dt)
[1 2 3 4 5]
>>> dt = np.asarray(list,dtype='float')
>>> print(dt)
[1. 2. 3. 4. 5.]
6. 通过已有数据通过流的范式读取,转化为ndarray对象
- 使用np.frombuffer(),创建ndarray对象
>>> strings = b'this is a string'
>>> dt = np.frombuffer(strings,dtype='S1')
>>> print(dt)
[b't' b'h' b'i' b's' b' ' b'i' b's' b' ' b'a' b' ' b's' b't' b'r' b'i'
b'n' b'g']
7. 通过可迭代对象中读取,转化为ndarray对象
- 使用np.forminter(),创建ndarray对象
>>> a = range(4)
>>> dt = np.fromiter(iter(a),dtype='float')
>>> print(dt)
[0. 1. 2. 3.]
8. 从取值范围中生成ndarray对象
- 使用arrange创建ndarray对象
参数的默认值如下:
np.arange(start,stop,step=1,dtype=None)
>>> dt = np.arange(1,10)
>>> print(dt)
[1 2 3 4 5 6 7 8 9]
- 使用linspace创建等差数列ndarray对象
参数的默认值如下:
np.linspace(start,stop,num=50,endpoint=False,retstep,dtype=None)
>>> dt = np.linspace(1,10)
>>> print(dt)
[ 1. 1.18367347 1.36734694 1.55102041 1.73469388 1.91836735
2.10204082 2.28571429 2.46938776 2.65306122 2.83673469 3.02040816
3.20408163 3.3877551 3.57142857 3.75510204 3.93877551 4.12244898
4.30612245 4.48979592 4.67346939 4.85714286 5.04081633 5.2244898
5.40816327 5.59183673 5.7755102 5.95918367 6.14285714 6.32653061
6.51020408 6.69387755 6.87755102 7.06122449 7.24489796 7.42857143
7.6122449 7.79591837 7.97959184 8.16326531 8.34693878 8.53061224
8.71428571 8.89795918 9.08163265 9.26530612 9.44897959 9.63265306
9.81632653 10. ]
>>> dt = np.linspace(start=1,stop=10,num=10)
>>> print(dt)
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
- 使用logspace创建等比数列ndarray对象
参数的默认值如下:
np.logspace(start,stop,num=50,endpoint=False,retstep,dtype=None)
>>> print(dt)
[1.00000000e+01 1.52641797e+01 2.32995181e+01 3.55648031e+01
5.42867544e+01 8.28642773e+01 1.26485522e+02 1.93069773e+02
2.94705170e+02 4.49843267e+02 6.86648845e+02 1.04811313e+03
1.59985872e+03 2.44205309e+03 3.72759372e+03 5.68986603e+03
8.68511374e+03 1.32571137e+04 2.02358965e+04 3.08884360e+04
4.71486636e+04 7.19685673e+04 1.09854114e+05 1.67683294e+05
2.55954792e+05 3.90693994e+05 5.96362332e+05 9.10298178e+05
1.38949549e+06 2.12095089e+06 3.23745754e+06 4.94171336e+06
7.54312006e+06 1.15139540e+07 1.75751062e+07 2.68269580e+07
4.09491506e+07 6.25055193e+07 9.54095476e+07 1.45634848e+08
2.22299648e+08 3.39322177e+08 5.17947468e+08 7.90604321e+08
1.20679264e+09 1.84206997e+09 2.81176870e+09 4.29193426e+09
6.55128557e+09 1.00000000e+10]
>>> dt = np.logspace(1,10,num=10)
>>> print(dt)
[1.e+01 1.e+02 1.e+03 1.e+04 1.e+05 1.e+06 1.e+07 1.e+08 1.e+09 1.e+10]
....待续