python numpy的学习

>>> 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值