基於機器學習分類演算法的鋼材缺陷檢測分類
theme: smartblue
公眾號:尤而小屋
作者:Peter
編輯:Peter
大家好,我是Peter~
今天給大家帶來一篇機器學習在工業資料的實戰文章:基於機器學習分類演算法的鋼材缺陷檢測分類
本文的資料集是來自uci,專門為機器學習提供資料的一個網站:https://archive.ics.uci.edu/ml/index.php
該資料集包含了7種帶鋼缺陷型別(鋼板故障的7種類型:裝飾、Z_劃痕、K_劃痕、汙漬、骯髒、顛簸、其他故障),帶鋼缺陷的27種特徵資料
本文的主要知識點:
資料資訊
具體檢視官網:https://archive.ics.uci.edu/ml/datasets/Steel+Plates+Faults
資料預處理
匯入資料
In [1]:
```python import pandas as pd import numpy as np
import plotly_express as px import plotly.graph_objects as go
子圖
from plotly.subplots import make_subplots
import matplotlib.pyplot as plt import seaborn as sns sns.set_theme(style="whitegrid") %matplotlib inline
忽略警告
import warnings warnings.filterwarnings('ignore') ```
In [2]:
df = pd.read_excel("faults.xlsx")
df.head()
Out[2]:
資料分割
將7種不同的型別和前面的特徵欄位分開:
```python df1 = df.loc[:,"Pastry":] # 7種不同的型別 df2 = df.loc[:,:"SigmoidOfAreas"] # 全部是特徵欄位
分類資料
df1.head()
```
下面是27個特徵的資料:
分類標籤生成
將7種不同的標籤進行分類生成:
型別編碼
In [7]:
```python dic = {} for i, v in enumerate(columns): dic[v]=i # 類別從0開始
dic ```
Out[7]:
python
{'Pastry': 0,
'Z_Scratch': 1,
'K_Scatch': 2,
'Stains': 3,
'Dirtiness': 4,
'Bumps': 5,
'Other_Faults': 6}
In [8]:
``` df1["Label"] = df1["Label"].map(dic)
df1.head() ```
Out[8]:
資料合併
In [9]:
df2["Label"] = df1["Label"]
df2.head()
EDA
資料的基本統計資訊
In [10]:
```
缺失值
df2.isnull().sum() ```
結果顯示是沒有缺失值的:
單個特徵分佈
```python parameters = df2.columns[:-1].tolist()
sns.boxplot(data=df2, y="Steel_Plate_Thickness") plt.show() ```
從箱型圖中能夠觀察到單個特徵的取值分佈情況。下面繪製全部引數的取值分佈箱型圖:
```python
兩個基本引數:設定行、列
fig = make_subplots(rows=7, cols=4) # 1行2列
fig = go.Figure()
新增兩個資料軌跡,形成圖形
for i, v in enumerate(parameters):
r = i // 4 + 1
c = (i+1) % 4
if c ==0:
fig.add_trace(go.Box(y=df2[v].tolist(),name=v),
row=r, col=4)
else:
fig.add_trace(go.Box(y=df2[v].tolist(),name=v),
row=r, col=c)
fig.update_layout(width=1000, height=900)
fig.show() ```
幾點結論:
- 特徵之間的取值範圍不同:從負數到10M
- 部分特徵的取值中存在異常值
- 有些特徵的取值只存在0和1
樣本不均衡
每種類別數量
In [15]:
```
每種型別的數量
df2["Label"].value_counts() ```
Out[15]:
6 673
5 402
2 391
1 190
0 158
3 72
4 55
Name: Label, dtype: int64
可以看到第6類的樣本有673條,但是第4類的樣本只有55條。明顯地不均衡
SMOTE解決
In [16]:
X = df2.drop("Label",axis=1)
y = df2[["Label"]]
In [17]:
```
使用imlbearn庫中上取樣方法中的SMOTE介面
from imblearn.over_sampling import SMOTE
設定隨機數種子
smo = SMOTE(random_state=42) X_smo, y_smo = smo.fit_resample(X, y) y_smo ```
統計一下每個類別的數量:
資料歸一化
特徵矩陣歸一化
In [19]:
```python from sklearn.preprocessing import StandardScaler from sklearn.preprocessing import MinMaxScaler
ss = StandardScaler() data_ss = ss.fit_transform(X_smo)
還原到原資料
origin_data = ss.inverse_transform(data_ss)
```
歸一化後的特徵矩陣
In [21]:
df3 = pd.DataFrame(data_ss, columns=X_smo.columns)
df3.head()
Out[21]:
新增y_smo
In [22]:
df3["Label"] = y_smo
df3.head()
建模
隨機打亂資料
In [23]:
from sklearn.utils import shuffle
df3 = shuffle(df3)
資料集劃分
In [24]:
X = df3.drop("Label",axis=1)
y = df3[["Label"]]
In [25]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.2,
random_state=4)
建模與評價
用函式的形式來解決:
In [26]:
```python from sklearn.model_selection import cross_val_score # 交叉驗證得分 from sklearn import metrics # 模型評價
def build_model(model, X_test, y_test):
model.fit(X_train, y_train)
# 預測概率
y_proba = model_LR.predict_proba(X_test)
# 找出概率值最大的所在索引,作為預測的分類結果
y_pred = np.argmax(y_proba,axis=1)
y_test = np.array(y_test).reshape(943)
print(f"{model}模型得分:")
print("召回率: ",metrics.recall_score(y_test, y_pred, average="macro"))
print("精準率: ",metrics.precision_score(y_test, y_pred, average="macro"))
```
```python
邏輯迴歸(分類)
from sklearn.linear_model import LogisticRegression
建立模型
model_LR = LogisticRegression()
呼叫函式
build_model(model_LR, X_test, y_test)
LogisticRegression()模型得分: 召回率: 0.8247385525937151 精準率: 0.8126617210922679 ```
下面是單獨建立每個模型:
邏輯迴歸
建模
In [28]:
``` from sklearn.linear_model import LogisticRegression # 邏輯迴歸(分類) from sklearn.model_selection import cross_val_score # 交叉驗證得分 from sklearn import metrics # 模型評價
建立模型
model_LR = LogisticRegression() model_LR.fit(X_train, y_train) ```
Out[28]:
LogisticRegression()
預測
In [29]:
```
預測概率
y_proba = model_LR.predict_proba(X_test) y_proba[:3] ```
Out[29]:
array([[4.83469692e-01, 4.23685363e-07, 1.08028560e-10, 3.19294899e-07,
8.92035714e-02, 1.33695855e-02, 4.13956408e-01],
[3.49120137e-03, 6.25018002e-03, 9.36037717e-03, 3.64702993e-01,
1.96814910e-01, 1.35722642e-01, 2.83657697e-01],
[1.82751269e-05, 5.55981861e-01, 3.16768568e-05, 4.90023258e-03,
2.84504970e-03, 3.67190965e-01, 6.90319398e-02]])
In [30]:
```
找出概率值最大的所在索引,作為預測的分類結果
y_pred = np.argmax(y_proba,axis=1) y_pred[:3] ```
Out[30]:
array([0, 3, 1])
評價
In [31]:
```
混淆矩陣
confusion_matrix = metrics.confusion_matrix(y_test, y_pred) confusion_matrix ```
Out[31]:
python
array([[114, 6, 0, 0, 7, 11, 10],
[ 0, 114, 1, 0, 2, 4, 4],
[ 0, 1, 130, 0, 0, 0, 2],
[ 0, 0, 0, 140, 0, 1, 0],
[ 1, 0, 0, 0, 120, 3, 6],
[ 13, 3, 2, 0, 3, 84, 11],
[ 21, 13, 9, 2, 9, 25, 71]])
In [32]:
y_pred.shape
Out[32]:
(943,)
In [33]:
y_test = np.array(y_test).reshape(943)
In [34]:
python
print("召回率: ",metrics.recall_score(y_test, y_pred, average="macro"))
print("精準率: ",metrics.precision_score(y_test, y_pred, average="macro"))
召回率: 0.8247385525937151
精準率: 0.8126617210922679
隨機森林迴歸
SVR
決策樹迴歸
神經網路
GBDT
```python from sklearn.ensemble import GradientBoostingClassifier gbdt = GradientBoostingClassifier(
loss='deviance',
learning_rate=1,
n_estimators=5,
subsample=1,
min_samples_split=2,
min_samples_leaf=1,
max_depth=2,
init=None,
random_state=None,
max_features=None,
verbose=0,
max_leaf_nodes=None,
warm_start=False
)
gbdt.fit(X_train, y_train)
預測概率
y_proba = gbdt.predict_proba(X_test)
最大概率的索引
y_pred = np.argmax(y_proba,axis=1)
print("召回率: ",metrics.recall_score(y_test, y_pred, average="macro")) print("精準率: ",metrics.precision_score(y_test, y_pred, average="macro"))
召回率: 0.9034547294196564 精準率: 0.9000750791353891 ```
LightGBM
結果
| 模型 | Recall | Precision | | ------------ | ------- | --------- | | 邏輯迴歸 | 0.82473 | 0.8126 | | 隨機森林迴歸 | 0.9176 | 0.9149 | | SVR | 0.8897 | 0.8856 | | 決策樹迴歸 | 0.8698 | 0.8646 | | 神經網路 | 0.8908 | 0.8863 | | GBDT | 0.9034 | 0.9 | | LightGBM | 0.9363 | 0.9331 |
上述結果很明顯:
- 整合學習的方案LightGBM、GBDT、隨機森林 的效果是高於其他的模型
- LightGBM 模型效果最佳!
- 基於機器學習分類演算法的鋼材缺陷檢測分類
- JSON資料,Python搞定!
- 邏輯迴歸:信貸違規預測!
- kaggle實戰-腫瘤資料統計分析
- Pandas操作mysql資料庫!
- 數學公式編輯神器-Mathpix Snipping Tool
- 精選20個Pandas統計函式
- 露一手,利用Python分析房價
- 德國信貸資料建模baseline!
- Python函式傳參機制詳解
- Python爬蟲周遊全國-蘭州站
- 一道Pandas題:3種解法
- 機器學習實戰:基於3大分類模型的中風病人預測
- 利用seaborn繪製柱狀圖
- 機器學習實戰:基於MNIST資料集的二分類問題
- 基於深度學習Keras的深圳租房建模
- 機器學習高頻使用程式碼片段
- Python入門:Python變數和賦值
- 深度學習框架Keras入門保姆教程
- kaggle實戰:極度不均衡的信用卡資料分析