理解 -1 在 Numpy Reshape 中的作用
在 Numpy 中, reshape() 方法允许转换数组形状。使用 2D 数组时,可以使用 reshape(-1) 将它们重塑为 1D 数组。例如:
import numpy as np a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) a.reshape(-1) # Output: array([[1, 2, 3, 4, 5, 6, 7, 8]])
通常,array[-1] 表示数组中的最后一个元素。然而,在 reshape(-1) 的上下文中,这具有不同的含义。
Reshape 中的 -1 参数
reshape(- 1) 用作通配符维度。这表明新形状的相应尺寸应自动确定。这是通过满足新形状必须与原始数组形状对齐并保留其线性维度的标准来完成的。
Numpy 允许在形状参数之一中使用 -1,从而能够指定未知维度。例如,(-1, 3) 或 (2, -1) 是有效形状,而 (-1, -1) 则不是。
重塑 (-1) 的示例
考虑以下数组:
z = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) z.shape # (3, 4)
使用 (-1) 重塑:
z.reshape(-1) # Output: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) # New shape: (12,)
使用 (-1, 1) 重塑(单一特征):
z.reshape(-1, 1) # Output: array([[ 1], [ 2], [ 3], [ 4], [ 5], [ 6], [ 7], [ 8], [ 9], [10], # [11], [12]]) # New shape: (12, 1)
使用 (-1, 2) 重塑(单行):
z.reshape(1, -1) # Output: array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]) # New shape: (1, 12)
使用 (2, -1) 重塑:
z.reshape(2, -1) # Output: array([[ 1, 2, 3, 4, 5, 6], [ 7, 8, 9, 10, 11, 12]]) # New shape: (2, 6)
使用重塑(3, -1)(原始形状):
z.reshape(3, -1) # Output: array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) # New shape: (3, 4)
请注意,将两个尺寸指定为 -1,即 (-1, -1),将导致错误。
通过理解 reshape() 中 -1 的意义,开发人员可以有效地转换数组形状,以满足他们在 Numpy 中的特定数据处理需求。
以上是Numpy 的 Reshape() 中的 -1 参数是通配符还是固定值?的详细内容。更多信息请关注PHP中文网其他相关文章!

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3汉化版
中文版,非常好用

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

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具