理解 -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) 重塑:
z.reshape(-1, 1) # Output: array([[ 1], [ 2], [ 3], [ 4], [ 5], [ 6], [ 7], [ 8], [ 9], [10], # [11], [12]]) # New shape: (12, 1)
使用(-1, 1) 重塑(單一特徵):
z.reshape(1, -1) # Output: array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]) # New shape: (1, 12)
使用(-1, 2) 重塑(單行):
z.reshape(2, -1) # Output: array([[ 1, 2, 3, 4, 5, 6], [ 7, 8, 9, 10, 11, 12]]) # New shape: (2, 6)
使用(2, -1) 重塑:
z.reshape(3, -1) # Output: array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) # New shape: (3, 4)
使用重塑(3, -1)(原始形狀):
請注意,將兩個尺寸指定為-1,即(-1, -1),將導致錯誤。 透過理解 reshape() 中 -1 的意義,開發人員可以有效地轉換數組形狀,以滿足他們在 Numpy 中的特定資料處理需求。以上是Numpy 的 Reshape() 中的 -1 參數是通配符還是固定值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

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)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

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
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

WebStorm Mac版
好用的JavaScript開發工具