• Slides: 34

Python撰寫 具 官方版IDLE Anaconda Jupyter Notebook 範例檔案放置於：ftp: //163. 25. 117/nplu/Py. Practice

Num. Py統計函式 ü ü ü ü amin(a, axis=None) amax(a, axis=None) nanmin(a, axis=None) average(a, axis=None, weights=None) median(a, axis=None) mean(a, axis=None) std(a, axis=None) var(a, axis=None) nanmedian(a, axis=None) nanmean(a, axis=None) nanstd(a, axis=None) nanvar(a, axis=None) … >>> score = np. array([[80, 75, 88, 80, 78], [88, 86, 90, 95, 86], [92, 85, 92, 98, 90], [81, 88, 80, 82, 85], [75, 80, 78, 80, 70]]) # 算術平均 >>> np. mean(score, axis = 1) array([80. 2, 89. , 91. 4, 83. 2, 76. 6]) # 加權平均 >>> np. average(score, axis = 1, weights = [0. 5, 0. 2, 0. 1, 0. 1]) array([79. 6, 88. 3, 91. , 82. 8, 76. 3]) >>> np. median(score, axis = 1) # 中位數 array([80. , 88. , 92. , 82. , 78. ]) >>> np. std(score, axis = 1) # 標準差 array([4. 30813185, 3. 34664011, 4. 1761226 , 2. 92574777, 3. 77359245]) >>> np. var(score, axis = 1) # 變異數 array([18. 56, 11. 2 , 17. 44, 8. 56, 14. 24])

Num. Py隨機取樣函式 簡單亂數資料 ü ü ü ü ü rand(d 0, d 1, …, dn) randn(d 0, d 1, …, dn) randint(low[, high, size, dtype]) random_integers(low[, high, size]) random_sample([size]) random([size]) ranf([size]) sample([size]) choice(a[, size, replace, p]) 變更順序 ü ü shuffle(x) permutation(x) 亂數�生器 ü seed(seed=None) 分佈 (distribution) ü ü ü ü beta(a, b[, size]) binomial(n, p[, size]) chisquare(df[, size]) dirichlet(alpha[, size]) exponential([scale, size]) f(dfnum, dfden[, size]) gamma(shape[, scale, size]) geometric(p[, size]) gumbel([loc, scale, size]) hypergeometric(ngood, nbad, nsample[, size]) laplace([loc, scale, size]) logistic([loc, scale, size]) lognormal([mean, sigma, size]) ……

Examples. py程式片段： import math import pandas as pd …. . # data = {'Time': ['2018/4/12 08: 45: 00', '2018/4/12 08: 50: 00 ', '2018/4/12 08: 55: 00'], # 'A. PV - Average': [49. 41580318, 62. 62195005, 62. 02720746], # 'B. OP - Average': [64. 12516777, 63. 6363677999999], # 'C. PV - Average': [56. 11305857, 54. 62703102, 55. 69705158]} # myframe = pd. Data. Frame(data, dtype="float 64" , index=[5, 6, 89]) # Read in dataframe csv. File = "C: /Users/NPLU/Desktop/Py. Practice/input. File. csv" myframe = pd. read_csv(csv. File) print("1. 匯入資料集：") print(myframe. head()) # input file name # LSM from matplotlib import pyplot as plt import numpy as np from sklearn. linear_model import Linear. Regression # x values x = np. array([[45], [50], [55]]) # y values y = np. array(myframe. iloc[: , 1]) 62. 02720746] # A. PV: [49. 41580318, 62. 62195005, n = x. shape[1] r = np. linalg. matrix_rank(x) U, sigma, VT = np. linalg. svd(x, full_matrices=False) myframe. index=[5, 6, 89] D_plus = np. diag(np. hstack([1/sigma[: r], np. zeros(n-r)])) print("n 2. 更動資料集index：") print(myframe. head()) V = VT. T # 標準差 print("n 3. 標準差：") print(myframe. std(axis=0)) # 均方根誤差 dsum = 0 for measured_value in myframe. iloc[: , 1]: dsum = dsum + (myframe. mean()[0] - measured_value)**2 MSE = dsum / 3 RMSE = math. sqrt(MSE) print("n 4. 均方根誤差：") print(RMSE) x_plus = V. dot(D_plus). dot(U. T) w = x_plus. dot(y) plt. scatter(x, y) plt. plot(x, w*x, c='red') print("n 11. LCM結果呈現於圖形視窗中. . . ") plt. show() print("n*** Examples. py執行完畢 ***") …… 完整檔案放置於：ftp: //163. 25. 117/nplu/Py. Practice