這篇文章帶給大家的內容是關於python中Numpy的廣播原則的程式碼解析,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
為了了解這個原則,首先我們來看一組範例:
# 数组直接对一个数进行加减乘除,产生的结果是数组中的每个元素都会加减乘除这个数。 In [12]: import numpy as np In [13]: a = np.arange(1,13).reshape((4, 3)) In [14]: a * 2 Out[14]: array([[ 2, 4, 6], [ 8, 10, 12], [14, 16, 18], [20, 22, 24]]) # 接下来我们看一下数组与数组之间的计算 In [17]: b = np.arange(12,24).reshape((4,3)) In [18]: b Out[18]: array([[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]) In [19]: a + b Out[19]: array([[13, 15, 17], [19, 21, 23], [25, 27, 29], [31, 33, 35]]) In [20]: c = np.array([1,2,3]) In [21]: a+c Out[21]: array([[ 2, 4, 6], [ 5, 7, 9], [ 8, 10, 12], [11, 13, 15]]) In [22]: d = np.arange(10,14).reshape((4,1)) In [23]: d Out[23]: array([[10], [11], [12], [13]]) In [24]: a + d Out[24]: array([[11, 12, 13], [15, 16, 17], [19, 20, 21], [23, 24, 25]]) # 从上面可以看出,和线性代数中不同的是,m*n列的m行的一维数组或者n列的一维数组也是可以计算的。
這是為什麼呢?這裡要提到numpy的廣播原則:
如果兩個陣列的後緣維度(從結尾開始算起的維度)的軸長度相符或其中一方的長度為1,則認為它們是廣播相容的。廣播會在缺失維度和(或)軸長度為1的維度上進行。在上面的程式碼中,a的維度是(4,3),c的維度是(1,3);d的維度是(4,1)。所以假設有兩個數組,第一個的維度是(x_1, y_1, z_1),另一個數組的維度是(x_2, y_2, z_2),要判斷這兩個數組能不能計算,可以用以下方法來判斷:
if z_1 == z_2 or z_1 == 1 or z_2 == 1: if y_1 == y_2 or y_1 == 1 or y_2 == 1: if x_1 == x_2 or x_1 == 1 or x_2 == 1: 可以运算 else: 不可以运算 else: 不可以运算 else: 不可以运算
這裡要注意:(3,3,2)和(3,2)是可以運算的,因為對於二維數組(3,2)也可以表示為(1,3 ,2),套用上述的規則是完全適用的,同理:(4,2,5,4)和(2,1,4)也是可以進行運算的。
以上是python中Numpy的廣播原則的程式碼解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

禪工作室 13.0.1
強大的PHP整合開發環境

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

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