搜索
首页后端开发Python教程Numpy 的 Reshape() 中的 -1 参数是通配符还是固定值?

Is the -1 Parameter in Numpy's Reshape() a Wildcard or a Fixed Value?

理解 -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中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

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

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

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

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

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

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

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

Python:它是真正的解释吗?揭穿神话Python:它是真正的解释吗?揭穿神话May 12, 2025 am 12:05 AM

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

与同一元素的Python串联列表与同一元素的Python串联列表May 11, 2025 am 12:08 AM

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

解释与编译语言:Python的位置解释与编译语言:Python的位置May 11, 2025 am 12:07 AM

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

循环时:您什么时候在Python中使用?循环时:您什么时候在Python中使用?May 11, 2025 am 12:05 AM

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

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

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

热门文章

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

mPDF

mPDF

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

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具