zoukankan      html  css  js  c++  java
  • numpy学习

    numpy学习
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS_HTML"></script>
    <!-- MathJax configuration -->
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\(","\)"] ],
            displayMath: [ ['$$','$$'], ["\[","\]"] ],
            processEscapes: true,
            processEnvironments: true
        },
        // Center justify equations in code and markdown cells. Elsewhere
        // we use CSS to left justify single line equations in code cells.
        displayAlign: 'center',
        "HTML-CSS": {
            styles: {'.MathJax_Display': {"margin": 0}},
            linebreaks: { automatic: true }
        }
    });
    </script>
    <!-- End of mathjax configuration --></head>
    
    In [121]:
    import numpy as np
    
    </div>
    

    为啥使用numpy

    计算购物车的总共价格

    In [2]:
    shop_car = [2,3,10,5]
    shop_price = [10,200,150,50]
    
    </div>
    
    In [3]:
    shop_car_np = np.array(shop_car) 
    
    </div>
    
    In [4]:
    shop_price_np = np.array(shop_price)
    
    </div>
    
    In [5]:
    shop_car_np
    
    </div>
    
    <div class="prompt output_prompt">Out[5]:</div>
    
    array([ 2,  3, 10,  5])
    In [6]:
    shop_price_np
    
    </div>
    
    <div class="prompt output_prompt">Out[6]:</div>
    
    array([ 10, 200, 150,  50])
    In [8]:
    np.sum(shop_car_np * shop_price_np)  #### 向量运算
    
    </div>
    
    <div class="prompt output_prompt">Out[8]:</div>
    
    2370
    In [123]:
    shop_car_np * 7  #### 矢量运算
    
    </div>
    
    <div class="prompt output_prompt">Out[123]:</div>
    
    array([14, 21, 70, 35])
    In [125]:
    shop_car * 7  
    
    </div>
    
    <div class="prompt output_prompt">Out[125]:</div>
    
    [2,
     3,
     10,
     5,
     2,
     3,
     10,
     5,
     2,
     3,
     10,
     5,
     2,
     3,
     10,
     5,
     2,
     3,
     10,
     5,
     2,
     3,
     10,
     5,
     2,
     3,
     10,
     5]

    常见属性

    一维数组

    In [10]:
    arr = np.array([1,2,3,4,5,6])
    
    </div>
    
    In [12]:
    arr  #### 简单的ndarray一维数组
    
    </div>
    
    <div class="prompt output_prompt">Out[12]:</div>
    
    array([1, 2, 3, 4, 5, 6])
    In [14]:
    arr.dtype   #### 数组的数据类型
    
    </div>
    
    <div class="prompt output_prompt">Out[14]:</div>
    
    dtype('int64')
    In [16]:
    arr.size  ##### 数组元素的个数
    
    </div>
    
    <div class="prompt output_prompt">Out[16]:</div>
    
    6
    In [17]:
    arr.ndim  #### 数组的维度
    
    </div>
    
    <div class="prompt output_prompt">Out[17]:</div>
    
    1
    In [18]:
    arr.shape
    
    </div>
    
    <div class="prompt output_prompt">Out[18]:</div>
    
    (6,)

    二维数组

    In [19]:
    arr2 = np.array([[1,2,3,4,5], [6,7,8,9,10]])
    
    </div>
    
    In [20]:
    arr2
    
    </div>
    
    <div class="prompt output_prompt">Out[20]:</div>
    
    array([[ 1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10]])
    In [21]:
    arr2.ndim  
    
    </div>
    
    <div class="prompt output_prompt">Out[21]:</div>
    
    2
    In [22]:
    arr2.shape
    
    </div>
    
    <div class="prompt output_prompt">Out[22]:</div>
    
    (2, 5)
    In [23]:
    arr2.T #### 高维数组转置
    
    </div>
    
    <div class="prompt output_prompt">Out[23]:</div>
    
    array([[ 1,  6],
           [ 2,  7],
           [ 3,  8],
           [ 4,  9],
           [ 5, 10]])

    数据类型转换

    In [24]:
    arr
    
    </div>
    
    <div class="prompt output_prompt">Out[24]:</div>
    
    array([1, 2, 3, 4, 5, 6])
    In [25]:
    arr.dtype
    
    </div>
    
    <div class="prompt output_prompt">Out[25]:</div>
    
    dtype('int64')
    In [26]:
    arr.astype('float')  #### 强制转换指定的数据类型
    
    </div>
    
    <div class="prompt output_prompt">Out[26]:</div>
    
    array([1., 2., 3., 4., 5., 6.])

    ndarray数组的创建方式

    In [27]:
    arr3 = np.array([1,2,3,4,5,6], dtype='float')
    
    </div>
    
    In [28]:
    arr3
    
    </div>
    
    <div class="prompt output_prompt">Out[28]:</div>
    
    array([1., 2., 3., 4., 5., 6.])
    In [29]:
    np.arange(1,10)  ### numpy版的arange
    
    </div>
    
    <div class="prompt output_prompt">Out[29]:</div>
    
    array([1, 2, 3, 4, 5, 6, 7, 8, 9])
    In [30]:
    np.arange(10)
    
    </div>
    
    <div class="prompt output_prompt">Out[30]:</div>
    
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    In [34]:
    np.linspace(1,10, num=5, endpoint=False)  ##### 均匀分配多少份
    
    </div>
    
    <div class="prompt output_prompt">Out[34]:</div>
    
    array([1. , 2.8, 4.6, 6.4, 8.2])
    In [36]:
    np.linspace?  #### 查看函数的使用方法
    
    </div>
    
    <div class="prompt"></div>
    
      File "<ipython-input-36-fe95272a62c5>", line 1
        np.linspace?  #### 查看函数的使用方法
                   ^
    SyntaxError: invalid syntax
    
    In [37]:
    np.zeros?
    
    </div>
    
    In [38]:
    np.zeros(5)
    
    </div>
    
    <div class="prompt output_prompt">Out[38]:</div>
    
    array([0., 0., 0., 0., 0.])
    In [40]:
    np.zeros((2,3), dtype='int')
    
    </div>
    
    <div class="prompt output_prompt">Out[40]:</div>
    
    array([[0, 0, 0],
           [0, 0, 0]])
    In [41]:
    np.ones(5)
    
    </div>
    
    <div class="prompt output_prompt">Out[41]:</div>
    
    array([1., 1., 1., 1., 1.])
    In [42]:
    np.ones((3,4))
    
    </div>
    
    <div class="prompt output_prompt">Out[42]:</div>
    
    array([[1., 1., 1., 1.],
           [1., 1., 1., 1.],
           [1., 1., 1., 1.]])
    In [43]:
    np.empty?
    
    </div>
    
    In [46]:
    np.empty((2,3))
    
    </div>
    
    <div class="prompt output_prompt">Out[46]:</div>
    
    array([[1., 2., 3.],
           [4., 5., 6.]])
    In [47]:
    np.eye?
    
    </div>
    
    In [51]:
    np.eye(5, dtype='int')  ### 默认生成5行5列的数组, 并且数组的对角线默认是1
    
    </div>
    
    <div class="prompt output_prompt">Out[51]:</div>
    
    array([[1, 0, 0, 0, 0],
           [0, 1, 0, 0, 0],
           [0, 0, 1, 0, 0],
           [0, 0, 0, 1, 0],
           [0, 0, 0, 0, 1]])

    ndarray的索引

    一维数组
    In [56]:
    arr
    
    </div>
    
    <div class="prompt output_prompt">Out[56]:</div>
    
    array([1, 2, 3, 4, 5, 6])
    In [58]:
    #### 在列表中怎么通过索引获取值,在这里也是一样的
    arr[2]
    
    </div>
    
    <div class="prompt output_prompt">Out[58]:</div>
    
    3

    二维数组

    In [59]:
    arr2
    
    </div>
    
    <div class="prompt output_prompt">Out[59]:</div>
    
    array([[ 1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10]])
    In [61]:
    arr2[0,2]   ##### 逗号前面是行索引, 逗号后面是列索引
    
    </div>
    
    <div class="prompt output_prompt">Out[61]:</div>
    
    3

    切片

    一维数组

    In [62]:
    arr
    
    </div>
    
    <div class="prompt output_prompt">Out[62]:</div>
    
    array([1, 2, 3, 4, 5, 6])
    In [76]:
    arr[0:-1]
    
    </div>
    
    <div class="prompt output_prompt">Out[76]:</div>
    
    array([1, 2, 3, 4, 5])

    二维数组

    In [65]:
    arr2
    
    </div>
    
    <div class="prompt output_prompt">Out[65]:</div>
    
    array([[ 1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10]])
    In [74]:
    arr2[:,1:3]  ##### 和索引的使用方法一致, 逗号前面是行索引 , 后面是列索引
    
    </div>
    
    <div class="prompt output_prompt">Out[74]:</div>
    
    array([[2, 3],
           [7, 8]])
    In [75]:
    arr2[:, :]
    
    </div>
    
    <div class="prompt output_prompt">Out[75]:</div>
    
    array([[ 1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10]])

    布尔型索引

    In [77]:
    ###查询arr中大于4的所有的数据
    arr
    
    </div>
    
    <div class="prompt output_prompt">Out[77]:</div>
    
    array([1, 2, 3, 4, 5, 6])
    In [78]:
    ###查询arr中大于4的所有的数据
    arr > 4
    
    </div>
    
    <div class="prompt output_prompt">Out[78]:</div>
    
    array([False, False, False, False,  True,  True])
    In [79]:
    arr[arr>4]   ##### 布尔型索引
    
    </div>
    
    <div class="prompt output_prompt">Out[79]:</div>
    
    array([5, 6])
    In [80]:
    arr2
    
    </div>
    
    <div class="prompt output_prompt">Out[80]:</div>
    
    array([[ 1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10]])
    In [82]:
    arr2[arr2 > 8]
    
    </div>
    
    <div class="prompt output_prompt">Out[82]:</div>
    
    array([ 9, 10])

    通用函数

    In [84]:
    a = -4
    np.abs(a)
    
    </div>
    
    <div class="prompt output_prompt">Out[84]:</div>
    
    4
    In [85]:
    np.ceil(4.3)  #### 向上取整
    
    </div>
    
    <div class="prompt output_prompt">Out[85]:</div>
    
    5.0
    In [86]:
    np.floor(4.6)  #### 向下取整
    
    </div>
    
    <div class="prompt output_prompt">Out[86]:</div>
    
    4.0
    In [87]:
    np.rint(4.6)
    
    </div>
    
    <div class="prompt output_prompt">Out[87]:</div>
    
    5.0
    In [88]:
    np.rint(4.4)
    
    </div>
    
    <div class="prompt output_prompt">Out[88]:</div>
    
    4.0
    In [89]:
    np.modf(5.6)
    
    </div>
    
    <div class="prompt output_prompt">Out[89]:</div>
    
    (0.5999999999999996, 5.0)
    In [90]:
    ## isnan  ### 判断某一个数是否是 nan === not a number (不是一个数)
    np.nan
    
    </div>
    
    <div class="prompt output_prompt">Out[90]:</div>
    
    nan
    In [91]:
    arr
    
    </div>
    
    <div class="prompt output_prompt">Out[91]:</div>
    
    array([1, 2, 3, 4, 5, 6])
    In [94]:
    np.sum(arr)
    
    </div>
    
    <div class="prompt output_prompt">Out[94]:</div>
    
    21
    In [95]:
    np.cumsum(arr)  #### 累加
    
    </div>
    
    <div class="prompt output_prompt">Out[95]:</div>
    
    array([ 1,  3,  6, 10, 15, 21])
    In [96]:
    arr
    
    </div>
    
    <div class="prompt output_prompt">Out[96]:</div>
    
    array([1, 2, 3, 4, 5, 6])
    In [97]:
    np.min(arr)
    
    </div>
    
    <div class="prompt output_prompt">Out[97]:</div>
    
    1
    In [98]:
    np.max(arr)
    
    </div>
    
    <div class="prompt output_prompt">Out[98]:</div>
    
    6
    In [99]:
    np.argmin(arr)
    
    </div>
    
    <div class="prompt output_prompt">Out[99]:</div>
    
    0

    随机数

    In [100]:
    np.random.rand(10)  ### 随机生成10个0-1之间的数字
    
    </div>
    
    <div class="prompt output_prompt">Out[100]:</div>
    
    array([0.56187207, 0.79693148, 0.83056134, 0.97434263, 0.08407437,
           0.3006474 , 0.56696281, 0.95745467, 0.75604117, 0.25491663])
    In [106]:
    np.random.randint(5)
    
    </div>
    
    <div class="prompt output_prompt">Out[106]:</div>
    
    3
    In [109]:
    np.random.choice(5,3)  ### 随机生成3个 0-5 之间的数
    
    </div>
    
    <div class="prompt output_prompt">Out[109]:</div>
    
    array([4, 4, 0])
    In [110]:
    np.random.shuffle?
    
    </div>
    
    In [111]:
    arr
    
    </div>
    
    <div class="prompt output_prompt">Out[111]:</div>
    
    array([1, 2, 3, 4, 5, 6])
    In [112]:
    np.random.shuffle(arr)   #### 打乱给定的一个数组
    
    </div>
    
    In [113]:
    arr
    
    </div>
    
    <div class="prompt output_prompt">Out[113]:</div>
    
    array([4, 1, 2, 6, 3, 5])
    In [114]:
    arr
    
    </div>
    
    <div class="prompt output_prompt">Out[114]:</div>
    
    array([4, 1, 2, 6, 3, 5])
    In [119]:
    arr.reshape(3,2)   #### 改变一维数组的形状成二维数组
    
    </div>
    
    <div class="prompt output_prompt">Out[119]:</div>
    
    array([[4, 1],
           [2, 6],
           [3, 5]])
    In [ ]:
     
    
    </div>
    

    numpy模块第二部分:numpy模块 03

  • 相关阅读:
    雪花算法 适用于ID 自增
    SB ,mybatis generator 插件 实现 更新操作
    spark优化
    Hive的导入导出方式汇总
    推荐系统架构图
    DBScan算法
    机器学习-逻辑回归算法
    机器学习-微博精准营销
    机器学习-TF-IDF算法
    机器学习-KNN识别手写数字
  • 原文地址:https://www.cnblogs.com/zhangchaocoming/p/12403965.html
Copyright © 2011-2022 走看看