# Python 中的 numpy.delete()

| | | | | | | | | | | | | | | |

numpy.delete (array, object, axis = None)：返回一個新數組，其中子數組與提到的軸一起被刪除。

`數組： [array_like] 輸入數組。 object:  [int, array of ints] 要刪除的子數組 axis:  我們要刪除子數組的軸。默認情況下，它對像被應用到扁平化數組`

`一個數組，其中子數組被刪除，按照提到的對象沿著給定軸。 `

` # Python 程序說明`
` #numpy.delete() `

` import ` ` numpy as geek `

` # Working on 1D `

` arr ` ` = ` ` geek.arange (` ` 5 ` `) `

` print ` ` (` ` "arr:" ` `, arr) `

` print ` ` (` ` "Shape:" ` `, arr.shape) `

` `
` # 從一維數組中刪除 `

` `

` object ` ` = ` ` 2 `

` a ` ` = ` ` geek.delete (arr, ` `object ` `) `

`打印` ` ( ` ` "刪除 arr 2 次：" ` `, a) `

`打印``(``"形狀："``, a.shape) `

` object ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `] `

` b ` ` = ` ` geek.delete (arr, ` ` object ` `) `

` print ` ` (` ` "刪除arr 3次：" ` ` , b) `

` print ` ` (` ` "Shape :" ` `, a.shape) `

`arr：[0 1 2 3 4]重複arr 2次：[0 0 1 1 2 2 3 3 4 4]形狀: (10,) 重複 arr 3 次：[0 0 0 ..., 4 4 4] 形狀：(15,) `

< br>

` # Python 程序說明 `
` #numpy.delete () `

` `

`導入` ` numpy as geek `

` `
` # 處理 1D `

` arr ` ` = ` ` geek .arange (` ` 12 ` `)。 reshape (` ` 3 ` `, ` ` 4 ` `) `

` print ` ` (` ` " arr:" ` ` , arr) `

` print ` ` (` ` "形狀："` `, arr.shape) `

` `
` # 從二維數組中移除 `

` a ` ` = ` ` geek.delete (arr, ` ` 1 ` ` , ` ` 0 ` `) `

` "" "`

` [[0 1 2 3] `

` [4 5 6 7] - > 刪除了 `

` [8 9 10 11]] `

` "" "`

` 打印 ` ` (` ` "刪除 arr 2 次：" ` `, a ) `

` print ` ` (` ` "Shape:" ` `, a.shape) `

` # 從二維數組中移除 `

` a ` ` = ` ` geek.delete (arr, ` ` 1 ` `, ` ` 1 ` `) `

` "" "`

` ` ` [[0 1 * 2 3 ] `

` [4 5 * 6 7] `

` ` ` [8 9 * 10 11]] `

` ^ `

` 刪除 `

` """`

`打印` ` (` ` "刪除 arr 2 次：" ` `, a) `

` print ` ` (` `" Shape: "` `, a.shape) `

`arr: [[0 1 2 3 ] [4 5 6 7] [8 9 10 11]] 形狀：(3, 4) 刪除 arr 2 次：[[0 1 2 3] [8 9 10 11] ] 形狀：(2, 4) 刪除 arr 2次：[[0 2 3] [4 6 7] [8 10 11]] 形狀：(3, 3) 刪除 arr 3 次：[0 3 4 5 6 7 8 9 10 11] 形狀：(3, 3) `

 ` # Python 程序說明 `` # numpy.delete () `` import ` ` numpy as geek `` `` arr ` ` = ` ` geek.arange (` ` 5 ` `) `` print ` ` (` ` "原始數組：" ` `, arr) `` 掩碼 ` ` = ` ` geek.ones (` ` len ` ` (arr ), dtype ` ` = ` ` bool ` `) ` ` # 等價於 np.delete (arr, [0,2,4 ], axis = 0) ` 掩碼 [[ ` 0 ` `, ` ` 2 ` `]] ` ` = ` ` False ` ` print ` ` (` `" 掩碼設置為："` `, 掩碼) `` 結果 ` ` = ` ` arr [mask, ...] `` print ` ` (` ` "使用布爾掩碼刪除：" ` `, result) ` 輸出：`原始數組：[0 1 2 3 4] 掩碼設置為：[False True False True True] 使用布爾掩碼刪除：[1 3 4] ` 注意：這些代碼不會為在線ID工作。請在您的系統上運行它們以查看它們是如何工作的本文由 Mohit Gupta_OMG 提供