日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

numpy中數組的堆疊方法_python

作者:夏日輕風有你 ? 更新時間: 2023-05-07 編程語言

一、環境?

Anaconda?3
Python 3.6
Numpy?1.14.3

二、功能用途及官方說明

1、hstack功能:沿水平方向堆疊數組(numpy array)
用途舉例:機器學習數據集準備過程中,可以用于將數據列與標簽列在水平方向上合并,從而得到帶標簽的數據集
官方說明:https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.hstack.html#numpy.hstack

2、vstack功能:沿垂直方向堆疊數組(numpy array)
用途舉例:機器學習數據集準備過程中,可以用于將從過個數據文件中加載的數據行在垂直方向上合并,從而將所有數據集整合為一個數據集
官方說明:https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.vstack.html

三、實例

實例一:使用 np.hstack 將數據與標簽合并

>>> import numpy as np
# 數據準備
>>> data = [i for i in range(18)]
>>> data_array = np.asarray(data)
>>> data_array = np.asarray(data).reshape([6,3])
>>> data_array.shape
(6, 3)
>>> data_array
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17]])
# 標簽準備
>>> label = [0, 1] *3 
>>> label_array = np.asarray(label)
>>> label_array.shape
(6,)
>>> label_array
array([0, 1, 0, 1, 0, 1])
# 在數據的右側水平方向上合并標簽
>>> data_label = np.hstack((data_array,label_array))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/numpy/core/shape_base.py", line 288, in hstack
    return _nx.concatenate(arrs, 1)
ValueError: all the input arrays must have same number of dimensions

直觀上看, np.hstack 只要保證要合并的兩個 numpy 數組的數據行相同,那么兩個 numpy 數組的列就可以沿著水平方向合并了!這里也是初學者常遇到個一個問題,仔細看一下報錯信息就會很容易發現,問題出在要合并的兩個 numpy 數組的維度數量不一致,data_array 的維度是二維(6, 3),而 label_array 的維度是一維 (6, ),因此即使兩個 numpy 數組的行數一樣,也不能沿水平方向進行正常的列堆疊!
正確的方法:

# 在準備標簽時,先將一維的標簽 reshape 為二維 numpy 數組,即 6 行 1 列
>>> label_array = label_array.reshape(-1,1)
>>> data_label = np.hstack((data_array,label_array))
>>> data_label.shape
(6, 4)
>>> data_label
array([[ 0,  1,  2,  0],
       [ 3,  4,  5,  1],
       [ 6,  7,  8,  0],
       [ 9, 10, 11,  1],
       [12, 13, 14,  0],
       [15, 16, 17,  1]])

實例二:使用 np.vstack 合并兩組數據集

# 準備第一數據集
>>> import numpy as np
>>> data1 = np.random.normal(0,1,(2,5))
>>> data1.shape
(2, 5)
>>> data1
array([[-1.49100993,  0.03782522,  0.33961941, -0.64073217,  0.84000297],
       [-1.02662855, -0.91858614, -0.27410549, -0.86956142, -0.44147313]])
# 準備第二個數據集
>>> data2 = np.arange(0,30,2)
>>> data2 = np.arange(0,30,2).reshape([3,5])
>>> data2.shape
(3, 5)
>>> data2
array([[ 0,  2,  4,  6,  8],
       [10, 12, 14, 16, 18],
       [20, 22, 24, 26, 28]])
# 垂直方向堆疊連個數據集
>>> data = np.vstack((data1,data2))
>>> data.shape
(5, 5)
>>> data
array([[-1.49100993,  0.03782522,  0.33961941, -0.64073217,  0.84000297],
       [-1.02662855, -0.91858614, -0.27410549, -0.86956142, -0.44147313],
       [ 0.        ,  2.        ,  4.        ,  6.        ,  8.        ],
       [10.        , 12.        , 14.        , 16.        , 18.        ],
       [20.        , 22.        , 24.        , 26.        , 28.        ]])

實例三:借助列表(list)對多個數據集進行一次性堆疊合并
可以用于在 for / while 循環讀取數據集時,依次先將數據加入到列表(list)中,然后在多個數據集一起堆疊合并,而不用在繁瑣地使用兩兩數據集堆疊合并的方式了

# 準備第一個數據集
>>> data_v1 = np.random.randint(0,10,(2,5))
>>> data_v1.shape
(2, 5)
>>> data_v1
array([[4, 4, 0, 7, 3],
       [3, 9, 0, 3, 0]])
# 準備第二個數據集
>>> data_v2 = np.ones((3,5))
>>> data_v2.shape
(3, 5)
>>> data_v2
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])
# 準備第三個數據集
>>> data_v3 = np.full((2,5),0)
>>> data_v3.shape
(2, 5)
>>> data_v3
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])
# 定義一個臨時存放多個數據集的列表(list),并將所有數據集添加到列表中
>>> data_vlist = []
>>> data_vlist.append(data_v1)
>>> data_vlist.append(data_v2)
>>> data_vlist.append(data_v3)
>>> len(data_vlist)
3
>>> data_vlist
[array([[4, 4, 0, 7, 3],
       [3, 9, 0, 3, 0]]), array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]]), array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])]
# 將存放所有數據集的列表作為 np.vstack() 的輸入參數,即可一次性合并多個數據集
>>> data_vstack = np.vstack(data_vlist)
>>> data_vstack
array([[4., 4., 0., 7., 3.],
       [3., 9., 0., 3., 0.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
>>> data_vstack.shape
(7, 5)
>>> data_vstack
array([[4., 4., 0., 7., 3.],
       [3., 9., 0., 3., 0.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
#########################################################

同理,np.hstack 也可以借助列表(list)一次性水平合并多個數據列

# 準備首個數列
>>> import numpy as np
>>> data_h1 = np.random.randint(0, 10, (3,3))
>>> data_h1.shape
(3, 3)
>>> data_h1 
array([[6, 4, 5],
       [4, 5, 0],
       [7, 1, 9]])
# 準備第二個數列
>>> data_h2 = np.zeros((3,2))
>>> data_h2.shape
(3, 2)
>>> data_h2
array([[0., 0.],
       [0., 0.],
       [0., 0.]])
# 準備第三個數列
>>> data_h3 = np.ones((3,1), dtype=int)
>>> data_h3.shape
(3, 1)
>>> data_h3
array([[1],
       [1],
       [1]])
# 定義一個臨時存放多個數據列的列表(list),并將所有數據列添加到列表中
>>> data_hlist = []
>>> data_hlist.append(data_h1)
>>> data_hlist.append(data_h2)
>>> data_hlist.append(data_h3)
>>> len(data_hlist)
3
>>> data_hlist
[array([[6, 4, 5],
       [4, 5, 0],
       [7, 1, 9]]), array([[0., 0.],
       [0., 0.],
       [0., 0.]]), array([[1],
       [1],
       [1]])]
# 將存放所有數據列的列表作為 np.hstack() 的輸入參數,即可一次性合并多個數據列
>>> data_hstack = np.hstack(data_hlist)
>>> data_hstack.shape
(3, 6)
>>> data_hstack
array([[6., 4., 5., 0., 0., 1.],
       [4., 5., 0., 0., 0., 1.],
       [7., 1., 9., 0., 0., 1.]])

原文鏈接:https://blog.csdn.net/fanlily913/article/details/109492916

欄目分類
最近更新