numpy的基本用法(四)——numpy array合并
2017-04-26 20:50
393 查看
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
本文主要是关于numpy的一些基本运算的用法。
博客:noahsnail.com | CSDN | 简书
本文主要是关于numpy的一些基本运算的用法。
#!/usr/bin/env python # _*_ coding: utf-8 _*_ import numpy as np # Test 1 A = np.array([1, 1, 1]) B = np.array([2, 2, 2]) # 合并array, 竖直方向 C = np.vstack((A, B)) print A.shape print C.shape print C # 合并array, 水平方向 D = np.hstack((A, B)) print A.shape print D.shape print D # Test 1 result (3,) (2, 3) [[1 1 1] [2 2 2]] (3,) (6,) [1 1 1 2 2 2] # Test 2 A = np.array([1, 1, 1]) # 添加维度 # 列方向上添加维度 B = A[:, np.newaxis] print A print B print A.shape print B.shape # 行方向上添加维度 C = A[np.newaxis, :] print A print C print A.shape print C.shape # Test 2 result [1 1 1] [[1] [1] [1]] (3,) (3, 1) [1 1 1] [[1 1 1]] (3,) (1, 3) # Test 3 A = 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 A print B print C print 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]]
参考资料
https://www.youtube.com/user/MorvanZhou相关文章推荐
- numpy的基本用法(一)——基本运算
- numpy的基本用法(三)——numpy的索引
- numpy学习笔记一:numpy的基本用法
- numpy的基本用法与其中的函数
- Numpy基本用法:ndarray的数据类型
- numpy的基本用法(五)——numpy array分割
- python-numpy的基本用法01
- numpy.random 的函数基本用法
- PyTorch基本用法(一)——Numpy,Torch对比
- numpy的基本用法(二)——基本运算
- numpy基本用法多维数组
- python-numpy的基本用法03
- 【DOC】Numpy 基本用法合集
- Python:一篇文章掌握Numpy的基本用法
- Py修行路 NumPy模块基本用法
- numpy基本用法
- numpy的基本用法(六)——numpy array的copy
- python numpy基础(一)基本用法
- python-numpy的基本用法02
- 让错误无处遁形gdb排错工具基本用法