Annie-python
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • 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 Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    --- title: 14.Python 亂數與統計模組 By 彭彭 tags: 學習, 紀錄 --- # 14.Python 亂數與統計模組 By 彭彭 {%youtube -xwCu6PN1jU %} ## 亂數、統計模組 ## 內建模組: 學習 ramdom模組 和statistics模組 1. ramdom模組 2. statistics模組 ## 亂數模組 * 載入模組 import random * 隨機選取 import random random.choice([0,1,5,8]) 從列表中隨機選取1個資料 random.sample([0,1,5,8],2) 從列表中隨機選取2個資料 * 隨機調換順序 import random data=[0,1,5,8] random.shuffle(data) print(data) * 隨機亂數 (出現機率相同) import random ramdom. random() 取得0.0~1.0之間的隨機亂數 random. uniform(0.0,1.0) (指定0.0~1.0之間的隨機亂數) * 常態分配亂數 import random 取得平均數100,標準差10的 常態分配亂數 random. normalvariate(100,10) (第一個參數指定平均數,第二個參數指定標準差) ![](https://i.imgur.com/3v7MQiR.png) 以此圖為例,常態分配會在90-110之間 只有少部分會在左右旁邊少數 ## 統計模組 * 載入模組 import statistics * 計算平均數 mean import statistics statistics.mean([1,4,6,9]) 計算列表中數字的平均數 * 計算中位數 median import statistics statistics. median([1,4,6,9]) * 計算標準差 stdev import statistics statistics.stdev([1,4,6,9]) 計算資料散佈的狀況 ## Practice ### 隨機模組 #### 隨機選取 * choice 列表中隨機選一個數字 import random data=random.choice([1,5,6,10,20]) print(data) >1 隨機印出列表中的一個數字 * sample 列表中隨機挑多個數字 import random data=random.sample([1,5,6,10,20],3) print(data) >[6,1,5] #### 隨機調換順序(就是洗牌的概念) * shuffle 列表中的數字就地調換後顯示 data=[1,5,8,20] random.shuffle(data) print(data) >[1,20,8,5] #### 隨機取得亂數 * random data=random.random() print(data) >0.55 random模組中的random()表示0.0-1.0之間的隨機亂數 * uniform(x, y) data=random.uniform(60,100) print(data) >62 兩數之間的隨機亂數 #### 取得 常態分配亂數 * normalvariate 常態分配 平均數100 標準差10 得到的資料多數在90-110之間 常態分配的效果 data=random.normalvariate(100,10) print(data) >98 * normalvariate 常態分配 平均數0 標準差5 得到的資料"多數"在-5~5之間 常態分配的效果 data=random.normalvariate(0,5) print(data) >1.0 ### 統計模組 * 平均數mean import statistics as stat 用別名 data=stat.mean([1,4,5,8]) print(data) >4.5 (1,4,5,8的平均數是4.5) * 中位數median import statistics as stat 用別名 data=stat.median([1,2,4,5,8,100]) print(data) > 4 中位數 會把極端數字排除 --- 同樣數字用平均數算 得出17 import statistics as stat 用別名 data=stat.mean([1,2,4,5,8,100]) print(data) > 17 --- * 標準差 Stdev import statistics as stat 用別名 data=stat.stdev([1,2,4,5,8,100]) print(data) >36.41820 代表資料散佈狀況 說明資料的差距是不是很大 這個例子因為有100導致之間差距大下面將100改成10以後 標準差變小 import statistics as stat 用別名 data=stat.stdev([1,2,4,5,8,10]) print(data) >3.25 ## End

    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