證明NumPy 陣列
簡介
在Python 中提供了高效的數量計算工具。一個常見的挑戰是調整 NumPy 數組中的元素,將它們左對齊、右對齊、上對齊或下對齊。本文提出了一種使用向量化方法的改進解決方案。
向量化解決方案
justify 函數對齊 2D陣列中的元素,將它們推送到指定的位置
def justify(a, invalid_val=0, axis=1, side='left'): justified_mask = np.sort(a!=invalid_val, axis=axis) if (side=='up') or (side=='left'): justified_mask = np.flip(justified_mask,axis=axis) out = np.full(a.shape, invalid_val) if axis==1: out[justified_mask] = a[a!=invalid_val] else: out.T[justified_mask.T] = a.T[a.T!=invalid_val] return out
用法
a = np.array([[1, 0, 2, 0], [3, 0, 4, 0], [5, 0, 6, 0], [0, 7, 0, 8]]) print(justify(a, axis=0, side='up')) # Justify values vertically "up" print(justify(a, axis=0, side='down')) # Justify values vertically "down" print(justify(a, axis=1, side='left')) # Justify values horizontally "left" print(justify(a, axis=1, side='right')) # Justify values horizontally "right"
輸出
[[1, 7, 2, 8] [3, 0, 4, 0] [5, 0, 6, 0] [0, 0, 0, 0]] [[0, 0, 0, 0] [1, 0, 2, 0] [3, 0, 4, 0] [5, 7, 6, 8]] [[1, 2, 0, 0] [3, 4, 0, 0] [5, 6, 0, 0] [0, 7, 0, 8]] [[0, 0, 1, 2] [0, 0, 3, 4] [0, 0, 5, 6] [0, 0, 7, 8]]
擴展為通用案例
justify_nd 函數擴充了此方法,以對齊任何維度的 ndarray 中的元素。
def justify_nd(a, invalid_val, axis, side): justified_mask = np.sort(a!=invalid_val, axis=axis) if side=='front': justified_mask = np.flip(justified_mask,axis=axis) out = np.full(a.shape, invalid_val) pushax = lambda a: np.moveaxis(a, axis, -1) if (axis==-1) or (axis==a.ndim-1): out[justified_mask] = a[a!=invalid_val] else: pushax(out)[pushax(justified_mask)] = pushax(a)[pushax(a!=invalid_val)] return out
用法(一般案例)
a = np.array([[[54, 57, 0, 77], [77, 0, 0, 31], [46, 0, 0, 98], [98, 22, 68, 75]], [[49, 0, 0, 98], [ 0, 47, 0, 87], [82, 19, 0, 90], [79, 89, 57, 74]], [[ 0, 0, 0, 0], [29, 0, 0, 49], [42, 75, 0, 67], [42, 41, 84, 33]], [[ 0, 0, 0, 38], [44, 10, 0, 0], [63, 0, 0, 0], [89, 14, 0, 0]]]) print(justify_nd(a, invalid_val=0, axis=0, side='front')) # Justify first dimension "front" print(justify_nd(a, invalid_val=0, axis=1, side='front')) # Justify second dimension "front" print(justify_nd(a, invalid_val=0, axis=2, side='front')) # Justify third dimension "front" print(justify_nd(a, invalid_val=0, axis=2, side='end')) # Justify third dimension "end"
輸出
[[[54, 57, 0, 77], [77, 47, 0, 31], [46, 19, 0, 98], [98, 22, 68, 75]], [[49, 0, 0, 98], [29, 10, 0, 87], [82, 75, 0, 90], [79, 89, 57, 74]], [[ 0, 0, 0, 38], [44, 0, 0, 49], [42, 0, 0, 67], [42, 41, 84, 33]], [[ 0, 0, 0, 0], [ 0, 0, 0, 0], [63, 0, 0, 0], [89, 14, 0, 0]]] [[[54, 57, 68, 77], [77, 22, 0, 31], [46, 0, 0, 98], [98, 0, 0, 75]], [[49, 47, 57, 98], [82, 19, 0, 87], [79, 89, 0, 90], [ 0, 0, 0, 74]], [[29, 75, 84, 49], [42, 41, 0, 67], [42, 0, 0, 33], [ 0, 0, 0, 0]], [[44, 10, 0, 38], [63, 14, 0, 0], [89, 0, 0, 0], [ 0, 0, 0, 0]]] [[[ 0, 54, 57, 77], [ 0, 0, 77, 31], [ 0, 0, 46, 98], [98, 22, 68, 75]], [[ 0, 0, 49, 98], [ 0, 0, 47, 87], [ 0, 82, 19, 90], [79, 89, 57, 74]], [[ 0, 0, 0, 0], [ 0, 0, 29, 49], [ 0, 42, 75, 67], [42, 41, 84, 33]], [[ 0, 0, 0, 38], [ 0, 0, 44, 10], [ 0, 0, 0, 63], [ 0, 0, 89, 14]]]
以上是如何有效證明 NumPy 數組中的元素合理?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文解釋瞭如何使用美麗的湯庫來解析html。 它詳細介紹了常見方法,例如find(),find_all(),select()和get_text(),以用於數據提取,處理不同的HTML結構和錯誤以及替代方案(SEL)

Python的statistics模塊提供強大的數據統計分析功能,幫助我們快速理解數據整體特徵,例如生物統計學和商業分析等領域。無需逐個查看數據點,只需查看均值或方差等統計量,即可發現原始數據中可能被忽略的趨勢和特徵,並更輕鬆、有效地比較大型數據集。 本教程將介紹如何計算平均值和衡量數據集的離散程度。除非另有說明,本模塊中的所有函數都支持使用mean()函數計算平均值,而非簡單的求和平均。 也可使用浮點數。 import random import statistics from fracti

Python 對象的序列化和反序列化是任何非平凡程序的關鍵方面。如果您將某些內容保存到 Python 文件中,如果您讀取配置文件,或者如果您響應 HTTP 請求,您都會進行對象序列化和反序列化。 從某種意義上說,序列化和反序列化是世界上最無聊的事情。誰會在乎所有這些格式和協議?您想持久化或流式傳輸一些 Python 對象,並在以後完整地取回它們。 這是一種在概念層面上看待世界的好方法。但是,在實際層面上,您選擇的序列化方案、格式或協議可能會決定程序運行的速度、安全性、維護狀態的自由度以及與其他系

本文比較了Tensorflow和Pytorch的深度學習。 它詳細介紹了所涉及的步驟:數據準備,模型構建,培訓,評估和部署。 框架之間的關鍵差異,特別是關於計算刻度的

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

本文討論了諸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和請求等流行的Python庫,並詳細介紹了它們在科學計算,數據分析,可視化,機器學習,網絡開發和H中的用途

該教程建立在先前對美麗湯的介紹基礎上,重點是簡單的樹導航之外的DOM操縱。 我們將探索有效的搜索方法和技術,以修改HTML結構。 一種常見的DOM搜索方法是EX

本文指導Python開發人員構建命令行界面(CLIS)。 它使用Typer,Click和ArgParse等庫詳細介紹,強調輸入/輸出處理,並促進用戶友好的設計模式,以提高CLI可用性。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

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

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