使用NumPy对数组进行拆分是一种高效且灵活的数据处理方式。NumPy提供了多种函数,如numpy.split()
, numpy.hsplit()
, 和 numpy.vsplit()
,使得数组可以根据不同的需求进行拆分。这些函数能够精确控制拆分的数量和位置,满足不同的数据处理和分析需求。例如,numpy.split()
可以按照指定的份数或索引位置将数组分割成多个子数组;而numpy.hsplit()
和numpy.vsplit()
则分别用于沿数组的水平和垂直方向进行拆分。通过这些拆分操作,用户可以轻松地将复杂的数据集划分为更小的、更易于管理的部分,从而简化数据处理流程,提高数据处理效率。
1.np.vsplit():垂直拆分,需要等分
# 导包
import numpy as np
# 创建一个6行4列的二维数组
n = np.random.randint(0,100,size=(6,4))
n
# 执行结果
array([[11, 47, 82, 13],
[17, 66, 24, 53],
[84, 10, 72, 20],
[83, 33, 7, 23],
[19, 67, 13, 19],
[70, 50, 39, 81]])
# np.vsplit:垂直拆分,需要等分
np.vsplit(n,3) # 平均拆成3份
# 执行结果
[array([[11, 47, 82, 13],
[17, 66, 24, 53]]),
array([[84, 10, 72, 20],
[83, 33, 7, 23]]),
array([[19, 67, 13, 19],
[70, 50, 39, 81]])]
# 可以按照指定的位置拆分:(0-1),(1-2),(2-4),(4-最后)
np.vsplit(n,(1,2,4))
# 执行结果
[array([[11, 47, 82, 13]]),
array([[17, 66, 24, 53]]),
array([[84, 10, 72, 20],
[83, 33, 7, 23]]),
array([[19, 67, 13, 19],
[70, 50, 39, 81]])]
2.np.hsplit():水平方向拆分
# 水平方向拆分
np.hsplit(n,2)
# 执行结果
[array([[11, 47],
[17, 66],
[84, 10],
[83, 33],
[19, 67],
[70, 50]]),
array([[82, 13],
[24, 53],
[72, 20],
[ 7, 23],
[13, 19],
[39, 81]])]
# 可以按照指定的位置拆分:(0-1),(1-2),(2-最后)
np.hsplit(n,(1,2))
# 执行结果
[array([[11, 47],
[17, 66],
[84, 10],
[83, 33],
[19, 67],
[70, 50]]),
array([[82, 13],
[24, 53],
[72, 20],
[ 7, 23],
[13, 19],
[39, 81]])]
# 可以按照指定的位置拆分:(0-1),(1-2),(2-最后)
np.hsplit(n,(1,2))
# 执行结果
[array([[11],
[17],
[84],
[83],
[19],
[70]]),
array([[47],
[66],
[10],
[33],
[67],
[50]]),
array([[82, 13],
[24, 53],
[72, 20],
[ 7, 23],
[13, 19],
[39, 81]])]
3.np.split():可以做水平或垂直拆分
# split:可以做水平或垂直拆分,axis=0 行,axis=1 列
# 默认按行拆分
np.split(n,2)
# 执行结果
[array([[11, 47, 82, 13],
[17, 66, 24, 53],
[84, 10, 72, 20]]),
array([[83, 33, 7, 23],
[19, 67, 13, 19],
[70, 50, 39, 81]])]
# 按行拆分
np.split(n,2,axis=0)
# 执行结果
[array([[11, 47, 82, 13],
[17, 66, 24, 53],
[84, 10, 72, 20]]),
array([[83, 33, 7, 23],
[19, 67, 13, 19],
[70, 50, 39, 81]])]
# 按列拆分
np.split(n,2,axis=1)
# 执行结果
[array([[11, 47],
[17, 66],
[84, 10],
[83, 33],
[19, 67],
[70, 50]]),
array([[82, 13],
[24, 53],
[72, 20],
[ 7, 23],
[13, 19],
[39, 81]])]
4.案例:把图片拆分
# 数据分析三剑客
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# python.png
# 图片:其实时数字组成的,三维数组
# RGB:红Red,绿Green,蓝Blue
# RGB范围:0-255
# plt.imread:读取图片的数据
pyimg = plt.imread("python.png")
pyimg
print(pyimg.shape)
# 执行结果
(539, 1080, 4)
# 切片
img = pyimg[:-1]
img.shape
# 执行结果
(538, 1080, 4)
plt.imshow(img)
# 垂直拆分
img1 = np.split(img,2)
img1
# 执行结果
[array([[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]],
[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]],
[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]],
...,
[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]],
[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]],
[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]]], dtype=float32),
array([[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]],
[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]],
[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]],
...,
[[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
...,
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ]],
[[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
...,
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ]],
[[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
...,
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ]]], dtype=float32)]
# 图片上半部分
plt.imshow(img1[0])
# 图片下半部分
plt.imshow(img1[1])
# 水平拆分成5等份
img2 = np.split(img,5,axis=1)
img2
# 执行结果
[array([[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ]],
[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ]],
[[0.09019608, 0.15294118, 0.24313726, 1. ],
[0.05882353, 0.12156863, 0.21176471, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ],
...,
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ]],
...,
[[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
...,
[0.11372549, 0.29803923, 0.64705884, 1. ],
[0.11764706, 0.29411766, 0.6509804 , 1. ],
[0.11764706, 0.29411766, 0.6509804 , 1. ]],
[[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
...,
[0.11372549, 0.29803923, 0.64705884, 1. ],
[0.11764706, 0.29411766, 0.6509804 , 1. ],
[0.11764706, 0.29411766, 0.6509804 , 1. ]],
[[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
...,
[0.11372549, 0.29803923, 0.64705884, 1. ],
[0.11764706, 0.29411766, 0.6509804 , 1. ],
[0.11764706, 0.29411766, 0.6509804 , 1. ]]], dtype=float32),
array([[[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
...,
[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ]],
[[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
...,
[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ]],
[[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
...,
[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ]],
...,
[[0.11764706, 0.29411766, 0.6509804 , 1. ],
[0.11764706, 0.29411766, 0.6509804 , 1. ],
[0.12156863, 0.29803923, 0.654902 , 1. ],
...,
[0.5294118 , 0.72156864, 0.9490196 , 1. ],
[0.53333336, 0.7294118 , 0.95686275, 1. ],
[0.5254902 , 0.7254902 , 0.9607843 , 1. ]],
[[0.11764706, 0.29411766, 0.6509804 , 1. ],
[0.11764706, 0.29411766, 0.6509804 , 1. ],
[0.12156863, 0.29803923, 0.654902 , 1. ],
...,
[0.53333336, 0.7254902 , 0.9529412 , 1. ],
[0.5372549 , 0.73333335, 0.9607843 , 1. ],
[0.5254902 , 0.7254902 , 0.9607843 , 1. ]],
[[0.11764706, 0.29411766, 0.6509804 , 1. ],
[0.12156863, 0.29803923, 0.654902 , 1. ],
[0.1254902 , 0.3019608 , 0.65882355, 1. ],
...,
[0.5372549 , 0.7294118 , 0.95686275, 1. ],
[0.54509807, 0.7372549 , 0.96862745, 1. ],
[0.53333336, 0.73333335, 0.96862745, 1. ]]], dtype=float32),
array([[[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ],
...,
[0.10196079, 0.21176471, 0.39607844, 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ]],
[[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ],
...,
[0.10196079, 0.21176471, 0.39607844, 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ]],
[[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ],
[0.11372549, 0.21960784, 0.40392157, 1. ],
...,
[0.10196079, 0.21176471, 0.39607844, 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ]],
...,
[[0.5176471 , 0.7176471 , 0.9647059 , 1. ],
[0.5137255 , 0.7254902 , 0.972549 , 1. ],
[0.52156866, 0.73333335, 0.9843137 , 1. ],
...,
[0.05882353, 0.19607843, 0.41568628, 1. ],
[0.06666667, 0.19215687, 0.41568628, 1. ],
[0.07058824, 0.19607843, 0.41960785, 1. ]],
[[0.52156866, 0.72156864, 0.96862745, 1. ],
[0.5176471 , 0.7294118 , 0.9764706 , 1. ],
[0.5254902 , 0.7372549 , 0.9882353 , 1. ],
...,
[0.05882353, 0.19607843, 0.42352942, 1. ],
[0.06666667, 0.19215687, 0.42352942, 1. ],
[0.07058824, 0.19607843, 0.42745098, 1. ]],
[[0.5254902 , 0.7254902 , 0.972549 , 1. ],
[0.52156866, 0.73333335, 0.98039216, 1. ],
[0.5294118 , 0.7411765 , 0.99215686, 1. ],
...,
[0.0627451 , 0.19607843, 0.42352942, 1. ],
[0.07058824, 0.19607843, 0.42745098, 1. ],
[0.07058824, 0.19607843, 0.42745098, 1. ]]], dtype=float32),
array([[[0.10196079, 0.21176471, 0.4 , 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ],
...,
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.09803922, 0.17254902, 0.2901961 , 1. ]],
[[0.10196079, 0.21176471, 0.4 , 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ],
...,
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.09803922, 0.17254902, 0.2901961 , 1. ]],
[[0.10196079, 0.21176471, 0.4 , 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ],
[0.10196079, 0.21176471, 0.4 , 1. ],
...,
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.10196079, 0.1764706 , 0.29411766, 1. ],
[0.09803922, 0.17254902, 0.2901961 , 1. ]],
...,
[[0.07058824, 0.19607843, 0.42745098, 1. ],
[0.07058824, 0.1882353 , 0.42352942, 1. ],
[0.06666667, 0.18431373, 0.41960785, 1. ],
...,
[0.23921569, 0.5176471 , 0.8627451 , 1. ],
[0.23921569, 0.5176471 , 0.8627451 , 1. ],
[0.23921569, 0.5176471 , 0.8627451 , 1. ]],
[[0.07058824, 0.19607843, 0.42745098, 1. ],
[0.07058824, 0.1882353 , 0.42352942, 1. ],
[0.06666667, 0.18431373, 0.41960785, 1. ],
...,
[0.1882353 , 0.4862745 , 0.8627451 , 1. ],
[0.1882353 , 0.4862745 , 0.8627451 , 1. ],
[0.1882353 , 0.4862745 , 0.8627451 , 1. ]],
[[0.07058824, 0.19607843, 0.42745098, 1. ],
[0.07058824, 0.1882353 , 0.42352942, 1. ],
[0.06666667, 0.18431373, 0.41960785, 1. ],
...,
[0.16470589, 0.47843137, 0.8666667 , 1. ],
[0.16470589, 0.47843137, 0.8666667 , 1. ],
[0.16470589, 0.47843137, 0.8666667 , 1. ]]], dtype=float32),
array([[[0.09411765, 0.16862746, 0.28627452, 1. ],
[0.10196079, 0.1764706 , 0.2901961 , 1. ],
[0.10196079, 0.18039216, 0.28627452, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]],
[[0.09411765, 0.16862746, 0.28627452, 1. ],
[0.10196079, 0.1764706 , 0.2901961 , 1. ],
[0.10196079, 0.18039216, 0.28627452, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]],
[[0.09411765, 0.16862746, 0.28627452, 1. ],
[0.10196079, 0.1764706 , 0.2901961 , 1. ],
[0.10196079, 0.18039216, 0.28627452, 1. ],
...,
[0.05490196, 0.11764706, 0.20784314, 1. ],
[0.05098039, 0.11372549, 0.20392157, 1. ],
[0.08235294, 0.14509805, 0.23529412, 1. ]],
...,
[[0.23921569, 0.5176471 , 0.8627451 , 1. ],
[0.23921569, 0.5176471 , 0.8627451 , 1. ],
[0.23921569, 0.5176471 , 0.8627451 , 1. ],
...,
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ]],
[[0.1882353 , 0.4862745 , 0.8627451 , 1. ],
[0.1882353 , 0.4862745 , 0.8627451 , 1. ],
[0.1882353 , 0.4862745 , 0.8627451 , 1. ],
...,
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ]],
[[0.16470589, 0.47843137, 0.8666667 , 1. ],
[0.16470589, 0.47843137, 0.8666667 , 1. ],
[0.16470589, 0.47843137, 0.8666667 , 1. ],
...,
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ],
[0.0627451 , 0.25882354, 0.5294118 , 1. ]]], dtype=float32)]
# 显示第三部分图片
plt.imshow(img2[2])