python numpy的学习

来源:互联网 发布:扫码查价软件 编辑:程序博客网 时间:2024/05/22 06:46
>>> c = np.array([[1,2],[3,4]],dtype=complex)
>>> c
array([[ 1.+0.j,  2.+0.j],
       [ 3.+0.j,  4.+0.j]])
>>> c = np.array([[1,2],[3,4]],dtype=float)
>>> c
array([[ 1.,  2.],
       [ 3.,  4.]])
>>> c = np.array([[1,2],[3,4]],dtype=int)
>>> c
array([[1, 2],
       [3, 4]])


可以显示不同的类型。


>> 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)
>>> np.zeros((2,3,4), dtype=np.int16)
array([[[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]],


       [[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]]], dtype=int16)
>>> np.seconds((2,3,4), dtype=np.int16)


Traceback (most recent call last):
  File "<pyshell#67>", line 1, in <module>
    np.seconds((2,3,4), dtype=np.int16)
AttributeError: 'module' object has no attribute 'seconds'
>>> np.ones((2,3,4), dtype=int16)


Traceback (most recent call last):
  File "<pyshell#68>", line 1, in <module>
    np.ones((2,3,4), dtype=int16)
NameError: name 'int16' is not defined
>>> np.ones((2,3,4), dtype=np.int)
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=np.int int 前面必须添加np.




>>> np.arange(10,30,5)
array([10, 15, 20, 25])
>>> np.arange(10,40,5)
array([10, 15, 20, 25, 30, 35])
>>> np.arange(10,30,8)
array([10, 18, 26])
这是一个等距离排列的数组,第一位是起点,第二位是终点,第三位是步长。




>>> a = np.array([20,30,40,50])
>>> b = np.arange(4)
>>> b
array([0, 1, 2, 3])#现在的b相当于a


>>> a = np.random.random((2,3))
>>> a   #产生2行3列的二维数组,是0-1的随机数
array([[ 0.95380498,  0.86690915,  0.68586189],
       [ 0.70173128,  0.59340681,  0.79196249]])
>>> a.sum()
4.5936766007748053
>>> a.min()
0.59340680688712921



0 0
原创粉丝点击