In this article, we will discuss how to split a numpy array in Python using different function. The Numpy module provides us with various functions to split a numpy array into different sub-arrays. Let us discuss them one by one. Table of Contents Split a numpy array using the split() ...
np.array_split(ary, indices_or_sections, axis=0) 示例代码如下: importnumpyasnp# 创建一个一维数组a = np.array([1,2,3,4,5,6])# 将数组分成三个子数组b = np.array_split(a,3)print(b)# 将数组分成四个子数组c = np.array_split(a,4)print(c) 运行结果: [array([1,2]), array([3...
包含分割的 NumPy 数组的列表。 例子 基本用法 a = np.array([4,5,6,7,8,9]) np.array_split(a, 2) [array([4, 5, 6]), array([7, 8, 9])] 当分割不均匀时,不会抛出错误: a = np.array([4,5,6,7,8]) np.array_split(a, 2) [array([4, 5, 6]), array([7, 8])] 通过...
import numpy as np a = np.array([1,2,3,4]) print("【显示】a =",a) print("【执行】np.array_split(a,2)") print(np.array_split(a,2)) print("【执行】np.array_split(a,3)") print(np.array_split(a,3)) print("【执行】np.array_split(a,5)") print(np.array_split(a,5))...
numpy.array_split(ary, indices_or_sections, axis=0) Parameters: Return value: Example: Splitting a NumPy array into equal-sized subarrays using numpy.array_split() >>> import numpy as np >>> a = np.arange(9.0) >>> np.array_split(a, 4) ...
将数组在一维数组中表明的位置分割:[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])] axis 为 0 时在水平方向分割,axis 为 1 时在垂直方向分割: 实例二:默认分割(水平分割) importnumpy as np a= np.arange(16).reshape(4, 4)print('第一个数组:')print(a)print('\n')print...
将一个数组s分为多个数组,按s中元素总数尽量均分,使用numpy.array_split()方法。【示例代码】import numpy as np a = np.array([1,2,3,4])print("原始数组:",a)print("使用numpy.array_split()将数组分为2个数组:",np.array_split(a,2))print("使用numpy.array_split()将数组分为3...
a, b= np.split(m, (4,), axis=1)print('a =',a)print('b =',b)#结果:#[0 1 2 3 4 5 6 7 8] (9,)#[array([0, 1, 2]), array([ 3, 4, 5]), array([6, 7, 8])]#[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]#不均等分割: [array([0...
在这个示例中,split()将一维数组arr等分为3个子数组。每个子数组的元素数量相等。如果数组不能被均匀分割,Numpy会抛出错误。因此,需要确保原始数组的长度能够被分割的数量整除。 使用split分割二维数组 代码语言:javascript 复制 # 创建一个二维数组 arr_2d=np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,...
>>> import numpy as np >>> A = np.arange(36).reshape((2, 2, 9)) >>> print(A) [[[ 0 1 2 3 4 5 6 7 8] [ 9 10 11 12 13 14 15 16 17]] [[18 19 20 21 22 23 24 25 26] [27 28 29 30 31 32 33 34 35]]] >>> print(A.shape) (2, 2, 9) >>> [A1, ...