Python numpy实现数组合并实例(vstack,hstack)

yipeiwu_com7年前Python基础

若干个数组可以沿不同的轴合合并到一起,vstack,hstack的简单用法,

>>> a = np.floor(10*np.random.random((2,2)))
>>> a
array([[ 8., 8.],
    [ 0., 0.]])
>>> b = np.floor(10*np.random.random((2,2)))
>>> b
array([[ 1., 8.],
    [ 0., 4.]])
>>> np.vstack((a,b))
array([[ 8., 8.],
    [ 0., 0.],
    [ 1., 8.],
    [ 0., 4.]])
>>> np.hstack((a,b))
array([[ 8., 8., 1., 8.],
    [ 0., 0., 0., 4.]])

column_stack函数功能是将一个1D数组转化成一个2D数组,相当于将1D数组垂直排列。

>>> from numpy import newaxis
>>> np.column_stack((a,b))  # With 2D arrays
array([[ 8., 8., 1., 8.],
    [ 0., 0., 0., 4.]])
>>> a = np.array([4.,2.])
>>> b = np.array([2.,8.])
>>> a[:,newaxis] # This allows to have a 2D columns vector
array([[ 4.],
    [ 2.]])
>>> np.column_stack((a[:,newaxis],b[:,newaxis]))
array([[ 4., 2.],
    [ 2., 8.]])
>>> np.vstack((a[:,newaxis],b[:,newaxis])) # The behavior of vstack is different
array([[ 4.],
    [ 2.],
    [ 2.],
    [ 8.]])

对于多维数组,hstack沿第二轴,vstack沿第一条轴。

总结

以上就是本文关于Python numpy实现数组合并实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

django 中使用DateTime常用的时间查询方式

1、lt:小于 queryset=yourobject.objects .filter(时间字段名称__lt=start) 2、lte:小于等于 queryset=yourob...

python pandas 对时间序列文件处理的实例

如下所示: import pandas as pd from numpy import * import matplotlib.pylab as plt import copy d...

python实现超市商品销售管理系统

python实现超市商品销售管理系统

本文实例为大家分享了python超市商品销售管理系统的具体代码,供大家参考,具体内容如下 class Goods(object): def __init__(self, id, n...

python 列表,数组,矩阵两两转换tolist()的实例

通过代码熟悉过程: # -*- coding: utf-8 -*- from numpy import * a1 =[[1,2,3],[4,5,6]] #列表 print('a1 :...

利用python实现微信头像加红色数字功能

利用python实现微信头像加红色数字功能

通过Python实现将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 类似于图中效果 实现过程: 准备两张图片如下:  ...