zoukankan      html  css  js  c++  java
  • anaconda及jupyter notebook的使用之numpy模块的用法(2)

    今日内容概要

    numpy模块结束

    • ndarray创建

    • numpy内置方法

    • 索引与切片(花式索引、布尔索引)

    • 常用函数

    • 统计方法

    • 随机数

    numpy的内置方法

    import numpy as np
    1.
    # 1.ndarray的创建
    np.array([1,2,3,4,5,6,7],ndmin=3)
    array([[[1,2,3,4,5,6,7]])
    2.
    # 2.python中的range
    # for i in range(10):
    #      print(i)
    np.arange(1,7)  # arange比python中的range更好用
    array([1,2,3,4,5,6])
    np.arange(1.0,8.0)
    array([1.,2.,3.,4.,5.,6.,7.])
    int是否包含最后一个数  
    # 3.linspace
        np.linspace(1,10,num=20,retstep=True,endpoint=False) # retstep展示间隔数  endpoint是否包含最后一个数  
        (array([1.  , 1.45, 1.9 , 2.35, 2.8 , 3.25, 3.7 , 4.15, 4.6 , 5.05, 5.5 ,
            5.95, 6.4 , 6.85, 7.3 , 7.75, 8.2 , 8.65, 9.1 , 9.55]), 0.45)
    
    # 4.zeros
    np.zeros(10,dtype=int) # 默认都是float
    res = np.zeros((3,5),dtype=int) # 默认都是二维
    res
       array([[0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0]])
    # 5.ones 用法跟zeros一致
    np.ones(10)
    array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
    
    # 6.empty
    np.empty((3,3))  # 默认也是二维
    array([[0.00000000e+000, 0.00000000e+000, 0.00000000e+000],
           [0.00000000e+000, 0.00000000e+000, 6.14617663e-321],
           [8.34448532e-308, 1.69105613e-306, 2.56765117e-312]])       
     
    # 7.eye
    np.eye(10,dtype=int)
    array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])       
    

    ndarray跟标量运算

    统一的规律都是
        数组内的每一个元素都跟该元素(数字)做运算
     
    数组与数组之间做运算
         两个数组的大小得一致
         运算的时候按照对应的位置计算
    

    索引与切片

    2 索引
    # python中索引:从0开始的
    # l = [111,222,333,444,555,666]
    # l[1]
    res = np.arrray([111,222,333,444,555,666,777])
    res[2] # numpy中索引也是从0开始
    333
    
    
    res1 = np.array([[1,2,3,4],[5,6,7,8]])
    res1
    '''
    在numpy索引的规律
                  0  1  2  3  列索引
       行索引     [
           0     [1, 2, 3, 4],
           1     [5, 6, 7, 8] 
                    ]
    '''
    array([[1, 2, 3, 4],
           [5, 6, 7, 8]])
    面是列索引
    # 求上述二维数字里面的元素7
    # res[1][2] # 写法1
    res1[1,2]  # 写法2  逗号前面是行索引,逗号后面是列索引
    7
    3   切片
    '''
    切片取值
    l1 = [11,22,33,44,55,66,77,88]
    l1[1:3]
    ​
    numpy切片操作跟python中的切片操作一致
    '''
    res2 = np.array([11,22,33,44,55,66,77])
    res2[1:6]
    array([22, 33, 44, 55, 66])
    res3 = np.array([[1,2,3,4],[5,6,7,8],[9,11,22,33]])
    res3
    array([[ 1,  2,  3,  4],
           [ 5,  6,  7,  8],
           [ 9, 11, 22, 33]])
    # 获取上述ndarray里面的6,7,11,22
    res3[1:3,1:3]
    array([[ 6,  7],
           [11, 22]])
    4  运算
    res * 3
    res = np.array([[1,2,3],[4,5,6]])
    res * 3
    array([[ 3,  6,  9],
           [12, 15, 18]])
    res1 + res2
    res1 = np.array([[11,22,33],[44,55,66]])
    res2 = np.array([[1,2,3],[4,5,6]])
    res1 + res2
    array([[12, 24, 36],
           [48, 60, 72]])
    
    res1 * res2
    array([[ 11,  44,  99],
           [176, 275, 396]])
    
    res3 = np.array([[1,2,3,4],[5,6,7,8]])
    res1 + res3
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-56-b5f7cab23041> in <module>
          1 res3 = np.array([[1,2,3,4],[5,6,7,8]])
    ----> 2 res1 + res3
    
    ValueError: operands could not be broadcast together with shapes (2,3) (2,4) 
    
    reshap
    
    res4 = np.array([111,222,333,444,555,666])
    # 转换数组的维数  转的时候一定要注意元素的个数到底够不够 不够直接报错
    res4.reshape(2,3)
    array([[111, 222, 333],
           [444, 555, 666]])
    
    res4.reshape(3,3)
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-60-274f26b3bc7a> in <module>
    ----> 1 res4.reshape(3,3)
    
    ValueError: cannot reshape array of size 6 into shape (3,3)
    
    

    布尔型索引

    import random
    li = [random.randint(1,10) for i in range(30)]
    '''
    python里面的列表生成式
    new_list = []
    for i in range(30):
        new_list.append(random.randint(1,10))  # 往列表中添加30个1到10的随机数
    '''
    res = np.array(li)
    res
    array([ 3,  8,  7, 10,  6,  2,  7,  4,  1, 10,  5,  3,  6,  6,  2,  9,  6,
            8,  1,  5,  7,  8,  2,  6,  6,  4,  5,  5,  9,  5])
    
    # 求出数字中大于5的数
    # res > 5 # 就是跟数组里面的每一个元素作比较    结果是布尔值索引
    res[res>5] # 布尔值索引取值
    # 布尔型索引:将同样大小的布尔数组传进索引,会返回一个有True对应位置的元素的数组
    array([ 8,  7, 10,  6,  7, 10,  6,  6,  9,  6,  8,  7,  8,  6,  6,  9])
    

    花式索引

    res5 = np.array([1,2,3,4,5,6,7,8,9,10])
    # 拿出 2 6 9 10
    res5[[1,5,8,9]] # 花式索引
    array([ 2,  6,  9, 10])
    # 在python中如何获取元素的索引值
    # l = [111,222,333,444,555,666]
    # l.index(333)
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-70-305672a1ca18> in <module>
          2 # l = [111,222,333,444,555,666]
          3 # l.index(333)
    ----> 4 res5.index(3)
    
    AttributeError: 'numpy.ndarray' object has no attribute 'index'
    
    

    通用函数

    # 1.绝对值
    np.abs([-1,-2,-3])
    np.fabs([-11.,-22.,-33.])
    array([11., 22., 33.])
    
    # 2.平方根
    np.sprt(2)
    1.4142135623730951
    # 3 平方
    np.square(4)
    16
    # 4 e的次方
    np.exp(1)
    2.718281828459045
    # 5 自然对数
    np.log(4)
    1.3862943611198906
    # 6 ceil取整 取大
    np.ceil(5.4)
    6.0
    # 7 floor取整 取小
    np.floor(4.6)
    4.0
    # 8 四舍五入
    np.rint(4.6)
    5.0
    # 9 将小数分割成整数和小数
    np.modf(4.5)
    (0.5, 4.0)
    np.modf([3.4,5.6])
    (array([0.4, 0.6]), array([3., 5.]))
    # 10 isnan  判断当前数据是否缺失
    np.isnan(56)
    False
    # 11 cos sin tan
    np.cos(45)
    0.5253219888177297
    

    补充

    1.自动提示
    
    ​    按TAB键
    
    2.自动查看方法的使用方式
    
       在方法名后面加?运行即可
    
    
    
    3.代码的注释
    
    ​         ctrl + ?
    
    为了舒适的结果,眼前的坎坷路程即使再长都是值得的。
  • 相关阅读:
    Python中使用pip安装库时提示:远程主机强迫关闭了一个现有的连接
    一个真正有能力的人
    一个程序员的自白(延迟满足)
    小规模团队如何“微服务”管理
    一个程序员的自白(无知之者)
    小型系统如何“微服务”开发
    TCP协议学习总结(下)
    TCP协议学习总结(中)
    TCP协议学习总结(上)
    一个程序员的自白(三十而立)
  • 原文地址:https://www.cnblogs.com/abudrSatan1998/p/13597044.html
Copyright © 2011-2022 走看看