YuKai0928
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- ###### tags: `資訊之芽` --- # Numpy & Matplotlib --- ## 大綱 - Numpy 簡單介紹 - 小技巧 - 更多介紹 - View/Reshape - 最後小撇步 ---- ## 簡單介紹 Numpy 內置array及一堆好用的小工具 包含向量/矩陣運算 也涵蓋統計,隨機,傅立葉轉換(?) ---- ## 簡單介紹 Numpy的array和之前的Tuple,List非常像 之前的各種資料型態固然方便,功能很多 但越多功能也就代表著速度越慢... ---- ## 簡單介紹 ![](https://i.imgur.com/skGAHTl.jpg) ---- ## 簡單介紹 安裝 ``` pip install numpy ``` 匯入 ```python= import numpy arr = numpy.array([1,2,3,4,5]) ``` ---- ## 簡單介紹 試試看! ```python= import numpy arr = numpy.array([1,2,3,4,5]) # 創造一個array print(arr) print(type(arr)) print(arr[2]) # 取值方式一樣 print(arr[0:2]) # 也可以Slice ``` ---- ## 簡單介紹 題外話 利用import ... as ... 來~~偷懶~~ 方便寫扣 ```python= import numpy as np arr = np.array([1,2,3,4,5]) ``` 註:簡報之後部分都不會再寫出import... ---- ## 簡單介紹 所以...現在是 QA Time! --- ## 小技巧 ```python= arr = np.arange(2,16,3) # [2,5,8,11,14] # 和range很像 arr = np.zeros(4) # [0, 0, 0, 0] arr = np.ones(5) # [1, 1, 1, 1, 1] arr = np.linspace(0, 10, num=5) # [l,r]範圍取num個點 # 0, 2.5, 5, 7.5, 10 ``` ---- ## 小技巧 快速生出大array的方法 (2) ```python= arr = np.empty(2) # [酷東西, 酷東西] arr = np.random.rand(5) # 長度為5 範圍為0~1 arr = np.random.randint(0,2,(4,3)) # 尺寸4*3 範圍為[0,2) ``` 酷東西:undefined ---- ## 小技巧 所以...現在是 QA Time! --- ## 更多介紹 多維度Array 多維度?回想之前的巢狀List ~~List俄羅斯套娃~~ ``` 這是一個二維array 尺寸為3 (列 row) * 3 (行 column) [ [1,4,7], [2,5,8], [3,6,9] ] 第一列為 [1,4,7] 第一行為 [1,2,3] ``` ---- ## 更多介紹 多維度array 基本上概念完全一樣 ```python= import numpy as np arr_0 = np.array(1) # 這是0維的 arr_1 = np.array([1,2,3]) arr_2 = np.array([[1,2,3],[4,5,6],[7,8,9]]) # 這是2維的 ``` ---- ## 更多介紹 新東西 檢查維度用 維度數量也可以說是Rank(秩) ```python= arr_2 = np.array([[1,2,3],[4,5,6],[7,8,9]]) print(arr_2.ndim) # 二維array/Rank = 2的array # output: 2 ``` ---- ## 更多介紹 新東西 創造維度! ```python= arr_2 = np.array([[1,2,3],[4,5,6],[7,8,9]]) arr_3 = arr_2[np.newaxis, :] # 多一個維度 print(arr_3.ndim) # output: 3 print(arr_3[0]) print(arr_2) # 可以發現以上兩個的內容完全一樣 ``` ---- ## 更多介紹 除了可以看Rank 也可以看詳細的尺寸 ```python= arr_2 = np.array([[1,2,3],[4,5,6],[7,8,9]]) arr_3 = arr_2[np.newaxis, :] # 多一個維度 print(arr_3.shape) # (1, 3, 3) ``` ---- ## 更多介紹 所以...現在是 QA Time! --- ## View 同一個array的資料,可以利用view來改變看待資料的方法 可以用view然後不指定內容來做出一個類似捷徑的資料 ```python= arr = np.array([1, 2, 3, 4, 5]) x = arr.view() print(x) arr[0] = 999 print(x) # 發現index = 0的元素被改成999了 z只是一個看待arr的方法 ``` ---- ## View 如果把arr的資料當作浮點數來看待,則印出來會是像x那樣 ```python= arr = np.array([1, 2, 3, 4, 5], dtype = np.int64) x = arr.view(float) # 用解讀float的方式解讀int的資料 print(x) # float/int都一樣佔8 Byte 但float有部分是紀錄小數點後的數值 ``` ---- ## 確認array中的資料型態 ```python= arr = np.array([1,2.5,3,4,5]) arr_2 = np.array([1,2,3,4,5]) print(arr.dtype) # float64 print(arr_2.dtype) # int64 ``` 更多補充:https://numpy.org/doc/stable/user/basics.types.html ---- ## View vs Copy Copy就是把整個array複製一份出來 但也要注意shallow copy的問題! ```python= arr = np.array([1, 2, 3, 4, 5]) x = arr.view() y = arr.copy() arr[0] = 999 print(x) print(y) # 不會被改到999 ``` ---- ## QA 時間 到目前還好ㄇ --- ## Reshape 跟捏泥巴一樣簡單 只要元素數量正確,就能任意改變陣列的尺寸大小! ```python= x = np.arange(6) # 0,1,2,3,4,5 y = x.reshape(2,3) print(y) # 輸出尺寸為2*3的矩陣 #[[0 1 2] # [3 4 5]] ``` ---- ## Reshape 真的只要元素數量正確就好 ```python= x = np.arange(6) # 0,1,2,3,4,5 w = x.reshape(1,1,1,1,1,6) # 維度控制大師(X) print(w) ``` ---- ## Reshape 不會除法也能當維度控制大師! ```python= arr_2 = np.arange(1236) # 有1236個元素 z = arr_2.view() z.shape = (2,103,-1) # 不知道某個維度方向長度時 可以用-1來代替 np會幫忙算好 print(z.shape) # (2,103,6) ``` ---- ## View補充! View能換個視角看資料 那能不能換個尺寸看資料?可以! ```python= arr = np.array([1,2,3,4,5,6]) x = arr.view() x.shape = (2,3) # 換尺寸 print(x) ``` ---- ## QA Time! 捏形狀還熟悉嗎 --- ## 最後小撇步! 這是最後一張了 array也內建直接運算 最大/小/平均/標準差的函式 學到賺到! ```python= a = np.arange(5,2391,19).reshape(2,3,7,3) # 有126個元素 print(a.min()) # 所有元素最小值 print(a.max()) # 所有元素最大值 print(a.sum()) # 所有元素之和 print(np.std(a)) # 標準差 print(np.mean(a)) # 平均 ``` --- ## Numpy End! ---- ## Matplotlib 剛剛已經會整理資料了 現在來把資料畫出來! ---- ## 大綱 - 安裝 - 安裝之外的 --- ## 安裝 ``` pip install -U matplotlib ``` 或是參考[這裡](https://matplotlib.org/stable/users/installing/index.html) 不要按[這裡](https://www.youtube.com/watch?v=-ePcMwbgl14) ---- ## 匯入 安裝之後 要使用前請記得匯入 ```python= from matplotlib import pyplot as plt import matplotlib.pyplot as plt ``` 以上兩種方式都可以 ---- ## QA Time 到這裡還好ㄇ? --- ## 基本語法 畫圖分成三步驟 - 給資料及格式 - 設定label - show ---- ## 簡單示範 ```python= import matplotlib.pyplot as plt x = [10,20,30,40] y = [20,10,25,50] # 點的座標分別會是(10,20), (20,10), (30,25), (40,50) ``` ---- ## 簡單示範 ```python= plt.plot(x, y,"rs-") # 將資料丟給plt # r 紅色, s square, - 直線 plt.title("Example") # 設定標題等 plt.ylabel("y-axis") plt.xlabel("x-axis") ``` ---- ## 簡單示範 最後一步,[Show!](https://youtu.be/nrZxwPwmgrw?t=76) ```python= plt.show() ``` (可以搭配音效) ---- ## QA Time 大家有成功show出來了ㄇ --- ## 其他格式 顏色 可用kwarg ``` #RGB, 0~1 color=(255/255,100/255,100/255) ``` ---- ## 其他格式 點樣式 - \+ - \. 小圓點 - o 圓點 - s 小方格 - D 稜形/鑽石 - H 16邊形 ---- ## 其他格式 線條樣式 - : 虛線 - \- 直線 ---- ## 小練習 畫出一個正方形 邊長為10 藍色 虛線 鑽石頂點,其中兩個頂點在(0,0), (10,10) ---- ## 小練習解答 ```python= import matplotlib.pyplot as plt x = [0,10,10,0,0] y = [0,0,10,10,0] plt.plot(x, y,"bd:") # 將資料畫出來 plt.title("Blue square") # 設定標題等 plt.show() # 將圖表顯示出來 ``` 有點醜但還是正方形的吧 --- ## 設定比例尺 利用axis,給入有四個數字的list 就會把比例尺設定在對應範圍中 ```python= plt.plot([0,1,2,3,4],[0,1,8,27,64]) plt.axis([0, 5, 0, 70]) #[x始 x終 y始 y終] plt.show() ``` ![](https://i.imgur.com/QOAwCUc.png) ---- ## 不同的圖 想畫出數學課本上滑滑的圖嗎 教你一招!不用一個一個座標慢慢打 回想numpy教的linspace ```python= x = np.linspace(0,5,21)# [0,5]之中取21個點 plt.plot(x,x**2) # x軸為0~5的數字 y軸為0~5的數字平方後的結果 plt.show() #生出0~5之間 y = x^2的圖 ``` ---- ## 不同的圖 直方圖 Histogram y值為提及x的次數 ```python= plt.hist([2,2,6,8]) ``` ![](https://i.imgur.com/WuDNI8T.png) ---- ## numpy random 一個一個打有點累,我們可以借助np.random來產出一堆資料 這裡隨便挑一個normal distribution做示範 ```python= import numpy as np x = np.random.normal(0,1,1000) # 平均為0 標準差為1的常態分佈試驗1000筆 plt.hist(x) plt.show() ``` 有興趣也能print(x)看看裡面有什麼 ---- ## 不同的圖 點分佈圖 scatter ``` #點座標分別為(5,10) (2,5) (9,8) (4,4) (7,2) plt.scatter([5, 2, 9, 4, 7], [10, 5, 8, 4, 2]) plt.show() ``` ---- ## 不同的圖 長條圖 bar ```python= # 一樣給x y座標 plt.bar([5, 2, 9, 4, 7], [10, 5, 8, 4, 2]) plt.show() ``` ---- ## QA Time 以上都有一個個試過了ㄇ ___ ## 延伸 還有很多沒講的 - 3D圖形 [mplot3d](https://matplotlib.org/3.5.0/tutorials/toolkits/mplot3d.html) - 4D圖形 - 時空穿越 - 烏托邦社會架構哲學問題 - 正確地騎乘魔法掃帚 - [選擇適合居住的維度指南](https://www.instagram.com/p/CbhAWZRP39H/?utm_medium=copy_link&fbclid=IwAR0h1zH4e4EbehIDmaXdJoYqcsspYaDAxluI2-BsvOp8vtJpd5ng7zAnwcI) - 加值悠遊卡不可不知的10大秘訣

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully