本文以iris数据集为例解释如何使用np.hstack()横向合并二维数组。
step1:加载iris数据集
from sklearn.datasets import load_iris
iris_dataset=load_iris()
#iris数据集中的键值如下
iris_dataset.keys()
-------------------
dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename'])
step2:查看样本特征集和标签集
#data是样本特征集
print("data:\n{}".format(iris_dataset['data'][:20]))
----------------------------------------------------
data:
[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]
[5.4 3.9 1.7 0.4]
[4.6 3.4 1.4 0.3]
[5. 3.4 1.5 0.2]
[4.4 2.9 1.4 0.2]
[4.9 3.1 1.5 0.1]
[5.4 3.7 1.5 0.2]
[4.8 3.4 1.6 0.2]
[4.8 3. 1.4 0.1]
[4.3 3. 1.1 0.1]
[5.8 4. 1.2 0.2]
[5.7 4.4 1.5 0.4]
[5.4 3.9 1.3 0.4]
[5.1 3.5 1.4 0.3]
[5.7 3.8 1.7 0.3]
[5.1 3.8 1.5 0.3]]
#有150个样本,4个特征
iris_dataset['data'].shape
---------------------------
(150, 4)
#标签集
iris_dataset['target']
-----------------------
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
iris_dataset['target'].shape
----------------------------
(150,)
step3:用np.hstack()合并data和target形成(150,5)的数据集
#reshape很重要,不然hstack会报错
new_target = np.reshape(iris_dataset['target'],(150,1))
np.hstack((iris_dataset['data'],new_target))
---------------------------------------------
array([[5.1, 3.5, 1.4, 0.2, 0. ],
[4.9, 3. , 1.4, 0.2, 0. ],
[4.7, 3.2, 1.3, 0.2, 0. ],
[4.6, 3.1, 1.5, 0.2, 0. ],
[5. , 3.6, 1.4, 0.2, 0. ],
[5.4, 3.9, 1.7, 0.4, 0. ],
[4.6, 3.4, 1.4, 0.3, 0. ],
[5. , 3.4, 1.5, 0.2, 0. ],
[4.4, 2.9, 1.4, 0.2, 0. ],
[4.9, 3.1, 1.5, 0.1, 0. ],
[5.4, 3.7, 1.5, 0.2, 0. ],
[4.8, 3.4, 1.6, 0.2, 0. ],
[4.8, 3. , 1.4, 0.1, 0. ],
[4.3, 3. , 1.1, 0.1, 0. ],
[5.8, 4. , 1.2, 0.2, 0. ],
[5.7, 4.4, 1.5, 0.4, 0. ],
[5.4, 3.9, 1.3, 0.4, 0. ],
[5.1, 3.5, 1.4, 0.3, 0. ],