1:库
numpy
h5py
matplotlib
PIL、scipy
2:魔法函数
IPython组内预先定义好的函数,通过命令行语句形式访问
#加载包和数据
import numpy as np #python科学计算中最重要的库
import matplotlib.pyplot as plt #python画图的库
import h5py #python与H5文件交互的库
import scipy #python科学计算相关库
from PIL import Image #python图像相关库
from scipy import ndimage #ndimage为数据文件夹
from lr_utils import load_dataset #???
%matplotlib inline #IPython编译器内嵌绘图,可以省掉plt.show()
#加载数据集
#说明:带orig的后缀为图像数据集(train、test),后续将处理它们。不带orig后缀的为返回值,且不需要进行处理
train_set_x_orig
train_set_y
test_set_x_orig
test_Set_y
classes=load_dataset()
3:squeeze函数
A=squeeze(B)
如果B存在单行单列的维度,去掉即为B,反之则A、B元素相同
4:[ : , n ]
[: , n]取所有行第n个(第n+1列)元素
[n , :]取第一维下标为n(第n+1行)的元素所有值
5:classes函数
???
#一个图片例子
index = 3 #可以修改
plt.imshow(train_set_x_orig[index]) #显示索引的图片
print ("y = " + str(train_set_y[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") + "' picture."
#运行结果:y = [0], it's a 'non-cat' picture.
6:BUG
深度学习中代码错误中最常见的就是:向量或矩阵维数对不上,写代码要多注意检查
7:shape函数
X.shape[n]——>X的第n维数据个数
X.shape——>X的维数信息(,)
m_train = train_set_x_orig.shape[0] #训练样本的数目
m_test = test_set_x_orig.shape[0] #测试样本的数目
num_px = train_set_x_orig.shape[1] #训练图像的高(宽)
print ("Number of training examples: m_train = " + str(m_train)) #209
print ("Number of testing examples: m_test = " + str(m_test)) #50
print ("Height/Width of each image: num_px = " + str(num_px)) #64
print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)") #(64,64,3)
print ("train_set_x shape: " + str(train_set_x_orig.shape)) #(209,64,64,3)
print ("train_set_y shape: " + str(train_set_y.shape)) #(1,209)
print ("test_set_x shape: " + str(test_set_x_orig.shape)) #(50,64,64,3)
print ("test_set_y shape: " + str(test_set_y.shape)) #(1,50)
8:reshape函数
X.reshape(X.shape[0],-1)重塑矩形形状,化为二维数据
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T #重塑矩形形状后转置,train_set_x_orig.shape[0]=209,身下的一维数据为其他维元素相乘的结果
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).T
print ("train_set_x_flatten shape: " + str(train_set_x_flatten.shape)) #(12288, 209)
print ("test_set_x_flatten shape: " + str(test_set_x_flatten.shape)) #(12288, 50)
print ("sanity check after reshaping: " + str(train_set_x_flatten[0:5,0])