Numpy的array分割:

import numpy as np

a = np.arange(12).reshape((3, 4))
print(a)
"""
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
"""

# 等量分割
print(np.split(a, 2, axis=1))
"""
[array([[0, 1],
       [4, 5],
       [8, 9]]), 
array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
"""

# 不等量分割
print(np.array_split(a, 3, axis=1))
"""
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2],
       [ 6],
       [10]]), array([[ 3],
       [ 7],
       [11]])]
"""

print(np.vsplit(a, 3))  # 横向
print(np.hsplit(a, 2))  # 纵向
"""
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
"""
最后修改:2019 年 05 月 24 日 01 : 15 PM
如果觉得我的文章对你有用,请随意赞赏