使用2 個索引清單對2D NumPy 陣列進行索引
將np.ix_ 與索引陣列結合使用
使用兩個清單對2D NumPy 數組進行索引索引、數組,我們可以將np.ix_ 函數與索引數組結合使用。
<code class="python">x_indexed = x[np.ix_(row_indices, col_indices)]</code>
將np.ix_ 與Masks 結合使用
或者,我們可以將np.ix_ 與用於選擇和索引數組的布林掩碼:
<code class="python">row_mask = [0, 1, 1, 0, 0, 1, 0] col_mask = [1, 0, 1, 0, 1, 1, 0, 0] x_indexed = x[np.ix_(row_mask, col_mask)]</code>
範例
考慮以下範例:
<code class="python">import numpy as np x = np.random.randint(0, 6, (20, 8)) row_indices = [4, 2, 18, 16, 7, 19, 4] col_indices = [1, 2]</code>
要使用提供的清單索引x,我們可以使用任一方法:
<code class="python"># Using np.ix_ with indexing arrays x_indexed = x[np.ix_(row_indices, col_indices)] # Using np.ix_ with masks row_mask = np.isin(np.arange(x.shape[0]), row_indices) col_mask = np.isin(np.arange(x.shape[1]), col_indices) x_indexed = x[np.ix_(row_mask, col_mask)]</code>
兩種方法都會產生所需的索引數組:
<code class="python">>>> x_indexed array([[76, 56], [70, 47], [46, 95], [76, 56], [92, 46]])</code>
以上是如何使用兩個索引列表來索引 2D NumPy 數組?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

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


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

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

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

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器