Numpy:np.vstack()&np.hstack() flat/flatten
一 . np.vstack: 按垂直方向(行顺序)堆叠数组构成一个新的数组
In[3]:
import numpy as np In[4]:
a = np.array([[1,2,3]])
a.shape
Out[4]:
(1, 3) In [5]:
b = np.array([[4,5,6]])
b.shape
Out[5]:
(1, 3) In [6]:
c = np.vstack((a,b)) # 将两个(1,3)形状的数组按垂直方向叠加
print(c)
c.shape # 输出形状为(2,3)
[[1 2 3]
[4 5 6]]
Out[6]:
(2, 3) In [7]:
a = np.array([[1],[2],[3]])
a.shape
Out[7]:
(3, 1) In [9]:
b = np.array([[4],[5],[6]])
b.shape
Out[9]:
(3, 1) In [10]:
c = np.vstack((a,b)) # 将两个(3,1)形状的数组按垂直方向叠加
print(c)
c.shape # 输出形状为(6,1)
[[1]
[2]
[3]
[4]
[5]
[6]]
Out[10]:
(6, 1)
二 . np.hstack:按水平方向(列顺序)堆叠数组构成一个新的数组
In[11]:
a = np.array([[1,2,3]])
a.shape
Out[11]:
(1, 3) In [12]:
b = np.array([[4,5,6]])
b.shape
Out[12]:
(1, 3) In [16]:
c = np.hstack((a,b)) # 将两个(1,3)形状的数组按水平方向叠加
print(c)
c.shape # 输出形状为(1,6)
[[1 2 3 4 5 6]]
Out[16]:
(1, 6) In [17]:
a = np.array([[1],[2],[3]])
a.shape
Out[17]:
(3, 1)
In [18]:
b = np.array([[4],[5],[6]])
b.shape
Out[18]:
(3, 1)
In [19]:
c = np.hstack((a,b)) 将两个(3,1)形状的数组按水平方向叠加
print(c)
c.shape # 输出形状为(3,2)
[[1 4]
[2 5]
[3 6]]
Out[19]:
(3, 2)
三 . numpy.ndarray.flat/flatten
1. flat返回的是一个迭代器,可以用for访问数组每一个元素
import numpy as np
a = np.arange(4).reshape(2,2)
print(a)
for i in a.flat:
print(i)
#迭代器可以用list进行输出
print(list(a.flat))
print(type(a.flat))#返回类型为 numpy.flatiter
#可以用索引对迭代器进行引号
a.flat[3]
#输出:
[[0 1]
[2 3]]
0
1
2
3
[0, 1, 2, 3]
<class 'numpy.flatiter'>
3
2. ndarray.flatten(order=’C’)
Return a copy of the array collapsed into one dimension.
将数组的副本转换为一维,并返回
可选参数,order:{‘C’,‘F’,‘A’,‘K’}
- ‘C’:C-style,行序优先
- ‘F’:Fortran-style,列序优先
- ‘A’:if a is Fortran contiguous in memory ,flatten in column_major order
- ‘K’:按照元素在内存出现的顺序进行排序
默认为’C’
a = np.array([[4,5],[4,9]])
#默认按行转换
b= a.flatten()
print(b)
#换成列来划分
c = a.flatten('F')
print(c)
[4 5 4 9]
[4 4 5 9]
Numpy:np.vstack()&np.hstack() flat/flatten的更多相关文章
- numpy中数据合并,stack ,concentrate,vstack,hstack
在python的numpy库中有一个函数np.stack(), 看过一些博文后觉得别人写的太复杂,然后自己有了一些理解之后做了一些比较简单的解释 np.stack 首先stack函数用于堆叠数组,其调 ...
- [转]numpy中数据合并,stack ,concentrate,vstack,hstack
转自:https://www.cnblogs.com/onemorepoint/p/9541761.html 在python的numpy库中有一个函数np.stack() np.stack 首先sta ...
- np.vstack()和np.hstack()
本文链接:https://blog.csdn.net/m0_37393514/article/details/79538748在这里我们介绍两个拼接数组的方法: np.vstack():在竖直方向上堆 ...
- np.hstack和np.vstack
np.vstack:按垂直方向(行顺序)堆叠数组构成一个新的数组 In[3]: import numpy as np In[4]: a = np.array([[1,2,3]]) a.shape Ou ...
- 【转】python中numpy模块下的np.clip()的用法
转自:https://blog.csdn.net/HHTNAN/article/details/79799612 Numpy 中clip函数的使用 一维数组 其中a是一个数组,后面两个参数分别表示最小 ...
- numpy 常用工具函数 —— np.bincount/np.average
numpy 常用工具函数 —— np.bincount/np.average numpy 常用api(一) numpy 常用api(二) 一个函数提供 random_state 的关键字参数(keyw ...
- h5py报错:FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
导入h5py的时候,报错: /home/harris/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: ...
- 区分range() , np.arange() , np.linspace()
content: range() np.arange() np.linspace() 一.range(start, stop, step) 1.range() 为 python 自带函数 2.生成一个 ...
- NumPy:数组计算
一.MumPy:数组计算 1.NumPy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础.2.NumPy的主要功能: ndarray,一个多维数组结构,高效且节省空间 无需循环 ...
随机推荐
- asp.net 4高级程序设计( 第4版)文摘
第一部分 核心概念 第1章 asp.net 简介 第2章 visual studio 第3章 Web窗体 3.2 web窗体处理阶段 页面框架初始化(page.init),用户代码初始化(page.l ...
- 懒人的ERP开发框架--2B&苦B程序员专用
在企业内部的ERP系统开发中,如果使用MS的技术,那么Winform + DevExpress + IIS + WCF +EF 就是懒人的黄金组合了,EF使用数据库优先,一般ERP应用主要关注点在数据 ...
- curl:get,post 以及SoapClien访问webservice数据
一.curl get模式 public function close_order(){ $url="http://192.168.2.50:7777/U8API.asmx?op=Insert ...
- Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model
使用laravel的db插入数据 DB::table('tags')->insert( ['name'=>'test'] ); 报错Class 'App\Http\Controllers\ ...
- python使用git进行版本控制2
对上一次的readme文件进行修改 现在,运行git status命令看看结果 $ git statusOn branch masterChanges not staged for commit: ...
- SOA和微服务到底是什么关系?
SOA和微服务到底是什么关系? 说实话,我确实不明白SOA和微服务到底有什么本质上的区别,两者说到底都是对外提供接口的一种架构设计方式.我倒觉得微服务其实就是随着互联网的发展,复杂的平台.业务的出现, ...
- 使用VMWare12.0安装Ubuntu系统
使用VMWare12.0安装Ubuntu系统 Vmware12的虚拟机的文档说明: http://pubs.vmware.com/workstation-12/index.jsp#com.vmware ...
- [label][phalcon] How to install Phalcon extension for Zend Server 6.3.0 for windows 7 64bit
At first , you should download Phalcon DLL file. You can download from this link under. http://stati ...
- nginx 用户登录认证
1.配置nginx server { listen ; server_name kibana.×××.com; location / { auth_basic "secret"; ...
- windows服务安装记录
首先打开cmd. 进入这个地址 C:\Windows\Microsoft.NET\Framework\v4.0.30319 执行操作 InstallUtil.exe E:\QueueWinServi ...