Python库——numpy.split
字数统计:119 阅读时长 ≈ 1分钟under 机器学习 tag Published on February 14th , 2020 at 06:06 am
函数原型
split(ary, indices_or_sections, axis=0)
参数说明
ary
需要切分的数组indices_or_sections
元组:沿该数代表的位置切分; 数组,为沿轴切分的位置(左开右闭);常数:该数平均切分axis
沿着哪个维度切,默认=0,为横向切分;为1时,纵向切分
import numpy as np
data = np.arange(9.0)
data1 = np.split(data, 3)
data2 = np.split(data, [3, 5, 6, 10])
print(data1)
print(data2)
[array([ 0., 1., 2.]), array([ 3., 4., 5.]), array([ 6., 7., 8.])]
[array([ 0., 1., 2.]),
array([ 3., 4.]),
array([ 5.]),
array([ 6., 7.]),
array([], dtype=float64)]
(3,)的用法
data = np.arange(8.0)
data = np.split(data, (3,))
print(data)
[array([0., 1., 2.]), array([3., 4., 5., 6., 7.])]
用法测试
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
import numpy as np
# Test 1
A = np.arange(12).reshape(3, 4)
print A
# 纵向分割, 分成两部分, 按列分割
print np.split(A, 2, axis = 1)
# 横向分割, 分成三部分, 按行分割
print np.split(A, 3, axis = 0)
# Test 1 result
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
# Test 2
# 不均等分割
print np.array_split(A, 3, axis = 1)
# Test 2 result
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2],
[ 6],
[10]]), array([[ 3],
[ 7],
[11]])]
In [5]:
# Test 3
# 垂直方向分割
print np.vsplit(A, 3)
# 水平方向分割
print np.hsplit(A, 2)
# Test 3 result
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
本文由simyng创作,
采用知识共享署名4.0 国际许可协议进行许可,转载前请务必署名
文章最后更新时间为:February 13th , 2020 at 10:06 pm