numpy的基本用法(四)——numpy array合并

来源:互联网 发布:什么叫五十知天命 编辑:程序博客网 时间:2024/05/29 04:08

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

本文主要是关于numpy的一些基本运算的用法。

#!/usr/bin/env python# _*_ coding: utf-8 _*_import numpy as np# Test 1A = np.array([1, 1, 1])B = np.array([2, 2, 2])# 合并array, 竖直方向C = np.vstack((A, B))print A.shapeprint C.shapeprint C# 合并array, 水平方向D = np.hstack((A, B))print A.shapeprint D.shapeprint D# Test 1 result(3,)(2, 3)[[1 1 1] [2 2 2]](3,)(6,)[1 1 1 2 2 2]# Test 2A = np.array([1, 1, 1])# 添加维度# 列方向上添加维度B = A[:, np.newaxis]print Aprint Bprint A.shapeprint B.shape# 行方向上添加维度C = A[np.newaxis, :]print Aprint Cprint A.shapeprint C.shape# Test 2 result[1 1 1][[1] [1] [1]](3,)(3, 1)[1 1 1][[1 1 1]](3,)(1, 3)# Test 3A = np.array([1, 1, 1])B = np.array([2, 2, 2])# A, B列方向添加维度A = A[:, np.newaxis]B = B[:, np.newaxis]# 合并多个array并指定合并的维度, 列方向上合并C = np.concatenate((A, B, B, A), axis = 0)# 合并多个array并指定合并的维度, 行方向上合并D = np.concatenate((A, B, B, A), axis = 1)print Aprint Bprint Cprint D# Test 3 result[[1] [1] [1]][[2] [2] [2]][[1] [1] [1] [2] [2] [2] [2] [2] [2] [1] [1] [1]][[1 2 2 1] [1 2 2 1] [1 2 2 1]]
0 0
原创粉丝点击