二分类——识别1、7
import numpy as np
import struct
import matplotlib.pyplot as plt
import os
from PIL import Image
from sklearn.utils import gen_batches
np.random.seed(2022)
train_image_file = './案例/05 -手写数字识别/train-images-idx3-ubyte'
train_label_file = './案例/05 -手写数字识别/train-labels-idx1-ubyte'
test_image_file = './案例/05 -手写数字识别/t10k-images-idx3-ubyte'
test_label_file = './案例/05 -手写数字识别/t10k-labels-idx1-ubyte'
def decode_image(path: str) -> np.ndarray:
with open(path, 'rb') as f:
magic, num, rows, cols = struct.unpack('>IIII', f.read(16))
images = np.fromfile(f, dtype=np.uint8).reshape(-1, 784)
images = np.array(images, dtype = float)
return images
def decode_label(path: str) -> np.ndarray:
with open(path, 'rb') as f:
magic, n = struct.unpack('>II',f.read(8))
labels = np.fromfile(f, dtype=np.uint8)
labels = np.array(labels, dtype = float)
return labels
def load_data() -> tuple:
train_X = decode_image(train_image_file)
train_Y = decode_label(train_label_file)
test_X = decode_image(test_image_file)
test_Y = decode_label(test_label_file)
return (train_X, train_Y, test_X, test_Y)
trainX, trainY, testX, testY = load_data() # (60000, 784),(60000,)
# digit1 and digit2 are two digits used for binary classification. You could change their values.
digit1 = 1
digit2 = 7
idx = (trainY == digit1)+(trainY==digit2) # 600000
trainX, trainY = trainX[idx, :], trainY[idx] # (13007, 784),(13007,)
num_train, num_feature = trainX.shape # (13007, 784)
idx = (testY == digit1)+(testY==digit2) # 100000
testX, testY = testX[idx, :], testY[idx] # (2163, 784),(2163,)
num_test = testX.shape[0] # 2163
plt.figure(1, figsize=(20,5))
for i in range(4):
idx = np.random.choice(range(num_train))
plt.subplot(int('14'+str(i+1)))
plt.imshow(trainX[idx,:].reshape((28,28)))
plt.title('label is %d'%trainY[idx])
plt.show()
print('number of features is %d'%num_feature)
print('number of training samples is %d'%num_train)
print('number of testing samples is %d'%num_test)
trainY[np.where(trainY == digit1)], trainY[ np.where(trainY ==digit2)] = 0,1
testY[np.where(testY == digit1)], testY[np.wher