本文主要是介绍NumPy(七):拆分【hsplit】【vsplit】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
数组拆分:输出结果为列表,列表中元素为数组
- 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
这篇关于NumPy(七):拆分【hsplit】【vsplit】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!