代码:
import pandas as pd
import tensorflow as tf
import numpy as np
#加载数据
train=pd.read_csv('../Desktop/DataSets/MINIST/train.csv')
test=pd.read_csv('../Desktop/DataSets/MINIST/test.csv')
X_train=train.iloc[:,1:].values
y_train=train.iloc[:,0].values
X_test=test.iloc[:,:].values
print(len(X_test))
print(len(y_train))
#数据预处理
X_train=X_train.astype(np.float)
X_train=np.multiply(X_train,1.0/225)
X_test=X_test.astype(np.float)
X_test=np.multiply(X_test,1.0/225)
image_size=X_train.shape[1]
#像正无穷大取整
image_width=image_height=np.ceil(np.sqrt(image_size)).astype(np.uint8)
#对结果进行处理
#y_count就是类别数 np.unique对列表去重并且从大到小排序
y_count=np.unique(y_train).shape[0]
print(y_count)
#进行one-hot编码
def one_hot(labels,num_classes):
nums_labels=labels.shape[0]#知道标签数
index=np.arange(nums_labels)*num_classes
labels_one_hot=np.zeros((nums_labels,num_classes))#初始化标签数*类别数的0矩阵
#ravel()将多维数