1、概要
??本篇學習AI人工智能機器學習之神經網絡,以MLPClassifier和MLPRegressor為例,從代碼層面講述最常用的神經網絡模型MLP。
2、神經網絡 - 簡介
在 Scikit-learn 中,神經網絡是通過 sklearn.neural_network 模塊提供的。最常用的神經網絡模型是多層感知器(MLP,Multi-layer Perceptron),它可以用于分類和回歸任務。
一些基本的概念
- 多層感知器(MLP):一種前饋神經網絡,由輸入層、隱藏層和輸出層組成。每一層的節點與下一層的節點是全連接的。
- 激活函數:每個神經元通常會有一個激活函數,如 ReLU、Sigmoid 和 Tanh,用于引入非線性。
- 損失函數:在訓練過程中用來評估模型性能的函數,MLP 允許使用不同的損失函數,具體取決于任務(如分類或回歸)。
- 優化算法:用于更新網絡權重的算法,最常用的是隨機梯度下降(SGD)和 Adam。
本篇,以兩個示例講述神經網絡MLP的使用方法:
- 示例1:MLPClassifier對數據集進行分類
- 示例2:MLPRegressor對數據進行回歸
本篇相關資料代碼參見:AI人工智能機器學習相關知識資源及使用的示例代碼
3、神經網絡
3.1、安裝依賴
python安裝機器學習庫: pip install scikit-learn
3.2、示例1: MLPClassifier對數據集進行分類
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report# 生成模擬數據集
X, y = make_classification(n_samples=100, n_features=3, n_informative=2, n_redundant=0, n_classes=2, random_state=42)# 劃分訓練集和測試集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)def test_MLPClassifier():# 創建神經網絡分類器實例# hidden_layer_sizes參數,一個元組,定義隱藏層的結構,例如 (100, 50) 表示有兩個隱藏層,第一層100神經元,第二層 50 個神經元。# max_iter參數,最大迭代次數# random_state:隨機種子,使結果可重復model = MLPClassifier(hidden_layer_sizes=(100, 50), max_iter=1000, random_state=42)# 訓練模型model.fit(X_train, y_train)# 進行預測y_pred = model.predict(X_test)# 計算準確率accuracy = accuracy_score(y_test, y_pred)print(f"準確率為: {accuracy:.2f}")# 計算混淆矩陣print(confusion_matrix(y_test, y_pred))# 報告print(classification_report(y_test, y_pred))test_MLPClassifier()
運行上述代碼的輸出:
準確率為: 0.93
[[10 1][ 1 18]]precision recall f1-score support0 0.91 0.91 0.91 111 0.95 0.95 0.95 19accuracy 0.93 30macro avg 0.93 0.93 0.93 30
weighted avg 0.93 0.93 0.93 30
3.3、示例2:MLPRegressor對數據進行回歸
from sklearn.neural_network import MLPRegressor
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split# 創建回歸數據集
X, y = make_regression(n_samples=100, n_features=1, noise=0.1, random_state=42)# 劃分訓練集和測試集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)def test_MLPRegressor():# 創建和訓練 MLPRegressormlp_regressor = MLPRegressor(hidden_layer_sizes=(10,), max_iter=1000, random_state=42)mlp_regressor.fit(X_train, y_train)# 進行預測y_pred = mlp_regressor.predict(X_test)# 打印預測結果print("Predicted values:", y_pred)print("True values:", y_test) test_MLPRegressor()
運行上述代碼的輸出:
Predicted values: [-15.9535171 11.18083111 6.66419755 -6.93420128 -4.88154814-5.62929653 -7.89092833 -19.22598705 6.72784808 7.470322088.14723448 2.80206811 -15.14571795 -8.72301651 -14.61559911-8.06564202 7.77080054 1.30566751 6.16147072 3.04358961]
True values: [-55.37503843 61.96236579 34.0206566 -16.26246864 -9.75232562-12.0363855 -19.53933098 -73.53859117 34.32170107 38.991729642.89105035 14.96006767 -50.87199832 -22.1085758 -48.12392116-19.9786311 40.84203409 10.11000622 30.8780412 15.82045024]
4、 總結
本篇以MLPClassifier和MLPRegressor為例,從代碼層面講述最常用的神經網絡模型MLP。雖然sklearn提供了接口來構建和訓練神經網絡模型,但是對于復雜的復雜的神經網絡模型,推薦使用 TensorFlow 或 PyTorch 等庫。