{DAY 12} NumPy 学习笔记(上)

前言

今天来到自学NumPy?

之前在学校上过基础的NumPy

今天要从youtube上找影片完整的学习一次

这个单元将分成三篇文章介绍

分别是NumPy简介+基本语法,NumPy的语法,NumPy的数学运算与基础绘图

https://www.youtube.com/watch?v=QUT1VHiLmmI&t=394s

NumPy简介

  • 主要处理多维度的阵列运算
  • 应用在大量资料处理上,也是许多套件的基础
  • 要往资料科学的世界深入了解的话,学习numpy会是最基本的常备知识!

学习笔记

What is NumPy?

NumPy is a multi-dimensional array library.

Compare Lists with NumPy

NumPy is faster for these reasons

  1. NumPy uses less bytes of memory
  2. no type checking when iterating through objects. Since in lists you could have a list of integers , float, string or boolean, so when go through list, you have to do type check on each element.
  3. NumPy use contiguous memory to store while lists store them scattering.

Application of NumPy?

  1. mathematics
  2. plotting (Matplotlib)
  3. backend of different applications( Pandas, Connect4, Digital Photography)
  4. Machine learning

基本语法练习

  1. 首先要先import 套件

    import numpy as np
    
  2. 建立阵列:(ndarray 储存的是相同资料型别的元素,例如:int、float 等)

    1. 产生指定阵列,或是从原本的list跟tuple更改:np.array(list或tuple都可放入)

      # 建立一维阵列
      a = np.array([1,2,3])
      print(a)
      #[1 2 3]
      
      # 建立二维阵列
      b = np.array([[5,3,6,7,1], [1,2,3,4,5]])
      print(b)
      #[[5 3 6 7 1]
       [1 2 3 4 5]]
      
      #将list换成numpy array:
      km_list = [3, 5, 10, 21, 42.195]
      km_array = np.array(km_list)
      print(km_array)
      #array([ 3.   ,  5.   , 10.   , 21.   , 42.195])
      
    2. 产生指定数字范围的阵列:np.arange(起始,结束)

      #产生数字范围
      arr1 = np.arange(10) #从0到9
      print(arr1)
      #[0 1 2 3 4 5 6 7 8 9]
      arr2 = np.arange(3,11) 
      print(arr2)
      #[ 3  4  5  6  7  8  9 10]
      

    c. 产生都是1或都是0的阵列:np.zeros(), np.ones()

    print(np.zeros((3,5)) #产生3*5,里面数值皆为0的阵列
    #array([[0., 0., 0., 0., 0.],
           [0., 0., 0., 0., 0.],
           [0., 0., 0., 0., 0.]])
    
    print(np.ones((4,2,2), dtype = "int32")) #也可以指定元素的type
    # array([[[1, 1],
            [1, 1]],
    
           [[1, 1],
            [1, 1]],
    
           [[1, 1],
            [1, 1]],
    
           [[1, 1],
            [1, 1]]], dtype=int32)
    

    d. 产生填满指定数字的阵列: np.full((the shape),the value)

    #上面我们看到内建可以创造都是0或1的语法,如果我们想要创其他数值的话
    #可以使用np.full((the shpae),the value)
    #当我们想创建填满99的3*3阵列
    np.full((3,3),99)
    '''
    array([[99, 99, 99],
           [99, 99, 99],
           [99, 99, 99]])'''
    
    #阵列的shape也可以用已经使用过的其他阵列的形状
    #语法是.full_like(阵列名称, 数值)
    #先创立一个叫做a的2*7阵列
    a = np.array([[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]])
    
    #想要创造跟a一样形状的阵列,只需要更改变数
    #假如我想要创造一个跟a一样形状,里面数值皆为5的阵列
    np.full_like(a, 5)
    '''
    array([[5, 5, 5, 5, 5, 5, 5],[5, 5, 5, 5, 5, 5, 5]])'''
    

    e. 产生随机阵列: .random.rand(shape)

    产生指定在数值范围内+指定形状: random.randint(startvalue, endvalue, size=())

    f. 产生identity matrix : .identity( )

    np.identity(5)
    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.]])
    

    g. 复制阵列: .repeat( )

    # 当我们想要复制一维阵列几次时
    arr = np.array([[1,2,3,4]]) #当我们想看到[1,2,3]以row的形式出现三行时
    r1 = np.repeat(arr,3,axis=0) #使用.repeat(阵列名称,复制的次数,依照什麽维度) 
    print(r1)
    '''
    [[1 2 3 4]
     [1 2 3 4]
     [1 2 3 4]]'''
    
  3. 取得阵列的资讯

    1. 取得维度资讯: .ndim

      #a跟b在太上面了,再来回顾一下他们两个长怎样
      print("a:", a)
      print("b:", b)
      '''
      a: [1 2 3]
      b: [[5 3 6 7 1]
       [1 2 3 4 5]]
      '''
      #取得维度资讯
      print(a.ndim) 
      print(b.ndim)
      '''
       1
      2'''
      
    2. 形状资讯: .shape(_)

      改变形状: .reshape(_)

      • 一维阵列
      # 使用.shape
      print(a.shape) #因为a只有一维度(一个row)
      print(b.shape) #因为b是2*5的二维矩阵
      '''
       (3,)
       (2, 5)
      '''
      
      • 二维阵列

        # 先建立原本的
        before = np.array([[1,2,3,4],[5,6,7,8]])
        print(before)
        '''
        [[1 2 3 4]
         [5 6 7 8]]'''
        
        # 改变成任意形状
        after = before.reshape((8,1)) #改成8*1的矩阵
        print(after)
        '''
        [[1]
         [2]
         [3]
         [4]
         [5]
         [6]
         [7]
         [8]]'''
        
        #想要看到4*2的矩阵
        after = after.reshape((4,2))#改成4*2的矩阵
        print(after)
        '''
        [[1 2]
         [3 4]
         [5 6]
         [7 8]]'''
        
    3. 取得元素的资料型别: .dtype

      若要更改资料型别: .astype(np.欲转换型态)

      # 取得资料型态
      print(a.dtype)
      #int64
      
      #改变资料型态
      change = a.astype(np.float64) #指定他要变成浮点数的型态
      print(a.dtype) #印出来检查看看
      #float64
      

结语

今天练习的是基础的NumPy,因为主要的目的是建立对处理多维度阵列的概念
youtube影片讲解得很清楚,语速很刚好
笔记内容大部分是来自上课讲解的内容,还有加上一点之前上课学过的概念
自己再带一点数字进去练习

所以今天只在文章里讲解了这个套件的特色
跟练习创建阵列、取得阵列资讯
之後会继续练习其他常用的基础语法,还有对处理多维度阵列的能力

因为之後当面对到庞大的数据时,都必须使用到NumPy


<<:  线上黑客松!

>>:  [Python 爬虫这样学,一定是大拇指拉!] DAY09 - TCP / UDP

大脑如何精准学习 (1) 注意力

今天,产生了一个想法、或说好奇:是否有什麽原因,让原本在学习表现上有开放可能性的人,变成不主动积极了...

Day19 - AVL tree

大家好,我是长风青云。今天是铁人赛第十九天。 这是今天的影片。 来说个题外话。 今天下午我在看云端课...

第二十三天:在 TeamCity 上产生覆盖率报告

昨天介绍了测试覆盖率的概念,也在 IntelliJ IDEA 里将 ShoppingCart 类别的...

[Day15] Flutter - 大海捞针不是办法 ( Dartz )

前言 Hi, 我是鱼板伯爵在原本的try&catch中我们可以截取大部分的错误,但是这仅能告诉我程序...