python3 numpy学习

###1.1,numpy & pandas有什么用?
教程链接
numpy基于C,pandas基于numpy。基于矩阵的运算,运算速度比python自带list或者dictionary的更快。
###1.2,安装

pip3 install numpy #3指向python3
pip3 install pandas

###2.1, numpy属性

>>> array = np.array([[2,3,4],[1,2,3]])  #定义一个矩阵
>>> print(array)
[[2 3 4]
 [1 2 3]]
>>> print('number of dim',array.ndim)   #矩阵维数
number of dim 2
>>> print('shape',array.shape)  #行列数
shape (2, 3)
>>> print('shape',array.shape[1])
shape 3
>>> print('size:',array.size)  #总共有多少数
size: 6

###2.2 numpy创建array

#不同数字格式,如float, int, float64, float32等。
>>> a = np.array([2,3,4,5])
>>> a
array([2, 3, 4, 5])
>>> a.dtype
dtype('int64')
>>> print(a.dtype)
int64
>>> a = np.array([2,3,4,5],dtype=float)
>>> print(a.dtype)
float64
>>> b = np.zeros((3,4),dtype=int)  #np.zeros构造0矩阵,np.ones构造1矩阵,np.empty构造空矩阵(值接近于0)。
>>> b
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])
>>> b = np.ones((3,4),dtype=int)
>>> b
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])
>>> np.arange(12).reshape((3,4))    #reshape可重定行列数
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> np.linspace(1,10,20)      #生成线段,1到10里,生成20个数
array([  1.        ,   1.47368421,   1.94736842,   2.42105263,
         2.89473684,   3.36842105,   3.84210526,   4.31578947,
         4.78947368,   5.26315789,   5.73684211,   6.21052632,
         6.68421053,   7.15789474,   7.63157895,   8.10526316,
         8.57894737,   9.05263158,   9.52631579,  10.        ])

###2.3 numpy基础运算
######加减乘除

>>> a = np.array([10,20,30,40])
>>>> b=np.arange(4)
>>> print(a,b)
[10 20 30 40] [0 1 2 3]
>>> a - b
array([10, 19, 28, 37])
>>> b**2    #幂运算
array([0, 1, 4, 9])
>>> 10*np.sin(a)   #sin(),cos(),tan(),cot()运算
array([-5.44021111,  9.12945251, -9.88031624,  7.4511316 ])
>>> 10*np.tan(a)
array([  6.48360827,
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值