NumPy
Python: NumPy 使用亂數種子來重現亂數 …
random.seed() 使用方式 import numpy as np for _ in range(5): np.random.seed(10) a = np.random.random((10,)) print(a) Output [0.77132064 0.02075195 0.63364823 0.74880388 0.49850701 0.22479665 0.19806286 0.76053071 0.16911084 0.08833981] [0.77132064 0.02075195 0.63364823 0.74880388 0.49850701 0.22479665 …
Python: NumPy 的二進制檔案 .npy
np.save numpy.save(file, arr, allow_pickle=True, fix_imports=True) file:file, str, or pathlib.Path 儲存檔名或路徑。 arr:array_like 要保存的NumPy陣列變數資料。 寫入 .npy 檔 with open("test.npy", "wb") as f: np.save(f, np.array([1, 2])) np.save(f, np.array([1, 3])) 讀取 .npy 檔 with open("test.npy", …
Python:NumPy 反轉 (Reverse)
最直接反轉的方式是用 ::-1 a = np.arange(10) b = a[::-1] 列印陣列 a print(a) Output array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 列印陣列 b print(b) Output array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
Python:設定 numpy 顯示矩陣的方式
前言 有時在使用 print() 顯示矩陣時,會發現螢幕上並不會全部顯示出來,甚至使用 write() 來儲存變數也會有一樣的問題,這時候就要利用 set_printoptions() 來設定 numpy 顯示的方式。 設定方式 set_printoptions 是全域設定。設完之後,除非關掉重開 Python,否則設定會一直持續存在。 numpy.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, …