基本機器學習工作流程(Machine Learning Workflow)
在 Happy coder 網站看到一篇關於機器學習的工作流程覺得對我這種初學者還蠻有幫助的。
- 明確定義問題 (Problem Definition)
- 獲取資料與探索性資料分析 (Get Data & Exploratory Data Analysis)
- 資料預處理與特徵工程 (Data Clean/Preprocessing & Feature Engineering)
- 訓練模型與校調 (Model Training)
- 模型驗證 (Model Predict & Testing)
- 模型優化 (Model Optimization)
- 上線運行 (Deploy Model)
- 明確定義問題 (Problem Definition):
才能決定後續使用的演算法
2. 獲取資料與探索性資料分析 (Get Data & Exploratory Data Analysis):
很多現實上想要透過機器學習解決的問題,其實都沒有剛好有現成的資料
可用來分析。
現在網路資訊發達,所以有部分問題所需的資料是可以從網路上取得,尤其是商業活動 相關的,我們可以透過網路爬蟲的方式得到;但科學或醫學相關的資料一開始還是得有 學者來做相關研究。
例如: 安德森鳶尾花卉數據集
3. 資料預處理與特徵工程 (Data Clean/Preprocessing & Feature Engineering):
不少資料集都會存成 csv 格式,透過 python pandas 可以輕鬆得存成 Data Frame 以方便 後續的資料預處理
iris_df = pd.read_csv(abs_file_path, header=None)
Sepal length Sepal width Petal length Petal width Species
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
可以透過資料可視的方式 (python matplotlib) 或是統計學的工具來了解不同特徵之間的 關係
4. 訓練模型與校調 (Model Training):
習慣上會定義名為 fit 或是 train 的函式來實作主要的訓練學習演算法
經由訓練過程的 cost 變化來調整 超參數 (Hyperparameters) 來讓錯誤率最低
或 避免 Overfit 或是 Underfit
def fit(self, X_train, Y, standardize=False):
super().fit(X_train, Y, standardize)
cost = 0
for epoch in range(self.num_epochs):
z = self.net_input(self.X_train)
output = self.activation_fn(z, activation=None)
error = (self.Y - output)
self.w_[1:] += self.learning_rate * np.dot(self.X_train[:, 1:].T, error)
self.w_[0] += self.learning_rate * error.sum()
cost = (error**2).sum() / 2.0
self.cost_.append(cost)
print("final w:\n", self.w_, "\nFinal cost:\n", cost, "\nepochs:\n", (epoch+1), "/", self.num_epochs)
5.模型驗證 (Model Predict & Testing):
訓練好的會有一組對應的 w 數組,我們會再使用與訓練時不同的資料
來確保之後的 未知資料 進來時也能有一樣的預測準確度
一般會在資料預處理階段,把原始資料分成各等分,如 Train 70%/ Test 30%
def predict(self, X_data, addBias=False, standardize=False):
if(standardize==True):
for i in range(self.D):
X_data[:,i] = (X_data[:,i] - X_data[:,i].mean()) / X_data[:,i].std()
# Add one bias term
if(addBias==True):
ones = np.ones((X_data.shape[0], 1))
X_data = np.concatenate((ones, X_data), axis=1)
z = self.net_input(X_data)
output = self.activation_fn(z, activation=None)
Y_hat = self.activation_fn(output, activation="sign")
return Y_hat
6. 模型優化 (Model Optimization):
這部分是比較需要進階或有經驗的分析,留待日後有心得再做筆記
7. 上線運行 (Deploy Model):
如果以上程序都沒問題就可以上線作戰啦,資料千萬種可能日後還是得透過線上訓練 等方式再把模型訓練得更好
沒有留言:
張貼留言