numpy基础: np.split()

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lthirdonel/article/details/88690923

split(ary, indices_or_sections, axis=0)
把一个数组从左到右按顺序切分

参数:
ary: 要切分的数组
indices_or_sections: 如果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置(左开右闭)
axis: 沿着哪个维度进行切向,默认为0,横向切分。为1时,纵向切分

一般用法:

>>> x = np.arange(9.0)
>>> np.split(x, 3)
[array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.,  8.])]
>>> x = np.arange(8.0)
>>> np.split(x, [3, 5, 6, 10])
[array([ 0.,  1.,  2.]),
 array([ 3.,  4.]),
 array([ 5.]),
 array([ 6.,  7.]),
 array([], dtype=float64)]

m = np.arange(8.0)
n = np.split(m, (3,))
print(n)
结果:[array([0., 1., 2.]), array([3., 4., 5., 6., 7.])]

机器学习中的用法解释:

#axis=1,代表列,是要把data数据集中的所有数据按第四、五列之间分割为X集和Y集。
x, y = np.split(data, (4,), axis=1)

纵向分割, 分成两部分, 按列分割

print np.split(A, 2, axis = 1)

横向分割, 分成三部分, 按行分割

print np.split(A, 3, axis = 0)

不均等分割

print np.array_split(A, 3, axis = 1)

垂直方向分割

print np.vsplit(A, 3)

水平方向分割

print np.hsplit(A, 2)

与array_split的差别:split必须要均等分,否则会报错。array_split不会

import numpy as np
x = np.arange(8.0)
print np.array_split(x,3)
print np.split(x, 3)

其他详见:
https://blog.csdn.net/mingyuli/article/details/81227629

猜你喜欢

转载自blog.csdn.net/lthirdonel/article/details/88690923