接上篇文章,本章主要說明ndarray的快速建立對象 建立ndarray對象除了使用np.array還有一下幾種方式快速建立。數組
>>> 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])
複製代碼
>>> 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)
複製代碼
>>> 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]])
複製代碼
>>> 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)
複製代碼
>>> 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]])
複製代碼
>>> 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.]
複製代碼
>>> 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']
複製代碼
>>> a = range(4)
>>> dt = np.fromiter(iter(a),dtype='float')
>>> print(dt)
[0. 1. 2. 3.]
複製代碼
參數的默認值以下:
np.arange(start,stop,step=1,dtype=None)
複製代碼
>>> dt = np.arange(1,10)
>>> print(dt)
[1 2 3 4 5 6 7 8 9]
複製代碼
參數的默認值以下:
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.]
複製代碼
參數的默認值以下:
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]
複製代碼
....待續bash