在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ NumPy數(shù)組操作
NumPy位操作
NumPy數(shù)學算數(shù)函數(shù)
NumPy高級索引
NumPy環(huán)境安裝配置
NumPy IO文件操作
NumPy字符串函數(shù)
NumPy切片和索引
NumPy統(tǒng)計函數(shù)
NumPy矩陣庫
NumPy數(shù)組創(chuàng)建例程
NumPy線性代數(shù)
NumPy Matplotlib庫
NumPy教程
NumPy排序、搜索和計數(shù)函數(shù)
NumPy字節(jié)交換
NumPy Ndarray對象
NumPy數(shù)組操作
NumPy使用 Matplotlib 繪制直方圖
NumPy數(shù)組屬性
NumPy廣播
NumPy來自現(xiàn)有數(shù)據(jù)的數(shù)組
NumPy副本和視圖
NumPy在數(shù)組上的迭代
NumPy來自數(shù)值范圍的數(shù)組
NumPy算數(shù)運算
NumPy數(shù)據(jù)類型

NumPy數(shù)組操作

NumPy - 數(shù)組操作

NumPy包中有幾個例程用于處理ndarray對象中的元素。 它們可以分為以下類型:

修改形狀

序號 形狀及描述
1. reshape 不改變數(shù)據(jù)的條件下修改形狀
2. flat 數(shù)組上的一維迭代器
3. flatten 返回折疊為一維的數(shù)組副本
4. ravel 返回連續(xù)的展開數(shù)組

numpy.reshape

這個函數(shù)在不改變數(shù)據(jù)的條件下修改形狀,它接受如下參數(shù):

numpy.reshape(arr, newshape, order')

其中:

  • arr:要修改形狀的數(shù)組
  • newshape:整數(shù)或者整數(shù)數(shù)組,新的形狀應當兼容原有形狀
  • order'C'為 C 風格順序,'F'為 F 風格順序,'A'為保留原順序。

例子

import numpy as np
a = np.arange(8)
print '原始數(shù)組:'
print a
print '\n'

b = a.reshape(4,2)
print '修改后的數(shù)組:'
print b

輸出如下:

原始數(shù)組:
[0 1 2 3 4 5 6 7]

修改后的數(shù)組:
[[0 1]
 [2 3]
 [4 5]
 [6 7]]

numpy.ndarray.flat

該函數(shù)返回數(shù)組上的一維迭代器,行為類似 Python 內建的迭代器。

例子

import numpy as np
a = np.arange(8).reshape(2,4)
print '原始數(shù)組:'
print a
print '\n'

print '調用 flat 函數(shù)之后:'
# 返回展開數(shù)組中的下標的對應元素
print a.flat[5]

輸出如下:

原始數(shù)組:
[[0 1 2 3]
 [4 5 6 7]]

調用 flat 函數(shù)之后:
5

numpy.ndarray.flatten

該函數(shù)返回折疊為一維的數(shù)組副本,函數(shù)接受下列參數(shù):

ndarray.flatten(order)

其中:

  • order'C' — 按行,'F' — 按列,'A' — 原順序,'k' — 元素在內存中的出現(xiàn)順序。

例子

import numpy as np
a = np.arange(8).reshape(2,4)

print '原數(shù)組:'
print a
print '\n'  
# default is column-major

print '展開的數(shù)組:'
print a.flatten()
print '\n'  

print '以 F 風格順序展開的數(shù)組:'
print a.flatten(order = 'F')

輸出如下:

原數(shù)組:
[[0 1 2 3]
 [4 5 6 7]]

展開的數(shù)組:
[0 1 2 3 4 5 6 7]

以 F 風格順序展開的數(shù)組:
[0 4 1 5 2 6 3 7]

numpy.ravel

這個函數(shù)返回展開的一維數(shù)組,并且按需生成副本。返回的數(shù)組和輸入數(shù)組擁有相同數(shù)據(jù)類型。這個函數(shù)接受兩個參數(shù)。

numpy.ravel(a, order)

構造器接受下列參數(shù):

  • order'C' — 按行,'F' — 按列,'A' — 原順序,'k' — 元素在內存中的出現(xiàn)順序。

例子

import numpy as np
a = np.arange(8).reshape(2,4)

print '原數(shù)組:'
print a
print '\n'  

print '調用 ravel 函數(shù)之后:'
print a.ravel()  
print '\n'

print '以 F 風格順序調用 ravel 函數(shù)之后:'
print a.ravel(order = 'F')
原數(shù)組:
[[0 1 2 3]
 [4 5 6 7]]

調用 ravel 函數(shù)之后:
[0 1 2 3 4 5 6 7]

以 F 風格順序調用 ravel 函數(shù)之后:
[0 4 1 5 2 6 3 7]

翻轉操作

序號 操作及描述
1. transpose 翻轉數(shù)組的維度
2. ndarray.Tself.transpose()相同
3. rollaxis 向后滾動指定的軸
4. swapaxes 互換數(shù)組的兩個軸

numpy.transpose

這個函數(shù)翻轉給定數(shù)組的維度。如果可能的話它會返回一個視圖。函數(shù)接受下列參數(shù):

numpy.transpose(arr, axes)

其中:

  • arr:要轉置的數(shù)組
  • axes:整數(shù)的列表,對應維度,通常所有維度都會翻轉。

例子

import numpy as np
a = np.arange(12).reshape(3,4)

print '原數(shù)組:'
print a  
print '\n'

print '轉置數(shù)組:'
print np.transpose(a)

輸出如下:

原數(shù)組:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

轉置數(shù)組:
[[ 0 4 8]
 [ 1 5 9]
 [ 2 6 10]
 [ 3 7 11]]

numpy.ndarray.T

該函數(shù)屬于ndarray類,行為類似于numpy.transpose。

例子

import numpy as np
a = np.arange(12).reshape(3,4)

print '原數(shù)組:'
print a
print '\n'  

print '轉置數(shù)組:'
print a.T

輸出如下:

原數(shù)組:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

轉置數(shù)組:
[[ 0 4 8]
 [ 1 5 9]
 [ 2 6 10]
 [ 3 7 11]]

numpy.rollaxis

該函數(shù)向后滾動特定的軸,直到一個特定位置。這個函數(shù)接受三個參數(shù):

numpy.rollaxis(arr, axis, start)

其中:

  • arr:輸入數(shù)組
  • axis:要向后滾動的軸,其它軸的相對位置不會改變
  • start:默認為零,表示完整的滾動。會滾動到特定位置。

例子

# 創(chuàng)建了三維的 ndarray
import numpy as np
a = np.arange(8).reshape(2,2,2)

print '原數(shù)組:'
print a
print '\n'
# 將軸 2 滾動到軸 0(寬度到深度)

print '調用 rollaxis 函數(shù):'
print np.rollaxis(a,2)  
# 將軸 0 滾動到軸 1:(寬度到高度)
print '\n'

print '調用 rollaxis 函數(shù):'
print np.rollaxis(a,2,1)

輸出如下:

原數(shù)組:
[[[0 1]
 [2 3]]
 [[4 5]
 [6 7]]]

調用 rollaxis 函數(shù):
[[[0 2]
 [4 6]]
 [[1 3]
 [5 7]]]

調用 rollaxis 函數(shù):
[[[0 2]
 [1 3]]
 [[4 6]
 [5 7]]]

numpy.swapaxes

該函數(shù)交換數(shù)組的兩個軸。對于 1.10 之前的 NumPy 版本,會返回交換后數(shù)組的試圖。這個函數(shù)接受下列參數(shù):

numpy.swapaxes(arr, axis1, axis2)
  • arr:要交換其軸的輸入數(shù)組
  • axis1:對應第一個軸的整數(shù)
  • axis2:對應第二個軸的整數(shù)
# 創(chuàng)建了三維的 ndarray
import numpy as np
a = np.arange(8).reshape(2,2,2)

print '原數(shù)組:'
print a
print '\n'  
# 現(xiàn)在交換軸 0(深度方向)到軸 2(寬度方向)

print '調用 swapaxes 函數(shù)后的數(shù)組:'
print np.swapaxes(a, 2, 0)

輸出如下:

原數(shù)組:
[[[0 1]
 [2 3]]

 [[4 5]
  [6 7]]]

調用 swapaxes 函數(shù)后的數(shù)組:
[[[0 4]
 [2 6]]

 [[1 5]
  [3 7]]]

修改維度

序號 維度和描述
1. broadcast 產生模仿廣播的對象
2. broadcast_to 將數(shù)組廣播到新形狀
3. expand_dims 擴展數(shù)組的形狀
4. squeeze 從數(shù)組的形狀中刪除單維條目

broadcast

如前所述,NumPy 已經內置了對廣播的支持。 此功能模仿廣播機制。 它返回一個對象,該對象封裝了將一個數(shù)組廣播到另一個數(shù)組的結果。

該函數(shù)使用兩個數(shù)組作為輸入?yún)?shù)。 下面的例子說明了它的用法。

import numpy as np
x = np.array([[1], [2], [3]])
y = np.array([4, 5, 6])  

# 對 y 廣播 x
b = np.broadcast(x,y)  
# 它擁有 iterator 屬性,基于自身組件的迭代器元組

print '對 y 廣播 x:'
r,c = b.iters
print r.next(), c.next()
print r.next(), c.next()
print '\n'  
# shape 屬性返回廣播對象的形狀

print '廣播對象的形狀:'
print b.shape
print '\n'  
# 手動使用 broadcast 將 x 與 y 相加
b = np.broadcast(x,y)
c = np.empty(b.shape)

print '手動使用 broadcast 將 x 與 y 相加:'
print c.shape
print '\n'  
c.flat = [u + v for (u,v) in b]

print '調用 flat 函數(shù):'
print c
print '\n'  
# 獲得了和 NumPy 內建的廣播支持相同的結果

print 'x 與 y 的和:'
print x + y

輸出如下:

對 y 廣播 x:
1 4
1 5

廣播對象的形狀:
(3, 3)

手動使用 broadcast 將 x 與 y 相加:
(3, 3)

調用 flat 函數(shù):
[[ 5. 6. 7.]
 [ 6. 7. 8.]
 [ 7. 8. 9.]]

x 與 y 的和:
[[5 6 7]
 [6 7 8]
 [7 8 9]]

numpy.broadcast_to

此函數(shù)將數(shù)組廣播到新形狀。 它在原始數(shù)組上返回只讀視圖。 它通常不連續(xù)。 如果新形狀不符合 NumPy 的廣播規(guī)則,該函數(shù)可能會拋出ValueError。

注意 - 此功能可用于 1.10.0 及以后的版本。

該函數(shù)接受以下參數(shù)。

numpy.broadcast_to(array, shape, subok)

例子

import numpy as np
a = np.arange(4).reshape(1,4)

print '原數(shù)組:'
print a
print '\n'  

print '調用 broadcast_to 函數(shù)之后:'
print np.broadcast_to(a,(4,4))

輸出如下:

[[0  1  2  3]
 [0  1  2  3]
 [0  1  2  3]
 [0  1  2  3]]

numpy.expand_dims

函數(shù)通過在指定位置插入新的軸來擴展數(shù)組形狀。該函數(shù)需要兩個參數(shù):

numpy.expand_dims(arr, axis)

其中:

  • arr:輸入數(shù)組
  • axis:新軸插入的位置

例子

import numpy as np
x = np.array(([1,2],[3,4]))

print '數(shù)組 x:'
print x
print '\n'  
y = np.expand_dims(x, axis = 0)

print '數(shù)組 y:'
print y
print '\n'

print '數(shù)組 x 和 y 的形狀:'
print x.shape, y.shape
print '\n'  
# 在位置 1 插入軸
y = np.expand_dims(x, axis = 1)

print '在位置 1 插入軸之后的數(shù)組 y:'
print y
print '\n'  

print 'x.ndim 和 y.ndim:'
print x.ndim,y.ndim
print '\n'  

print 'x.shape 和 y.shape:'
print x.shape, y.shape

輸出如下:

數(shù)組 x:
[[1 2]
 [3 4]]

數(shù)組 y:
[[[1 2]
 [3 4]]]

數(shù)組 x 和 y 的形狀:
(2, 2) (1, 2, 2)

在位置 1 插入軸之后的數(shù)組 y:
[[[1 2]]
 [[3 4]]]

x.shape 和 y.shape:
2 3

x.shape and y.shape:
(2, 2) (2, 1, 2)

numpy.squeeze

函數(shù)從給定數(shù)組的形狀中刪除一維條目。 此函數(shù)需要兩個參數(shù)。

numpy.squeeze(arr, axis)

其中:

  • arr:輸入數(shù)組
  • axis:整數(shù)或整數(shù)元組,用于選擇形狀中單一維度條目的子集

例子

import numpy as np  
x = np.arange(9).reshape(1,3,3)

print '數(shù)組 x:'
print x
print '\n'  
y = np.squeeze(x)

print '數(shù)組 y:'
print y
print '\n'  

print '數(shù)組 x 和 y 的形狀:'
print x.shape, y.shape

輸出如下:

數(shù)組 x:
[[[0 1 2]
 [3 4 5]
 [6 7 8]]]

數(shù)組 y:
[[0 1 2]
 [3 4 5]
 [6 7 8]]

數(shù)組 x 和 y 的形狀:
(1, 3, 3) (3, 3)

數(shù)組的連接

序號 數(shù)組及描述
1. concatenate 沿著現(xiàn)存的軸連接數(shù)據(jù)序列
2. stack 沿著新軸連接數(shù)組序列
3. hstack 水平堆疊序列中的數(shù)組(列方向)
4. vstack 豎直堆疊序列中的數(shù)組(行方向)

numpy.concatenate

數(shù)組的連接是指連接。 此函數(shù)用于沿指定軸連接相同形狀的兩個或多個數(shù)組。 該函數(shù)接受以下參數(shù)。

numpy.concatenate((a1, a2, ...), axis)

其中:

  • a1, a2, ...:相同類型的數(shù)組序列
  • axis:沿著它連接數(shù)組的軸,默認為 0

例子

import numpy as np
a = np.array([[1,2],[3,4]])

print '第一個數(shù)組:'
print a
print '\n'  
b = np.array([[5,6],[7,8]])

print '第二個數(shù)組:'
print b
print '\n'  
# 兩個數(shù)組的維度相同

print '沿軸 0 連接兩個數(shù)組:'
print np.concatenate((a,b))
print '\n'  

print '沿軸 1 連接兩個數(shù)組:'
print np.concatenate((a,b),axis = 1)

輸出如下:

第一個數(shù)組:
[[1 2]
 [3 4]]

第二個數(shù)組:
[[5 6]
 [7 8]]

沿軸 0 連接兩個數(shù)組:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]

沿軸 1 連接兩個數(shù)組:
[[1 2 5 6]
 [3 4 7 8]]

numpy.stack

此函數(shù)沿新軸連接數(shù)組序列。 此功能添加自 NumPy 版本 1.10.0。 需要提供以下參數(shù)。

numpy.stack(arrays, axis)

其中:

  • arrays:相同形狀的數(shù)組序列
  • axis:返回數(shù)組中的軸,輸入數(shù)組沿著它來堆疊
import numpy as np
a = np.array([[1,2],[3,4]])

print '第一個數(shù)組:'
print a
print '\n'
b = np.array([[5,6],[7,8]])

print '第二個數(shù)組:'
print b
print '\n'  

print '沿軸 0 堆疊兩個數(shù)組:'
print np.stack((a,b),0)
print '\n'  

print '沿軸 1 堆疊兩個數(shù)組:'
print np.stack((a,b),1)

輸出如下:

第一個數(shù)組:
[[1 2]
 [3 4]]

第二個數(shù)組:
[[5 6]
 [7 8]]

沿軸 0 堆疊兩個數(shù)組:
[[[1 2]
 [3 4]]
 [[5 6]
 [7 8]]]

沿軸 1 堆疊兩個數(shù)組:
[[[1 2]
 [5 6]]
 [[3 4]
 [7 8]]]

numpy.hstack

numpy.stack函數(shù)的變體,通過堆疊來生成水平的單個數(shù)組。

例子

import numpy as np
a = np.array([[1,2],[3,4]])

print '第一個數(shù)組:'
print a
print '\n'  
b = np.array([[5,6],[7,8]])

print '第二個數(shù)組:'
print b
print '\n'  

print '水平堆疊:'
c = np.hstack((a,b))
print c
print '\n'

輸出如下:

第一個數(shù)組:
[[1 2]
 [3 4]]

第二個數(shù)組:
[[5 6]
 [7 8]]

水平堆疊:
[[1 2 5 6]
 [3 4 7 8]]

numpy.vstack

numpy.stack函數(shù)的變體,通過堆疊來生成豎直的單個數(shù)組。

import numpy as np
a = np.array([[1,2],[3,4]])

print '第一個數(shù)組:'
print a
print '\n'  
b = np.array([[5,6],[7,8]])

print '第二個數(shù)組:'
print b
print '\n'

print '豎直堆疊:'
c = np.vstack((a,b))
print c

輸出如下:

第一個數(shù)組:
[[1 2]
 [3 4]]

第二個數(shù)組:
[[5 6]
 [7 8]]

豎直堆疊:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]

數(shù)組分割

序號 數(shù)組及操作
1. split 將一個數(shù)組分割為多個子數(shù)組
2. hsplit 將一個數(shù)組水平分割為多個子數(shù)組(按列)
3. vsplit 將一個數(shù)組豎直分割為多個子數(shù)組(按行)

numpy.split

該函數(shù)沿特定的軸將數(shù)組分割為子數(shù)組。函數(shù)接受三個參數(shù):

numpy.split(ary, indices_or_sections, axis)

其中:

  • ary:被分割的輸入數(shù)組
  • indices_or_sections:可以是整數(shù),表明要從輸入數(shù)組創(chuàng)建的,等大小的子數(shù)組的數(shù)量。 如果此參數(shù)是一維數(shù)組,則其元素表明要創(chuàng)建新子數(shù)組的點。
  • axis:默認為 0

例子

import numpy as np
a = np.arange(9)

print '第一個數(shù)組:'
print a
print '\n'  

print '將數(shù)組分為三個大小相等的子數(shù)組:'
b = np.split(a,3)
print b
print '\n'  

print '將數(shù)組在一維數(shù)組中表明的位置分割:'
b = np.split(a,[4,7])
print b

輸出如下:

第一個數(shù)組:
[0 1 2 3 4 5 6 7 8]

將數(shù)組分為三個大小相等的子數(shù)組:
[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]

將數(shù)組在一維數(shù)組中表明的位置分割:
[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])]

numpy.hsplit

numpy.hsplitsplit()函數(shù)的特例,其中軸為 1 表示水平分割,無論輸入數(shù)組的維度是什么。

import numpy as np
a = np.arange(16).reshape(4,4)

print '第一個數(shù)組:'
print a
print '\n'  

print '水平分割:'
b = np.hsplit(a,2)
print b
print '\n'

輸出:

第一個數(shù)組:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]
 [12 13 14 15]]

水平分割:                                                         
[array([[ 0,  1],                                                             
       [ 4,  5],                                                              
       [ 8,  9],                                                              
       [12, 13]]), array([[ 2,  3],                                           
       [ 6,  7],                                                              
       [10, 11],                                                              
       [14, 15]])]

numpy.vsplit

numpy.vsplitsplit()函數(shù)的特例,其中軸為 0 表示豎直分割,無論輸入數(shù)組的維度是什么。下面的例子使之更清楚。

import numpy as np
a = np.arange(16).reshape(4,4)

print '第一個數(shù)組:'
print a
print '\n'

print '豎直分割:'
b = np.vsplit(a,2)
print b

輸出如下:

第一個數(shù)組:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]
 [12 13 14 15]]

豎直分割:                                                           
[array([[0, 1, 2, 3],                                                         
       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],                               
       [12, 13, 14, 15]])]

添加/刪除元素

序號 元素及描述
1. resize 返回指定形狀的新數(shù)組
2. append 將值添加到數(shù)組末尾
3. insert 沿指定軸將值插入到指定下標之前
4. delete 返回刪掉某個軸的子數(shù)組的新數(shù)組
5. unique 尋找數(shù)組內的唯一元素

numpy.resize

此函數(shù)返回指定大小的新數(shù)組。 如果新大小大于原始大小,則包含原始數(shù)組中的元素的重復副本。 該函數(shù)接受以下參數(shù)。

numpy.resize(arr, shape)

其中:

  • arr:要修改大小的輸入數(shù)組
  • shape:返回數(shù)組的新形狀

例子

import numpy as np
a = np.array([[1,2,3],[4,5,6]])

print '第一個數(shù)組:'
print a
print '\n'

print '第一個數(shù)組的形狀:'
print a.shape
print '\n'  
b = np.resize(a, (3,2))

print '第二個數(shù)組:'
print b
print '\n'  

print '第二個數(shù)組的形狀:'
print b.shape
print '\n'  
# 要注意 a 的第一行在 b 中重復出現(xiàn),因為尺寸變大了

print '修改第二個數(shù)組的大?。?#39;
b = np.resize(a,(3,3))
print b

輸出如下:

第一個數(shù)組:
[[1 2 3]
 [4 5 6]]

第一個數(shù)組的形狀:
(2, 3)

第二個數(shù)組:
[[1 2]
 [3 4]
 [5 6]]

第二個數(shù)組的形狀:
(3, 2)

修改第二個數(shù)組的大小:
[[1 2 3]
 [4 5 6]
 [1 2 3]]

numpy.append

此函數(shù)在輸入數(shù)組的末尾添加值。 附加操作不是原地的,而是分配新的數(shù)組。 此外,輸入數(shù)組的維度必須匹配否則將生成ValueError

函數(shù)接受下列函數(shù):

numpy.append(arr, values, axis)

其中:

  • arr:輸入數(shù)組
  • values:要向arr添加的值,比如和arr形狀相同(除了要添加的軸)
  • axis:沿著它完成操作的軸。如果沒有提供,兩個參數(shù)都會被展開。

例子

import numpy as np
a = np.array([[1,2,3],[4,5,6]])

print '第一個數(shù)組:'
print a
print '\n'  

print '向數(shù)組添加元素:'
print np.append(a, [7,8,9])
print '\n'  

print '沿軸 0 添加元素:'
print np.append(a, [[7,8,9]],axis = 0)
print '\n'  

print '沿軸 1 添加元素:'
print np.append(a, [[5,5,5],[7,8,9]],axis = 1)

輸出如下:

第一個數(shù)組:
[[1 2 3]
 [4 5 6]]

向數(shù)組添加元素:
[1 2 3 4 5 6 7 8 9]

沿軸 0 添加元素:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

沿軸 1 添加元素:
[[1 2 3 5 5 5]
 [4 5 6 7 8 9]]

numpy.insert

此函數(shù)在給定索引之前,沿給定軸在輸入數(shù)組中插入值。 如果值的類型轉換為要插入,則它與輸入數(shù)組不同。 插入沒有原地的,函數(shù)會返回一個新數(shù)組。 此外,如果未提供軸,則輸入數(shù)組會被展開。

insert()函數(shù)接受以下參數(shù):

numpy.insert(arr, obj, values, axis)

其中:

  • arr:輸入數(shù)組
  • obj:在其之前插入值的索引
  • values:要插入的值
  • axis:沿著它插入的軸,如果未提供,則輸入數(shù)組會被展開

例子

import numpy as np
a = np.array([[1,2],[3,4],[5,6]])

print '第一個數(shù)組:'
print a
print '\n'  

print '未傳遞 Axis 參數(shù)。 在插入之前輸入數(shù)組會被展開。'
print np.insert(a,3,[11,12])
print '\n'  
print '傳遞了 Axis 參數(shù)。 會廣播值數(shù)組來配輸入數(shù)組。'

print '沿軸 0 廣播:'
print np.insert(a,1,[11],axis = 0)
print '\n'  

print '沿軸 1 廣播:'
print np.insert(a,1,11,axis = 1)

numpy.delete

此函數(shù)返回從輸入數(shù)組中刪除指定子數(shù)組的新數(shù)組。 與insert()函數(shù)的情況一樣,如果未提供軸參數(shù),則輸入數(shù)組將展開。 該函數(shù)接受以下參數(shù):

Numpy.delete(arr, obj, axis)

其中:

  • arr:輸入數(shù)組
  • obj:可以被切片,整數(shù)或者整數(shù)數(shù)組,表明要從輸入數(shù)組刪除的子數(shù)組
  • axis:沿著它刪除給定子數(shù)組的軸,如果未提供,則輸入數(shù)組會被展開

例子

import numpy as np
a = np.arange(12).reshape(3,4)

print '第一個數(shù)組:'
print a
print '\n'  

print '未傳遞 Axis 參數(shù)。 在插入之前輸入數(shù)組會被展開。'
print np.delete(a,5)
print '\n'  

print '刪除第二列:'  
print np.delete(a,1,axis = 1)
print '\n'  

print '包含從數(shù)組中刪除的替代值的切片:'
a = np.array([1,2,3,4,5,6,7,8,9,10])
print np.delete(a, np.s_[::2])

輸出如下:

第一個數(shù)組:
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]

未傳遞 Axis 參數(shù)。 在插入之前輸入數(shù)組會被展開。
[ 0 1 2 3 4 6 7 8 9 10 11]

刪除第二列:
[[ 0 2 3]
 [ 4 6 7]
 [ 8 10 11]]

包含從數(shù)組中刪除的替代值的切片:
[ 2 4 6 8 10]

numpy.unique

此函數(shù)返回輸入數(shù)組中的去重元素數(shù)組。 該函數(shù)能夠返回一個元組,包含去重數(shù)組和相關索引的數(shù)組。 索引的性質取決于函數(shù)調用中返回參數(shù)的類型。

numpy.unique(arr, return_index, return_inverse, return_counts)

其中:

  • arr:輸入數(shù)組,如果不是一維數(shù)組則會展開
  • return_index:如果為true,返回輸入數(shù)組中的元素下標
  • return_inverse:如果為true,返回去重數(shù)組的下標,它可以用于重構輸入數(shù)組
  • return_counts:如果為true,返回去重數(shù)組中的元素在原數(shù)組中的出現(xiàn)次數(shù)

例子

import numpy as np
a = np.array([5,2,6,2,7,5,6,8,2,9])

print '第一個數(shù)組:'
print a
print '\n'  

print '第一個數(shù)組的去重值:'
u = np.unique(a)
print u
print '\n'  

print '去重數(shù)組的索引數(shù)組:'
u,indices = np.unique(a, return_index = True)
print indices
print '\n'  

print '我們可以看到每個和原數(shù)組下標對應的數(shù)值:'
print a
print '\n'  

print '去重數(shù)組的下標:'
u,indices = np.unique(a,return_inverse = True)
print u
print '\n'

print '下標為:'
print indices
print '\n'  

print '使用下標重構原數(shù)組:'
print u[indices]
print '\n'  

print '返回去重元素的重復數(shù)量:'
u,indices = np.unique(a,return_counts = True)
print u
print indices

輸出如下:

第一個數(shù)組:
[5 2 6 2 7 5 6 8 2 9]

第一個數(shù)組的去重值:
[2 5 6 7 8 9]

去重數(shù)組的索引數(shù)組:
[1 0 2 4 7 9]

我們可以看到每個和原數(shù)組下標對應的數(shù)值:
[5 2 6 2 7 5 6 8 2 9]

去重數(shù)組的下標:
[2 5 6 7 8 9]

下標為:
[1 0 2 0 3 1 2 4 0 5]

使用下標重構原數(shù)組:
[5 2 6 2 7 5 6 8 2 9]

返回唯一元素的重復數(shù)量:
[2 5 6 7 8 9]
 [3 2 2 1 1 1]