iT邦幫忙

2021 iThome 鐵人賽

DAY 12
1
AI & Data

從資料庫到資料分析視覺化系列 第 12

{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


上一篇
{DAY11} SQL查詢語法3
下一篇
{DAY 13} NumPy 學習筆記(中)
系列文
從資料庫到資料分析視覺化30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言