Scikit-Learn 机器学习笔记 – MNIST
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import SGDClassifier
def load_dataset():
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('MNIST original', data_home='dataset')
X, y = mnist['data'], mnist['target']
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]
shuffle_index = np.random.permutation(60000)
X_train, y_train = X_train[shuffle_index], y_train[shuffle_index]
print('load mnist successfully\n', 'X_train shape is: ', X_train.shape, 'X_test shape is:', X_test.shape)
return X_train, X_test, y_train, y_test
def show_data(dataset, labels, index):
sample = dataset[index]
sample_img = sample.reshape(28, 28)
print('The label of this image is:', labels[index])
plt.imshow(sample_img)
plt.axis('off')
plt.show()
def single_number_classify(X_train, y_train, number):
y_train_i = (y_train == number)
sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(X_train, y_train_i)
return sgd_clf, y_train_i
def snc_predict(sgd_clf, samples):
predict = sgd_clf.predict(samples)
print(' Predicted as:', predict)
def snc_assess(sgd_clf, X_train, y_train_i):
from sklearn.model_selection import cross_val_score
crs = cross_val_score(sgd_clf, X_train, y_train_i, cv=3, scoring="accuracy")
print('3折交叉验证精度为:', crs)
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix
y_train_pred = cross_val_predict(sgd_clf, X_train, y_train_i, cv=3)
confu_matrix = confusion_matrix(y_train_i, y_train_pred)
print('单数字二分类器混淆矩阵为:', confu_matrix)
from sklearn.metrics import precision_score, recall_score, f1_score
precision = precision_score(y_train_i, y_train_pred)
recall = recall_score(y_train_i, y_train_pred)
f1_sco = f1_score(y_train_i, y_train_pred)
print('准确率为:', precision, '召回率为', recall, 'F1值为:', f1_sco)
from sklearn.metrics import precision_recall_curve
y_scores = cross_val_predict(sgd_clf, X_train, y_train_i, cv=3, method="decision_function")
precisions, recalls, thresholds = precision_recall_curve(y_train_i, y_scores)
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])
plt.show()
from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(y_train_i, y_scores)
plt.plot(fpr, tpr, linewidth=2, label=None)
plt.plot([0, 1], [0, 1], 'k--')
plt.axis([0, 1, 0, 1])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()
def number_classify_ova(X_train, y_train):
sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(X_train, y_train)
sample = X_train[100]
predict = sgd_clf.predict([sample])
digit_scores = sgd_clf.decision_function([sample])
print('OvA的随机梯度下降分类器预测结果为:', predict, '该样本的各类得分:', digit_scores)
return sgd_clf
def number_classify_ovo(X_train, y_train):
from sklearn.multiclass import OneVsOneClassifier
ovo_sgd_clf = OneVsOneClassifier(SGDClassifier(random_state=42))
ovo_sgd_clf.fit(X_train, y_train)
sample = X_train[100]
predict = ovo_sgd_clf.predict([sample])
print('OvO的随机梯度下降分类器预测结果为:', predict)
def number_classify_rf(X_train, y_train):
from sklearn.ensemble import RandomForestClassifier
forest_clf = RandomForestClassifier(random_state=42)
forest_clf.fit(X_train, y_train)
sample = X_train[100]
predict = forest_clf.predict([sample])
print('随机森林预测分类器结果为', predict)
def input_scaled_sgd(sgd_clf, X_train, y_train):
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train.astype(np.float64))
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import cross_val_score
scores = cross_val_score(sgd_clf, X_train_scaled, y_train, cv=3, scoring="accuracy")
print('输入正则化后的手写数字随机梯度下降多分类器的3折交叉验证精度为:', scores)
y_train_pred = cross_val_predict(sgd_clf, X_train_scaled, y_train, cv=3)
conf_mx = confusion_matrix(y_train, y_train_pred)
print('输入正则化后的手写数字随机梯度下降多分类器的混淆矩阵:\n', conf_mx)
row_sums = conf_mx.sum(axis=1, keepdims=True)
norm_conf_mx = conf_mx / row_sums
np.fill_diagonal(norm_conf_mx, 0)
plt.matshow(norm_conf_mx, cmap=plt.cm.gray)
plt.show()
if __name__ == '__main__':
X_train, X_test, y_train, y_test = load_dataset()
sgd_clf_ova = number_classify_ova(X_train, y_train)
input_scaled_sgd(sgd_clf_ova, X_train, y_train)