一 np.array
np.array()创建时需要几个维度就要用几个[ ]括起来,这种创建方式要给定数据;
二 np.ones
然后print m[1,:,:,:],:代表默认值(就是一开始你输入时指定的值),这句代码相当于输出2个包中的第1个包(从0开始计数),这个包里面有3个小包,小包里面是2*3的二维数组,所以结果就是上面的~
然后print m[:,1,:,:],相当于输出2个大包,每个大包输出第1个二包取出来
然后print m[:,:,1,:],相当于输出2个大包,每个大包输出3个二包,二包里面第一个三包取出来
然后print m[:,:,:,1],相当于输出2个大包,每个大包输出3个二包,二包里面取三包的第一个数,组成1*2数组
三 np.random.randn
def zero_pad(X, pad):
"""
Pad with zeros all images of the dataset X. The padding is applied to the height and width of an image,
as illustrated in Figure 1.
Argument:
X -- python numpy array of shape (m, n_H, n_W, n_C) representing a batch of m images
pad -- integer, amount of padding around each image on vertical and horizontal dimensions
Returns:
X_pad -- padded image of shape (m, n_H + 2*pad, n_W + 2*pad, n_C)
"""
### START CODE HERE ### (≈ 1 line)
X_pad = np.pad(X, ((0,0), (pad,pad), (pad,pad), (0,0)), 'constant')
### END CODE HERE ###
return X_pad
x=np.random.randn(4,3,3,2)
print ("x",x)
x_pad = zero_pad(x, 2)
print (x_pad)
x [[[[ 6.23362177e-01 -4.34956683e-01]
[ 1.40754000e+00 1.29101580e-01]
[ 1.61694960e+00 5.02740882e-01]]
[[ 1.55880554e+00 1.09402696e-01]
[ -1.21974440e+00 2.44936865e+00]
[ -5.45774168e-01 -1.98837863e-01]]
[[ -7.00398505e-01 -2.03394449e-01]
[ 2.42669441e-01 2.01830179e-01]
[ 6.61020288e-01 1.79215821e+00]]]
[[[ -1.20464572e-01 -1.23312074e+00]
[ -1.18231813e+00 -6.65754518e-01]
[ -1.67419581e+00 8.25029824e-01]]
[[ -4.98213564e-01 -3.10984978e-01]
[ -1.89148284e-03 -1.39662042e+00]
[ -8.61316361e-01 6.74711526e-01]]
[[ 6.18539131e-01 -4.43171931e-01]
[ 1.81053491e+00 -1.30572692e+00]
[ -3.44987210e-01 -2.30839743e-01]]]
[[[ -2.79308500e+00 1.93752881e+00]
[ 3.66332015e-01 -1.04458938e+00]
[ 2.05117344e+00 5.85662000e-01]]
[[ 4.29526140e-01 -6.06998398e-01]
[ 1.06222724e-01 -1.52568032e+00]
[ 7.95026094e-01 -3.74438319e-01]]
[[ 1.34048197e-01 1.20205486e+00]
[ 2.84748111e-01 2.62467445e-01]
[ 2.76499305e-01 -7.33271604e-01]]]
[[[ 8.36004719e-01 1.54335911e+00]
[ 7.58805660e-01 8.84908814e-01]
[ -8.77281519e-01 -8.67787223e-01]]
[[ -1.44087602e+00 1.23225307e+00]
[ -2.54179868e-01 1.39984394e+00]
[ -7.81911683e-01 -4.37508983e-01]]
[[ 9.54250872e-02 9.21450069e-01]
[ 6.07501958e-02 2.11124755e-01]
[ 1.65275673e-02 1.77187720e-01]]]]
[[[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 6.23362177e-01 -4.34956683e-01]
[ 1.40754000e+00 1.29101580e-01]
[ 1.61694960e+00 5.02740882e-01]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 1.55880554e+00 1.09402696e-01]
[ -1.21974440e+00 2.44936865e+00]
[ -5.45774168e-01 -1.98837863e-01]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ -7.00398505e-01 -2.03394449e-01]
[ 2.42669441e-01 2.01830179e-01]
[ 6.61020288e-01 1.79215821e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]]
[[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ -1.20464572e-01 -1.23312074e+00]
[ -1.18231813e+00 -6.65754518e-01]
[ -1.67419581e+00 8.25029824e-01]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ -4.98213564e-01 -3.10984978e-01]
[ -1.89148284e-03 -1.39662042e+00]
[ -8.61316361e-01 6.74711526e-01]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 6.18539131e-01 -4.43171931e-01]
[ 1.81053491e+00 -1.30572692e+00]
[ -3.44987210e-01 -2.30839743e-01]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]]
[[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ -2.79308500e+00 1.93752881e+00]
[ 3.66332015e-01 -1.04458938e+00]
[ 2.05117344e+00 5.85662000e-01]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 4.29526140e-01 -6.06998398e-01]
[ 1.06222724e-01 -1.52568032e+00]
[ 7.95026094e-01 -3.74438319e-01]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 1.34048197e-01 1.20205486e+00]
[ 2.84748111e-01 2.62467445e-01]
[ 2.76499305e-01 -7.33271604e-01]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]]
[[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 8.36004719e-01 1.54335911e+00]
[ 7.58805660e-01 8.84908814e-01]
[ -8.77281519e-01 -8.67787223e-01]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ -1.44087602e+00 1.23225307e+00]
[ -2.54179868e-01 1.39984394e+00]
[ -7.81911683e-01 -4.37508983e-01]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 9.54250872e-02 9.21450069e-01]
[ 6.07501958e-02 2.11124755e-01]
[ 1.65275673e-02 1.77187720e-01]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00]]]]
print (x[1,1])#同print(x([1,1,:,:]))
print (x_pad[1,1])#同print(x_pad([1,1,:,:]))
[[ 2.19069973 -1.89636092]
[-0.64691669 0.90148689]
[ 2.52832571 -0.24863478]]
[[[[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]]
[[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]]
[[ 0. 0. ]
[ 0. 0. ]
[ 0.52946532 0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. 0. ]
[ 0. 0. ]]
[[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]]
[[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]]]]
x=np.random.randn(4,3,3,2)
print (x[0,:,:,0])#取第0个大包里面三个二包里面三个三包里面第0个数据排成1*3数组
x [[[[ 1.62434536 -0.61175641]
[-0.52817175 -1.07296862]
[ 0.86540763 -2.3015387 ]]
[[ 1.74481176 -0.7612069 ]
[ 0.3190391 -0.24937038]
[ 1.46210794 -2.06014071]]
[[-0.3224172 -0.38405435]
[ 1.13376944 -1.09989127]
[-0.17242821 -0.87785842]]]
[[[ 0.04221375 0.58281521]
[-1.10061918 1.14472371]
[ 0.90159072 0.50249434]]
[[ 0.90085595 -0.68372786]
[-0.12289023 -0.93576943]
[-0.26788808 0.53035547]]
[[-0.69166075 -0.39675353]
[-0.6871727 -0.84520564]
[-0.67124613 -0.0126646 ]]]
[[[-1.11731035 0.2344157 ]
[ 1.65980218 0.74204416]
[-0.19183555 -0.88762896]]
[[-0.74715829 1.6924546 ]
[ 0.05080775 -0.63699565]
[ 0.19091548 2.10025514]]
[[ 0.12015895 0.61720311]
[ 0.30017032 -0.35224985]
[-1.1425182 -0.34934272]]]
[[[-0.20889423 0.58662319]
[ 0.83898341 0.93110208]
[ 0.28558733 0.88514116]]
[[-0.75439794 1.25286816]
[ 0.51292982 -0.29809284]
[ 0.48851815 -0.07557171]]
[[ 1.13162939 1.51981682]
[ 2.18557541 -1.39649634]
[-1.44411381 -0.50446586]]]]
[[ 1.62434536 -0.52817175 0.86540763]
[ 1.74481176 0.3190391 1.46210794]
[-0.3224172 1.13376944 -0.17242821]]