python中numpy.pad簡單填充0用法
阿新 • • 發佈:2019-04-29
constant 缺省 const nump 上下 sha pytho ons 簡單 print(D)
#對A上下左右填充
E = np.pad(D, (2,0), ‘constant‘)#,constant_values 缺省,則默認填充均為0
print(E)
#輸出
‘‘‘
[[95 96]
[97 98]]
[[ 0 0 0 0 0 0]
[ 0 0 0 0 0 0]
[ 0 0 95 96 0 0]
[ 0 0 97 98 0 0]
[ 0 0 0 0 0 0]
[ 0 0 0 0 0 0]]
[[ 0 0 0 0]
[ 0 0 0 0]
[ 0 0 95 96]
[ 0 0 97 98]]
[[95 96 0 0]
[97 98 0 0]
[ 0 0 0 0]
[ 0 0 0 0]]
[[ 0 0 0 0 0 0]
[ 0 0 0 0 0 0]
[ 0 0 95 96 0 0]
[ 0 0 97 98 0 0]
[ 0 0 0 0 0 0]
[ 0 0 0 0 0 0]]
‘‘‘
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 28 22:07:02 2019
@author: jiangshan
"""
import numpy as np
A = np.arange(95,99).reshape(2,2)
print(A)
#對A上下左右填充
B = np.pad(A, 2, ‘constant‘)#,constant_values 缺省,則默認填充均為0
print(B)
#對A左上填充
C = np.pad(A, (2,0), ‘constant‘)#,constant_values 缺省,則默認填充均為0
print(C)
#對A右下填充
D = np.pad(A, (0,2), ‘constant‘)#,constant_values 缺省,則默認填充均為0
#對A上下左右填充
E = np.pad(D, (2,0), ‘constant‘)#,constant_values 缺省,則默認填充均為0
print(E)
#輸出
‘‘‘
[[95 96]
[97 98]]
[[ 0 0 0 0 0 0]
[ 0 0 0 0 0 0]
[ 0 0 95 96 0 0]
[ 0 0 97 98 0 0]
[ 0 0 0 0 0 0]
[ 0 0 0 0 0 0]]
[[ 0 0 0 0]
[ 0 0 0 0]
[ 0 0 95 96]
[ 0 0 97 98]]
[[95 96 0 0]
[97 98 0 0]
[ 0 0 0 0]
[ 0 0 0 0]]
[[ 0 0 0 0 0 0]
[ 0 0 95 96 0 0]
[ 0 0 97 98 0 0]
[ 0 0 0 0 0 0]
[ 0 0 0 0 0 0]]
‘‘‘
python中numpy.pad簡單填充0用法