本文是對(duì)書《機(jī)器學(xué)習(xí)實(shí)戰(zhàn):基于Scikit-Learn和Tensorflow》第三章的知識(shí)學(xué)習(xí)以及代碼復(fù)現(xiàn),歡迎大家一起學(xué)習(xí)一起進(jìn)步。
獲取數(shù)據(jù)集
提前將MNIST數(shù)據(jù)集下載好,并放在'\scikit_learn_data'目錄之下
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('MNIST original', data_home=r'C:\Users\12637\scikit_learn_data')
mnist

001.png
X, y = mnist["data"], mnist["target"]
X.shape

002.png
一共有70000張圖片,每張圖片有784個(gè)特征。因?yàn)閳D片是28x28像素,每個(gè)特征代表了一個(gè)像素點(diǎn)的強(qiáng)度,從0(白色)到255(黑色)。隨手抓取一個(gè)實(shí)例的特征向量,將其重新形成一個(gè)28X28數(shù)組,然后用Matplotlib的imshow()函數(shù)將其顯示出來
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
some_digit = X[36000]
some_digit_image = some_digit.reshape(28, 28)
plt.imshow(some_digit_image, cmap=matplotlib.cm.binary, interpolation="nearest")
plt.axis("off")
plt.show()

003.png
# MNIST數(shù)據(jù)集中的部分?jǐn)?shù)字圖像
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]
# 給訓(xùn)練集數(shù)據(jù)洗牌
import numpy as np
shuffle_index = np.random.permutation(60000)
X_train, y_train = X_train[shuffle_index], y_train[shuffle_index]
訓(xùn)練一個(gè)二元分類器
# 訓(xùn)練一個(gè)二元分類器
y_train_5 = (y_train == 5) # True for all 5s, False for all other digits
y_test_5 = (y_test == 5)
from sklearn.linear_model import SGDClassifier
sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(X_train, y_train_5)
sgd_clf.predict([some_digit])

004.png
實(shí)施交叉驗(yàn)證
# 實(shí)施交叉驗(yàn)證
from sklearn.model_selection import StratifiedKFold
from sklearn.base import clone
skfolds = StratifiedKFold(n_splits=3, random_state=42)
for train_index, test_index in skfolds.split(X_train, y_train_5):
clone_clf = clone(sgd_clf)
X_train_folds = X_train[train_index]
y_train_folds = (y_train_5[train_index])
X_test_fold = X_train[test_index]
y_test_fold = (y_train_5[test_index])
clone_clf.fit(X_train_folds, y_train_folds)
y_pred = clone_clf.predict(X_test_fold)
n_correct = sum(y_pred == y_test_fold)
print(n_correct / len(y_pred))

005.png

006.png
混淆矩陣
# 混淆矩陣
from sklearn.model_selection import cross_val_predict
y_train_pred = cross_val_predict(sgd_clf, X_train, y_train_5, cv=3)
from sklearn.metrics import confusion_matrix
confusion_matrix(y_train_5, y_train_pred)

007.png
精度和召回率以及精度/召回率平衡

008.png
y_scores = cross_val_predict(sgd_clf, X_train, y_train_5, cv=3, method="decision_function")
from sklearn.metrics import precision_recall_curve
precisions, recalls, thresholds = precision_recall_curve(y_train_5, y_scores)
def plot_precision_recall_vs_threshold(precisions, recalls, thresholds):
plt.plot(thresholds, precisions[:-1], "b--", label="Precision")
plt.plot(thresholds, recalls[:-1], "g-", label="Recall")
plt.xlabel("Threshold")
plt.legend(loc="upper left")
plt.ylim([0, 1])
plot_precision_recall_vs_threshold(precisions, recalls, thresholds)
plt.show()

009.png
ROC曲線繪制
# ROC曲線
from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(y_train_5, y_scores)
def plot_roc_curve(fpr, tpr, label=None):
plt.plot(fpr, tpr, linewidth=2, label=label)
plt.plot([0, 1], [0, 1], 'k--')
plt.axis([0, 1, 0, 1])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plot_roc_curve(fpr, tpr)
plt.show()

010.png
from sklearn.ensemble import RandomForestClassifier
forest_clf = RandomForestClassifier(random_state=42)
y_probas_forest = cross_val_predict(forest_clf, X_train, y_train_5, cv=3, method="predict_proba")
y_scores_forest = y_probas_forest[:, 1]
fpr_forest, tpr_forest, thresholds_forest = roc_curve(y_train_5, y_scores_forest)
plt.plot(fpr, tpr, "b:", label="SGD")
plot_roc_curve(fpr_forest, tpr_forest, "Random Forest")
plt.legend(loc="bottom right")
plt.show()

011.png
多類別分類器

012.png

013.png
錯(cuò)誤分析
# 錯(cuò)誤分析
y_train_pred = cross_val_predict(sgd_clf, X_train_scaled, y_train, cv=3)
conf_mx = confusion_matrix(y_train, y_train_pred)
conf_mx

014.png

015.png

016.png
多標(biāo)簽分類
# 多標(biāo)簽分類
from sklearn.neighbors import KNeighborsClassifier
y_train_large = (y_train >= 7)
y_train_odd = (y_train % 2 == 1)
y_multilabel = np.c_[y_train_large, y_train_odd]
knn_clf = KNeighborsClassifier()
knn_clf.fit(X_train, y_multilabel)
knn_clf.predict([some_digit])
多輸出分類
# 多輸出分類
noise = np.random.randint(0, 100, (len(X_train), 784))
X_train_mod = X_train + noise
noise = np.random.randint(0, 100, (len(X_test), 784))
X_test_mod = X_test + noise
y_train_mod = X_train
y_test_mod = X_test
def plot_digit(data):
image = data.reshape(28, 28)
plt.imshow(image, cmap = matplotlib.cm.binary,
interpolation="nearest")
plt.axis("off")
some_index = 5500
plt.subplot(121); plot_digit(X_test_mod[some_index])
plt.subplot(122); plot_digit(y_test_mod[some_index])
plt.show()

017.png
knn_clf.fit(X_train_mod, y_train_mod)
clean_digit = knn_clf.predict([X_test_mod[some_index]])
plot_digit(clean_digit)

018.png