np.split() 均等分割,不均等会报错
np.array_split() 不均等分割,不会报错
### 关于np.split()函数
x = np.arange(9) #9行1列的列向量
print(x, np.shape(x))
y = np.split(x, 3) # 平均分成三份,不能平均的话则会报错
print(y)
y = np.split(x, 3, axis=0) # 平均分成三份,不能平均的话则会报错,axis默认为0
print(y)
# 不均等分割 np.array_split()
y = np.array_split(x, 4, axis=0) #第0项分割出来的元素最多,剩下的均等分
print('不均等分割:',y)
y = np.split(x, (3,)) # 在第3行之前进行切割,切割成2份
print(y)
y = np.split(x, [3, 5, 7, 8]) #都是开区间进行分割,在第3行,第5行···前进行切割
print(y)
k = np.arange(1, 3, 0.5).reshape(-1, 1)
print(k)
m = x + k
print(m)
m1 = np.split(m, 3, axis=1) # axis=0表示横着切,axis=1表示竖着切
print(m1)
m0 = np.split(m, 2, axis=0)
print(m0)
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, 1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]
# [array([0, 1, 2]), array([3, 4, 5, 6, 7, 8])]
# [array([0, 1, 2]), array([3, 4]), array([5, 6]), array([7]), array([8])]
# [[1. ]
# [1.5]
# [2. ]
# [2.5]]
# [[ 1. 2. 3. 4. 5. 6. 7. 8. 9. ]
# [ 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5]
# [ 2. 3. 4. 5. 6. 7. 8. 9. 10. ]
# [ 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5]]
# [array([[1. , 2. , 3. ],
# [1.5, 2.5, 3.5],
# [2. , 3. , 4. ],
# [2.5, 3.5, 4.5]]), array([[4. , 5. , 6. ],
# [4.5, 5.5, 6.5],
# [5. , 6. , 7. ],
# [5.5, 6.5, 7.5]]), array([[ 7. , 8. , 9. ],
# [ 7.5, 8.5, 9.5],
# [ 8. , 9. , 10. ],
# [ 8.5, 9.5, 10.5]])]
# [array([[1. , 2. , 3. , 4. , 5. , 6. , 7. , 8. , 9. ],
# [1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]]), array([[ 2. , 3. , 4. , 5. , 6. , 7. , 8. , 9. , 10. ],
# [ 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5]])]
# a = [[1. 2. 3. 4. ]
# [1.5 2.5 3.5 4.5]
# [2. 3. 4. 5. ]
# [2.5 3.5 4.5 5.5]]
# b = [[ 5. 6. 7. 8. 9. ]
# [ 5.5 6.5 7.5 8.5 9.5]
# [ 6. 7. 8. 9. 10. ]
# [ 6.5 7.5 8.5 9.5 10.5]]