發表文章

目前顯示的是 2026的文章

Tensorflow實作_阿拉伯手寫數字分類的實作_基本功常見操作

圖片
 本篇實作 使用python3.11 tensorflow2.20 ->這個裝好通常會自動把numpy2.4.6、tensorboard2.20、h5py 也裝好 matplotlib3.11.1 numpy2.4.6 tensorboard2.20 第一次於專案裝會要等大概10~15分鐘,會要等好一陣子。 Phase1程式碼-載入mnist資料集 嘗試透過將像素值內容非0的統一轉為1,觀察圖像張量資料。 import tensorflow as tf mnist = tf.keras.datasets.mnist # 匯入 MNIST 阿拉伯數字訓練資料 (x_train, y_train),(x_test, y_test) = mnist.load_data() # 訓練/測試資料的 X/y 維度 print ( "訓練資料X維度:{0}" .format(x_train.shape)) print ( "訓練資料Y維度:{0}" .format(y_train.shape)) print ( "測試資料X維度:{0}" .format(x_test.shape)) print ( "測試資料X維度:{0}" .format(y_test.shape)) # 訓練資料前10筆圖片的數字標準答案 print (y_train[: 10 ]) print (x_train[ 0 ]) #打印第一張訓練圖的像素值矩陣 data = x_train[ 0 ].copy() data[data> 0 ]= 1 #把非0的數字轉為1,顯示第1張圖片 print ( "==========================================" ) #把轉換後二維矩陣內容印出來,可以隱約看出數字為5 text_image=[] for i in range (data.shape[ 0 ]): text_image.append( '' .join( str (data[i]))) for i, row in enumerate (text_image): if i < len (...

Python物件導向語法筆記2_方法覆寫Override、方法多載Overloading、私有成員會前面多兩個底線

圖片
方法覆寫Override 程式1-原本子類別方法呼叫 class Parent : def myMethod ( self ): print ( "父類別方法" ) class Child (Parent): def myMethod ( self ): print ( "子類別方法" ) c = Child() c.myMethod() 程式2-方法覆寫Override super(子類別,self).同名方法 class Parent : def myMethod ( self ): print ( "父類別方法" ) class Child (Parent): def myMethod ( self ): print ( "子類別方法" ) super (Child, self ).myMethod() c = Child() c.myMethod() 方法多載Overloading 在python程式語言中,加減乘除四則運算子皆可用於預設變數、list的資料型別,但是如果是兩個自訂物件要進行運算,就勢必要重新定義這些運算子,不然就會發生不如預期的錯誤。 除了算術運算以外,比如要針對物件之間的比較運算、邏輯運算也都需要進行overloading。 固定覆寫寫法要留意,是以下這些制式化方法名稱 以下是一個簡單的二維點座標案例 TypeError: unsupported operand type(s) for +: 'Point' and 'Point' 另外像是要打印點座標直接print也會直接印出記憶體位置 程式1-As If class Point : def __init__ ( self ,x= 0 ,y= 0 ): self .x = x self .y = y p1 = Point( 4 , 3 ) p2 = Point( 2 , 1 ) #錯誤示範 #print(p2) #print(p1+p2) 程式2-To Be class Point : ...

Python物件導向語法筆記1_類別物件的初始化與共用成員、模組化、繼承

圖片
  一個簡單的Employee物件示範 __init__用途是定義物件剛建立後,預計初始化的流程 類別內的method和一般python函數定義不同,必須包含參數self,並且務必放在第一個參數位置 在單一個main.py程式定義物件並使用物件 class Employee : empCount = 0 #定義物件剛建立後,預計初始化的流程。 #self就是物件自己本身 def __init__ ( self , name, salary): self .name = name self .yoursalary = salary Employee.empCount += 1 #和一般python函數定義不同,類別內的method必須包含參數self,並且務必放在第一個參數位置 def displayTotalEmpCount ( self ): print ( "Total Employee Count: %d" % self .empCount) def displayEmpInfo ( self ): print ( "Name:" , self .name, "Salary:" , self .yoursalary) emp1 = Employee( "Mike" , 42000 ) emp1.displayEmpInfo() emp1.displayTotalEmpCount() emp2 = Employee( "Michelle" , 50000 ) emp2.displayEmpInfo() emp1.displayTotalEmpCount() emp2.displayTotalEmpCount() 可看到empCount會是被各個獨立的物件實體,所共用的屬性,類似以前C#、Java的static修飾。 練習程式2-獨立物件實體成員屬性額外定義與刪除 class Employee : empCount = 0 #定義物件剛建立後,預計初始化的流程。 #self就是物...

DSP筆記3_使用matplotlib練習繪製連續時間訊號子圖做訊號不同頻率比較

圖片
  測試程式 import numpy as np import matplotlib.pyplot as plt t = np.arange( 0 , 1 , 0.01 ) xt1 = np.cos( 2 *np.pi* 3 *t) xt2 = np.cos( 2 *np.pi* 5 *t) xt3 = np.cos( 2 *np.pi* 7 *t) xt4 = np.cos( 2 *np.pi* 9 *t) #subplot(row,column,繪製圖表個數) plt.subplot( 4 , 1 , 1 ) plt.plot(t,xt1) plt.subplot( 4 , 1 , 2 ) plt.plot(t,xt2) plt.subplot( 4 , 1 , 3 ) plt.plot(t,xt3) plt.subplot( 4 , 1 , 4 ) plt.plot(t,xt4) plt.show()

DSP筆記2_使用 NumPy練習繪製連續時間訊號正弦波(Sine)_餘弦波(Cosine)_跟離散時間訊號繪製_stem用法

圖片
  連續時間正弦波-數位取樣訊號 從 0 秒取樣到 0.999 秒,每隔:0.001秒取一個樣本。 plt.plot(t, xt)會把點連起來,像連續波形。 程式碼 import numpy as np import matplotlib.pyplot as plt t = np.arange( 0 , 1 , 0.001 ) #print(t) xt = np.sin( 2 *np.pi* 3 *t) plt.plot(t, xt) plt.title( "Sine Wave" ) plt.xlabel( "Time" ) plt.ylabel( "Amplitude" ) plt.show() xt = np.cos( 2 *np.pi* 3 *t) plt.plot(t, xt) plt.title( "Cosine Wave" ) plt.xlabel( "Time" ) plt.ylabel( "Amplitude" ) plt.show() 正弦波最標準的數學式通常寫成: 符號 A : 振幅 Amplitude f : 頻率 Frequency ,每秒完成幾個週期。 t : 時間 Time  ϕ : 初始相位 Phase 2πf : 角頻率,圓的一整圈是:360度,而在數學上通常不用度數,而使用「弧度 radian」。 36 0 ∘= 2π radians 對照式子可推得各參數分別如下 振幅=1 訊號頻率=3 Hz ,代表一秒鐘有 3 個週期。由於時間剛好介於一秒,因此會看到三個完整的正弦波 初始相位=0 角頻率=6π rad/s t = 0~0.999 秒 而餘弦波也依樣畫葫蘆。 若固定在一個 3 Hz 正弦波,於不同取樣間隔下,離散取樣點會有什麼差異。 左側是用0.001間隔密度採樣。也就是在一秒鐘內對 3 Hz 正弦波取 1000 點。 右側則用0.01間隔密度採樣,取樣間隔變大了 10 倍。每秒只取 100 個樣本。 stem() 很常用來表示離散時間訊號,每一根垂直線代表「一個取樣值」。 程式碼 import numpy as np import matplotlib.pyplot as p...

ObsPy和scipy去趨勢的API介紹

  在之前學習筆記探討過時間序列資料集可能會隱含趨勢,所謂的趨勢就是序列資料隨著時間持續增加或減少的現象。 通常在進行時間序列預測或辨識、建模之前去趨勢會對評估更有幫助。 obspy.core.trace.Trace.detrend https://docs.obspy.org/packages/autogen/obspy.core.trace.Trace.detrend.html#obspy.core.trace.Trace.detrend 在深入了解趨勢的課題之前,先回顧「線性迴歸」與「多項式迴歸」。 線性迴歸(Linear Regression) 適用於簡單線性資料集的擬合直線。如果資料點的散佈形狀近似直線,則資料為線性。 線性趨勢線通常會呈現以固定趨近等比的穩定變化間隔增減。 多項式迴歸(Polynomial regression) 是線性迴歸的延伸,其將依變數與自變數之間的關係建模為 n 次多項式。多項式趨勢線是一種曲線,適合擺動不定的資料使用,多項式的冪次可由資料波動的次數或曲線彎曲點 (波峰和波谷) 的個數決定。 因此二階多項式趨勢線通常僅有一個波峰或波谷。三階多項式趨勢線通常有一個或兩個波峰或波谷。 Scipy提供detrend方法 在Scipy提供detrend方法,預設若無指定type會採用linear線性最小平方法擬合方式或也可以指定constant然後用mean來做去趨勢。 Remove linear or constant trend along axis from data. https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.detrend.html#scipy.signal.detrend obspy.signal.detrend 封裝了預設有三種detrend方法 定義在兩個模組中 第一個模組 obspy.signal.detrend https://docs.obspy.org/packages/autogen/obspy.signal.detrend.html 1.obspy.signal.detrend.simple https://docs.obspy.org/packages/autogen/obspy.signal.detrend.s...