数组拆分:输出结果为列表,列表中元素为数组
- numpy.hsplit(ary, indices_or_sections):将数组水平(逐列)拆分为多个子数组 → 按列拆分
- numpy.vsplit(ary, indices_or_sections)::将数组垂直(行方向)拆分为多个子数组 → 按行拆
import numpy as np
# 数组拆分:输出结果为列表,列表中元素为数组
ar = np.arange(16).reshape(4, 4)
print('ar = ', ar)
print('-' *100)
# numpy.hsplit(ary, indices_or_sections):将数组水平(逐列)拆分为多个子数组 → 按列拆分
ar1 = np.hsplit(ar, 2)
print('ar1 = {0}, type(ar1) = {1}'.format(ar1, type(ar1)))
print('-' *100)
# numpy.vsplit(ary, indices_or_sections)::将数组垂直(行方向)拆分为多个子数组 → 按行拆
ar2 = np.vsplit(ar, 4)
print('ar2 = {0}, type(ar2) = {1}'.format(ar2, type(ar2)))
打印结果:
ar = [ [ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
----------------------------------------------------------------------------------------------------
ar1 = [array([ [ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]),
array([ [ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])], type(ar1) = <class 'list'>
----------------------------------------------------------------------------------------------------
ar2 = [array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]]), array([[12, 13, 14, 15]])], type(ar2) = <class 'list'>
Process finished with exit code 0