數據分析——“岩石 vs. 水雷”數據集嶺迴歸分類

一.Python代碼

#!/usr/bin/env python3
# encoding: utf-8
'''
@file: classifierRidgeRocksVMines.py
@time: 2020/5/31 0031 18:39
@author: Jack
@contact: [email protected]
'''
import urllib.request
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import roc_curve, auc
import pylab as plt

## 讀取數據集
target_url = ("https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data")
data = urllib.request.urlopen(target_url)
xList = []
labels = []

for line in data:
    row = str(line, encoding='utf-8').strip().split(",")
    # assign label 1.0 for "M" and 0.0 for "R"
    if row[-1] == 'M':
        labels.append(1.0)
    else:
        labels.append(0.0)
    # remove lable from row
    row.pop()
    # convert row to floats
    floatRow = [float(num) for num in row]
    xList.append(floatRow)

## 將屬性矩陣和label分成訓練集training set(2/3 of data)和測試集test sets (1/3 of data)
indices = range(len(xList))
xListTest = [xList[i] for i in indices if i % 3 == 0]
xListTrain = [xList[i] for i in indices if i % 3 != 0]
labelsTest = [labels[i] for i in indices if i % 3 == 0]
labelsTrain = [labels[i] for i in indices if i % 3 != 0]
## form list of list input into numpy arrays to match input class for scikit-learn linear model
xTrain = np.array(xListTrain)
yTrain = np.array(labelsTrain)
xTest = np.array(xListTest)
yTest = np.array(labelsTest)
alphaList = [0.1 ** i for i in [-3, -2, -1, 0, 1, 2, 3, 4, 5]]

aucList = []
for alph in alphaList:
    rocksVMinesRidgeModel = linear_model.Ridge(alpha=alph)
    rocksVMinesRidgeModel.fit(xTrain, yTrain)
    fpr, tpr, thresholds = roc_curve(yTest, rocksVMinesRidgeModel.predict(xTest))
    roc_auc = auc(fpr, tpr)
    aucList.append(roc_auc)

print("AUC alpha")
for i in range(len(aucList)):
    print(aucList[i], alphaList[i])

## plot auc values versus alpha values
x = [-3, -2, -1, 0, 1, 2, 3, 4, 5]
plt.plot(x, aucList)
plt.xlabel('-log(alpha)')
plt.ylabel('AUC')
plt.show()
## 可視化最好分類器的性能
indexBest = aucList.index(max(aucList))
alph = alphaList[indexBest]
rocksVMinesRidgeModel = linear_model.Ridge(alpha=alph)
rocksVMinesRidgeModel.fit(xTrain, yTrain)
## 繪製散點圖
plt.scatter(rocksVMinesRidgeModel.predict(xTest), yTest, s=100, alpha=0.25)
plt.xlabel("Predicted Value")
plt.ylabel("Actual Value")
plt.show()

AUC alpha
0.8411138411138411 999.9999999999999
0.864045864045864 99.99999999999999
0.9074529074529074 10.0
0.9180999180999181 1.0
0.8828828828828829 0.1
0.8615888615888616 0.010000000000000002
0.8517608517608517 0.0010000000000000002
0.8509418509418509 0.00010000000000000002
0.8493038493038493 1.0000000000000003e-05

在這裏插入圖片描述

在這裏插入圖片描述

二.分類結果分析

AUC的值接近1對應於更好的性能,接近於0.5說明效果不太好。使用AUC的目標是使其最大化而不是最小化。AUC在α=1.0時有一個明顯的突起。數據以及圖示顯示在α遠離1.0 時有明顯的下降。隨着α變小,解方案接近於不受限的線性迴歸問題。α小於1.0時,性能下降表明不受限的解難以達到嶺迴歸的效果。在之前不受限普通均方迴歸的結果可以看到,其中AUC在訓練集上的預測性能爲0.98,在測試集上的預測性能爲0.85,非常接近於使用較小的alpha(α設爲1E-5)的嶺迴歸的AUC值,這說明嶺迴歸會顯著提升性能。對於岩石-水雷問題,數據集包含60個屬性,總共208行數據。將70個樣本移除作爲預留數據,剩下138行用於訓練。樣本數量大約是屬性數量的2倍,但是不受限解(基於普通的最小二乘法)仍然會過擬合數據。這時使用10折交叉驗證來估計性能是一個很好的替換方案。使用 10 折交叉驗證,每一份數據只有20個樣本,訓練數據相對測試數據就會多很多,從而性能上會有一致的提升。
上述圖1爲AUC與alpha參數的關係,該圖展示了在係數向量上使用歐式長度限制可以降低解的複雜度。圖2爲實際分類結果與分類器預測結果的散點圖。該圖與紅酒預測中的散點圖類似,因爲實際預測的輸出是離散的,所以呈現 2 行水平的點。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章