# 第十二天 Modules模組 > [color=#40f1ef][name=LHB阿好伯, 2020/12/20][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `Python_30` `R & python` ![](https://i.imgur.com/NZ559GW.jpg) [TOC] 模組是包含可以包含在應用程序中的一組程式碼或一組功能的文件 模組可以是包含單個變量或函數的文件 [Python標準函式庫 (Standard library)](https://docs.python.org/3/py-modindex.html) 內含許多模組許多模組方便使用 除此之外可以使用第三方的套件 也可以自己造輪子,寫自己的套件或模組 # 創建一個模組 要創建模組我們將程式碼編寫為python的指令將其另存副檔名為.py的文件 ```python= # 儲存檔案為mymodule.py def generate_full_name(firstname, lastname): space = ' ' fullname = firstname + space + lastname return fullname ``` ```python= # Python code import mymodule print(mymodule.generate_full_name('Asabeneh', 'Yetayeh')) ``` :::success Asabeneh Yetayeh ::: ## 載入模組並重新命名 在載入模組時我們可以重命名模組的名稱 避免重複名稱或是名稱過長的問題 ```python= # Python code from mymodule import generate_full_name as fullname print(fullname('Asabeneh', 'Yetayeh')) ``` :::success Asabeneh Yetayeh ::: # 載入內置模組 通過使用import來導入模組 一些常見的內置模組:`math`, `datetime`, `os`, `sys`, `random`, `statistics`, `collections`, `json`, `re` ## 操作檔案與路徑-os 使用python os模組可以自動執行許多操作系統任務 OS提供了用於創建,更改當前工作目錄以及刪除目錄(文件夾) 獲取其內容、更改和標識當前目錄的功能 以下節錄一些常用的程式碼 ```python= # Python code # 顯示所有檔名 FilePath = 'C:\\FolderName' def find_dir(path): # 顯示指定路徑下的所有檔案及資料夾名稱 for fd in os.listdir(path): full_path=os.path.join(path,fd) if os.path.isdir(full_path): print('Folder name:',full_path) find_dir(full_path) else: print('data name:',full_path) find_dir(FilePath) ## 顯示所有資料夾與檔名 path='C:\\FolderName' def find_dir(dir): fds=os.listdir(dir) for fd in fds: full_path=os.path.join(dir,fd) if os.path.isdir(full_path): print('Folder name:',full_path) find_dir(full_path) else: print('data name:',full_path) find_dir(path) ## 列出指定結尾檔案 import os path='C:\\python' for root,dirs,files in os.walk(path): for file in files: if file.endswith('.py'): #py為結尾 print(os.path.join(root,file)) # 更改文件檔名 - os.rename() os.rename("原檔名", "新檔名") # 創建資料夾 - os.mkdir() FN='C:\\FolderName' if not os.path.exists(FN): #檢查目錄是否存在,若無則建立資料夾 os.mkdir(FN) else: print(FN + 'Already established !') # 刪除資料夾 - os.rmdir() os.rmdir(FN) # 更改當前目錄 - os.chdir() os.chdir('path') # 獲取當前的工作目錄 - os.getcwd() os.getcwd() ``` R語言也有幾個與路徑有關的函數順便介紹 ```r= #取得工作目錄 getwd() #設定工作目錄 須將微軟預設的"\"改成 "/" setwd("C:/Users/...") #取得路徑下特定檔案路徑 #其中pattern可以使用正規表達式來搜尋特定檔案 FilePath<-"C:/Users/gtgrt/Documents/" files <- list.files(FilePath,pattern=".*csv$",all.files = TRUE,full.names = TRUE) #以視窗選取來替代路徑,搭配輸入函數 file.choose() #以視窗選取來替代路徑,可以選取多個檔案 choose.files() ``` ## 統計模塊Statistics Module >[官方函數詳細說明](https://docs.python.org/zh-tw/3/library/statistics.html) > |函數|功能| |:---:|:---:| | [mean()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.mean "統計平均值") | 數據的算術平均(平均值)。 | | [fmean()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.fmean "統計數據") | 快速的,浮點算數平均數。 | | [geometric_mean()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.geometric_mean "statistics.geometric_mean") | 數據的幾何平均數 | | [harmonic_mean()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.harmonic_mean "statistics.harmonic_mean") | 數據的調和均值 | | [median()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.median "統計中位數") | 數據的中位數(中間值)。 | | [median_low()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.median_low "statistics.median_low") | 數據中較小的中位數。 | | [median_high()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.median_high "statistics.median_high") | 數據中較大的中位數。 | | [median_grouped()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.median_grouped "statistics.median_grouped") | 分組數據的中位數或50%處。 | | [mode()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.mode "統計模式") | 離散的或標稱的數據的單個眾數(出現最多的值)。 | | [multimode()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.multimode "統計多模") | 離散的或標稱的數據的眾數列表(出現最多的值)。 | | [quantiles()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.quantiles "統計分位數") | 將數據以相等的概率分為多個間隔。 | | [pstdev()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.pstdev "statistics.pstdev") | 數據的母體標準差 | | [pvariance()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.pvariance "統計方差") | 數據的母體變異數 | | [stdev()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.stdev "statistics.stdev") | 數據的樣本標準差 | | [variance()](https://docs.python.org/zh-tw/3/library/statistics.html#statistics.variance "統計方差") | 數據的樣本變異數 | ```python= #Python Code from statistics import * # 導入所有統計模組函數 ages = [20,20,24,24,25,22,26,20,23,22,26] print(mean(ages)) print(median(ages)) print(mode(ages)) print(stdev(ages)) ``` :::success 22.90909090909091 23 20 2.3001976199685736 ::: R幾乎所有的統計功能都有相應的內建函數或是套件 這邊也不可能一一列出來 需要的可以自行搜尋 下面陳列幾個上面範例對應的函數 ```r= #R Code ages <- c(20,20,24,24,25,22,26,20,23,22,26) print(mean(ages)) print(median(ages)) print(attributes(which.max(table(ages)))$names) #取得眾數 #其他方式print(as.numeric(names(table(ages)))[which.max(table(ages))] ) print(sd(ages)) ``` :::success [1] 22.90909 [1] 20 [1] "20" [1] 2.300198 ::: >[眾數參考寫法](https://www.cyclismo.org/tutorial/R/types.html#tables) >[自訂函數](https://www.1ju.org/r/r-mean-median-mode) ```r= getmode <- function(v) { uniqv <- unique(v) uniqv[which.max(tabulate(match(v, uniqv)))] } ``` # 數學模塊 math | math 方法 |  說明 | |:---:|:---:| |  sqrt(x) |  傳回 x 的平方根 | |  pow(x, y) |  傳回 x 的 y 次方 | |  exp(x) |  傳回 x 的自然指數 | |  expm1(x) |  傳回 x 的自然指數-1 (在 x 接近 0 時仍有精確值) | |  log(x [, b]) |  傳回 x 以 b 為基底的對數 (預設 b=e 自然對數) | |  log10(x) |  傳回 x 的常用對數 (以 10 為底數) | |  degrees(x) |  傳回弧度 x 的角度 (degree) | |  radians(x) |  傳回角度 x 的弧度 (radian) | |  dist(p, q) |  傳回兩個座標點 p, q 的歐幾里得距離 (畢式定理斜邊) | |  hypot(coor) |  傳回座標序列 coor 的歐幾里得距離 | |  sin(x) |  傳回 x 的正弦值 | |  cos(x) |  傳回 x 的餘弦值 | |  tan(x) |  傳回 x 的正切值 | |  asin(x) |  傳回 x 的反正弦值 (sin 的反函數) | |  acos(x) |  傳回 x 的反餘弦值 (cos 的反函數) | |  atan(x) |  傳回 x 的反正切值 (tan 的反函數) | |  atan2(y, x) |  傳回 y/x 的反正切值 (tan 的反函數)=atan(y/x) | |  sinh(x) |  傳回 x 的雙曲正弦值 | |  cosh(x) |  傳回 x 的雙曲餘弦值 | |  tanh(x) |  傳回 x 的雙曲正切值 | |  asinh(x) |  傳回 x 的反雙曲正弦值=log(x+sqrt(x**2+1)) | |  acosh(x) |  傳回 x 的反雙曲餘弦值=log(x+sqrt(x**2-1)) | |  atanh(x) |  傳回 x 的反雙曲正切值=1/2*log((1+x)/(1-x)) | |  fabs(x) |  傳回 x 的絕對值 (或稱模數, modulus) | |  floor(x) |  傳回浮點數 x 的向下取整數 (即小於 x 之最大整數) | |  ceil(x) |  傳回浮點數 x 的向上取整數 (即大於 x 之最小整數) | |  trunc(x) |  傳回浮點數 x 的整數部分 (捨去小數) | |  modf(x) |  傳回浮點數 x 的 (小數, 整數) 元組 | |  factorial(x) |  傳回 x 階乘 (x!, x=整數) | |  gcd(x, y) |  傳回整數 x, y 之最大公因數 | |  comb(n, k) |  傳回 n 取 k 的組合數 (不依序不重複) | |  perm(n, k) |  傳回 n 取 k 的組合數 (依序不重複) | |  modf(x, y) |  傳回 x/y 之精確餘數 (浮點數 float) | |  fsum(iter) |  傳回可迭代數值 iter 之精確總和 | |  isclose(x, y) |  若 a, b 值很接近傳回 True (預設差小於 1e-9) | |  isfinite(x) |  若 x 不是 nan 或 inf 傳回 True, 否則 False | |  isnan(x) |  若 x 為 nan 傳回 True, 否則 False | |  isinf(x) |  若 x 為 inf 傳回 True, 否則 False | >[參考資料](http://yhhuang1966.blogspot.com/2019/10/python-math-cmath.html) # 字串模組 String ## 常用函數 >[string模組](https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/9971/) | 函數 | 功能描述 | |:---:|:---:| | str.capitalize() | 把字串的首字母大寫 | | str.center(width) | 將原字串用空格填充成一個長度為width的字串,原字串內容居中 | | str.count(s) | 返回字串s在str中出現的次數 | | str.decode(encoding='UTF-8',errors='strict') | 以指定編碼格式解碼字串 | | str.encode(encoding='UTF-8',errors='strict') | 以指定編碼格式編碼字串 | | str.endswith(s) | 判斷字串str是否以字串s結尾 | | str.find(s) | 返回字串s在字串str中的位置索引,沒有則返回-1 | | str.index(s) | 和find()方法一樣,但是如果s不存在於str中則會丟擲異常 | | str.isalnum() | 如果str至少有一個字元並且都是字母或數字則返回True,否則返回False | | str.isalpha() | 如果str至少有一個字元並且都是字母則返回True,否則返回False | | str.isdigit() | 如果str只包含數字則返回 True 否則返回 False | | str.islower() | 如果str存在區分大小寫的字元,並且都是小寫則返回True 否則返回False | | str.isspace() | 如果str中只包含空格,則返回 True,否則返回 False | | str.istitle() | 如果str是標題化的(單詞首字母大寫)則返回True,否則返回False | | str.isupper() | 如果str存在區分大小寫的字元,並且都是大寫則返回True 否則返回False | | str.ljust(width) | 返回一個原字串左對齊的並使用空格填充至長度width的新字串 | | str.lower() | 轉換str中所有大寫字元為小寫 | | str.lstrip() | 去掉str左邊的不可見字元 | | str.partition(s) | 用s將str切分成三個值 | | str.replace(a, b) | 將字串str中的a替換成b | | str.rfind(s) | 類似於 find()函式,不過是從右邊開始查詢 | | str.rindex(s) | 類似於 index(),不過是從右邊開始 | | str.rjust(width) | 返回一個原字串右對齊的並使用空格填充至長度width的新字串 | | str.rpartition(s) | 類似於 partition()函式,不過是從右邊開始查詢 | | str.rstrip() | 去掉str右邊的不可見字元 | | str.split(s) | 以s為分隔符切片str | | str.splitlines() | 按照行分隔,返回一個包含各行作為元素的列表 | | str.startswith(s) | 檢查字串str是否是以s開頭,是則返回True,否則返回False | | str.strip() | 等於同時執行rstrip()和lstrip() | | str.title() | 返回"標題化"的str,所有單詞都是以大寫開始,其餘字母均為小寫 | | str.upper() | 返回str所有字元為大寫的字串 | | str.zfill(width) | 返回長度為 width 的字串,原字串str右對齊,前面填充0 | ## 字串常數 | 常數 | 含義 | |:---:|:---:| | string.ascii_lowercase | 小寫字母'abcdefghijklmnopqrstuvwxyz' | | string.ascii_uppercase | 大寫的字母'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | | string.ascii_letters | ascii_lowercase和ascii_uppercase常量的連線串 | | string.digits | 數字0到9的字串:'0123456789' | | string.hexdigits | 字串'0123456789abcdefABCDEF' | | string.letters | 字串'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | | string.lowercase | 小寫字母的字串'abcdefghijklmnopqrstuvwxyz' | | string.octdigits | 字串'01234567' | | string.punctuation | 所有標點字元 | | string.printable | 可列印的字元的字串。包含數字、字母、標點符號和空格 | | string.uppercase | 大學字母的字串'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | | string.whitespace | 空白字元 '\t\n\x0b\x0c\r ' | ```python= # Python code import string print(string.ascii_letters) # abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ print(string.digits) # 0123456789 print(string.punctuation) # !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ ``` R語言也內建了幾個常數可以使用 ```r= # R code LETTERS #英文字母(大寫) letters #英文字母(小寫) month.abb # 英文月份的的縮寫 month.name #月份名稱 pi #圓周率 ``` ## 亂數模組 Random random模組具有很多功能 讓我們載入random模組 random模組可以快速生成0到0.9999之間的隨機數... ```python= # Python code from random import random, randint print(random()) #不帶任何參數;返回0到0.9999之間的數值 print(randint(5, 20)) #返回參數設定之間的隨機整數 ``` 而R中則也有內建的公式可以使用 例如dnorm,pnorm,qnorm和rnorm,其中,dnorm表示密度函數,pnorm表示分佈函數,qnorm表示分位數函數,rnorm表示生成隨機數的函數 sample(a:b,n,replace) 為抽樣函數可以用於生成特定範圍的隨機數 replace=T時是在[a,b]內有放回的隨機抽出n個整數 ```r= # R Code runif(n=1, min=0, max=1) #取得一組平均分布數據 sample(5:20,1) ``` ### 設定亂數種子 若在程序中使用到亂數時會發生樣本不夠大時 下次執行的結果可能會有差異 這時候就可以設定種子 使得下次可以得到一樣的結果 記得每次使用亂數前都要執行一次種子 ```r= set.seed(1234567) #控制隨機數,用於重複得到相同隨機數 runif(n=1, min=0, max=1) ``` :::success [1] 0.5622608 ::: ```python= # Python code import random random.seed(1234567) #如果a被省略或為None,則使用當前系統時間或是設定為任一整數值 print(random.random()) #不帶任何參數;返回0到0.9999之間的數 ``` :::success 0.9631432476096223 ::: # 時間模組 time R語言相關時間函數與套件可以參考之前分享的文章 >[R語言_時間資料處理](https://hackmd.io/@LHB-0222/Rdate) ## 取得當前時間 在R中內建許多函數可以取得時間資訊 ```R= Sys.Date() #當前日期 lubridate::today() date() #當前系統日期和時間 Sys.time() #當前系統日期和時間 lubridate::now() Sys.timezone() #有關時區的信息將返回當前時區的名稱 ``` :::success [1] "2020-12-22" [1] "Tue Dec 22 13:02:33 2020" [1] "2020-12-22 13:02:33 CST" [1] "Asia/Taipei" ::: ```python= # Python code import time # 從 1970/1/1 00:00:00 至今的秒數 seconds = time.time() # 將秒數轉為本地時間 local_time = time.ctime(seconds) # 輸出結果 print("Time :", local_time) ``` 🌟全文可以至下方連結觀看或是補充 https://hackmd.io/@LHB-0222/30_Days_of_Python 全文分享至 https://www.facebook.com/LHB0222/ https://www.instagram.com/ahb0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) ![](https://i.imgur.com/47HlvGH.png)