7 4 3 3 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 python articles

[Python] 均方根誤差(root mean square deviation, RMSE)

說明 「均方根誤差」(RMSE) 就是殘差 (預測誤差) 的標準差。 殘差是離迴歸線資料點有多遠的測量值;RMSE 則是這些殘差值有多散開的測量值。 換句話說,它會告訴您資料在最佳預測線附近的集中情況 公式 $RMSE = \sqrt{\displaystyle\sum_{ i = 1 }^{ n } \frac{(\hat{y}_i - y_i)^2}{n}}$ $\hat{y}_i$ : 預測值 $y_i$ : 觀測值 n : 觀測值的個數 範例 >>> from sklearn.metrics import mean_squared_error >>> actual = [2, 3, 5, 5, 9] >>> calculated = [3, 3, 8, 7, 6] >>> rmse = mean_squared_error(actual, calculated, squared=False) >>> print("Root Mean Square deviation : " + str(rmse)) Root Mean Square deviation : 2.……

Continue reading

[Python] 解決 python 下載資料時憑證錯誤問題

狀況 今天在 Python 寫 AI 用到 langchain 模組, 使用前需要下載文字轉向量 model, 結果遇到 certificate verify ailed: self signed certificate in certificate chain (_ssl.c:997) 錯誤, 這是因為公司有網路設備在做 SSL 憑證置換的關係卡到, 解決方法除了不檢查憑證,也可以把公司的憑證匯入 certifi, 下載時就會信任公司的憑證. 這樣做如果連到不安全的網址仍然檢查, 程式會停止存取, 所以我認為是相對安全的. 後來測試以前用 requests 或 urllib module 的錯誤也不會卡住,感覺更方便. 做法如下 首先確認 certifi 安裝的路徑 先打開預存取的網站,這邊用 yahoo 網站為例, 然後選詳細資料的最上面階層的憑證,把憑證給匯出 下面第一圖是馬賽克後的憑證內容, 將憑證的內容貼到 cacert.pem 內 python 程式就可以正常下載檔案 ……

Continue reading

[Python] Pandas DataFrame 如何顯示所有欄位

在使用 Python 的 Pandas DataFrame,如果要列出結果,顯示的行數只有 20 行。 import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(0, 26,size=(10, 26)), columns=list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')) df 修改參數為 None 之後,就會顯示所有的行數,不再只顯示 20 行。 pd.set_option("display.max.columns", None) df ……

Continue reading

練習 Python 100 Days(5)

練習 Python 100 Days 前幾天的比較簡單, 就從第五天的練習題開始 水仙花數(Narcissistic number) 用來描述一個 N 位非負數, 其中每個數字的 N 次方和等於該數字本身 代碼: for num in range(1000): squares = len(str(num)) count = 0 count = sum([int(x)**squares for x in str(num)]) if num == count: print(f"The Narcissistic number is : {count}" ) 完美數(Perfect number) 用來描述一個數字, 除了自己以外的所有真因子的總和, 等於它自己, 就是完美數 代碼: for num in range(100): list = [x for x in range(1, num) if num%x==0] if sum != [] and sum(list) == num: print(num) 參考: Python-100-Days - Day5……

Continue reading

實數轉換成整數

前言 ​ 使用 int() 函式做數值轉換, 會遇到小數點以下有值時, 會被無條件捨去的困擾, 從 stackoverflow 看到有兩種解法 >>> num1 = 1.0 >>> int(num1) 1 >>> num2 = 1.5 >>> int(num2) 1 解法一 ​ 使用 is_integer() 函式配合 if 判斷是否為整數, 是才做 int() 轉換 >>> (1.0).is_integer() True >>> (1).is_integer() ### 缺點是已經是整數的變數, 會跳錯 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'int' object has no attribute 'is_integer' >>> (1.5).is_integer() False >>> (0.9999999999 ).is_integer() False >>> data = [1.……

Continue reading

使用 Pandas groupby 和 agg 合併資料

前因 想要把下面 dataframe , 分類後合併在一起 order devie abbr 0 first memory m 1 second cpu c 2 third disk d 3 first cpu c 成為下面的 data format order devie abbr 0 first memory, cpu [m, c] 1 second cpu [c] 2 third disk [d] 先建立兩個 lambda 變數 ​ f1 的功用是將 dataframe 轉換成字串, f2 則將 dataframe 轉換成 list >>>f1 = lambda x: ", ".join(x.dropna()) >>>f1(data.abbr) 'm, c, d, c' >>>f2 = lambda x: [z for y in x for z in y] >>>f2(data.……

Continue reading