在这里我们介绍两个拼接数组的方法:
np.vstack():在竖直方向上堆叠
np.hstack():在水平方向上平铺
import numpy as np arr1=np.array([1,2,3]) arr2=np.array([4,5,6]) print np.vstack((arr1,arr2)) print np.hstack((arr1,arr2)) a1=np.array([[1,2],[3,4],[5,6]]) a2=np.array([[7,8],[9,10],[11,12]]) print a1 print a2 print np.hstack((a1,a2))
结果如下:
[[1 2 3]
[4 5 6]]
[1 2 3 4 5 6]
[[1 2]
[3 4]
[5 6]]
[[ 7 8]
[ 9 10]
[11 12]]
[[ 1 2 7 8]
[ 3 4 9 10]
[ 5 6 11 12]]
这里还需要强调一点,在hstack应用的时候,我在做cs231n上的assignment1的时候,我总是在hstack这里出错!才发现我以前学的很肤浅啊!
(1)np.hstack()
函数原型:numpy.hstack(tup)
其中tup是arrays序列,tup : sequence of ndarrays
The arrays must have the same shape along all but the second axis,except 1-D arrays which can be any length.
等价于:np.concatenate(tup, axis=1)
例子一:
import numpy as np brr1=np.array([1,2,3,4,55,6,7,77,8,9,99]) brr1_folds=np.array_split(brr1,3) print brr1_folds print brr1_folds[0:2]+brr1_folds[1:3] print np.hstack((brr1_folds[:2]+brr1_folds[1:3])) print brr1_folds[0:2] print brr1_folds[1:3] #print np.hstack((brr1_folds[0:2],brr1_folds[1:3]))
最后一行如果不注释掉就会出错;
[array([1, 2, 3, 4]), array([55, 6, 7, 77]), array([ 8, 9, 99])]
[array([1, 2, 3, 4]), array([55, 6, 7, 77]), array([55, 6, 7, 77]), array([ 8, 9, 99])]
[ 1 2 3 4 55 6 7 77 55 6 7 77 8 9 99]
[array([1, 2, 3, 4]), array([55, 6, 7, 77])]
[array([55, 6, 7, 77]), array([ 8, 9, 99])]
错误的原因就是以为我的array的维度不一致。改成+就好啦,加号是list的拼接!
例子二:
print np.hstack(([1,2,3,3,4],[3,4,5,8,6,6,7]))
结果是:表明了一维的数组hstack是随意的。
[1 2 3 3 4 3 4 5 8 6 6 7]
例子三:
表明我们的hstack必须要第二维度是一样的:
print np.hstack(([1,2,3,3,4],[3,4,5,8,6,6,7])) print np.hstack(([[1,2,3],[2,3,4]],[[1,2],[2,3]]))
结果:
[1 2 3 3 4 3 4 5 8 6 6 7]
[[1 2 3 1 2][2 3 4 2 3]]
如果你把上面改成下面就会报错了!!!
print np.hstack(([1,2,3,3,4],[3,4,5,8,6,6,7])) print np.hstack(([[1,2,3],[2,3,4]],[[1,2]]))
(2)np.vstack()
函数原型:numpy.hstack(tup)
tup : sequence of ndarrays
The arrays must have the same shape along all but the first axis.1-D arrays must have the same length.
表示我们除了第一维可以不一样外,其他的维度上必须相同的shape。一维的数组必须大小一样。
例子一:
print np.vstack(([1,2,3],[3,4,3])) print np.vstack(([1,2,3],[2,3]))
但是你要注意的是第二行是出错的!
例子二:
print np.vstack(([[1,2,3],[3,4,3]],[[1,3,4],[2,4,5]])) print np.vstack(([[1,2,3],[3,4,3]],[[3,4],[4,5]]))
同样的表明了,如果我们的数组的第二维不一样所以出错了。
print np.vstack(([[1,2,3],[3,4,3]],[[2,4,5]])) print np.vstack(([[1,2,3],[3,4,3]],[[4,5]]))
例子三:
我们传入的是list:
import numpy as np arr1=np.array([[1,2],[2,4],[11,33],[2,44],[55,77],[11,22],[55,67],[67,89]]) arr11=np.array([[11,2,3],[22,3,4],[4,5,6]]) arr1_folds=np.array_split(arr1,3) print arr1_folds print np.vstack(arr1_folds)
结果:
[array([[ 1, 2],
[ 2, 4],
[11, 33]]), array([[ 2, 44],
[55, 77],
[11, 22]]), array([[55, 67],
[67, 89]])]
[[ 1 2]
[ 2 4]
[11 33]
[ 2 44]
[55 77]
[11 22]
[55 67]
[67 89]]
以上是Python中的np.vstack()和np.hstack()怎么使用的详细内容。更多信息请关注PHP中文网其他相关文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

数组的同质性对性能的影响是双重的:1)同质性允许编译器优化内存访问,提高性能;2)但限制了类型多样性,可能导致效率低下。总之,选择合适的数据结构至关重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,内存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

记事本++7.3.1
好用且免费的代码编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中