B10_NumPy数组操作、修改数组形状、翻转数组、修改数组维度、连接数组、分割数组、数组元素的添加与删除
NumPy數(shù)組操作
Numpy 中包含了一些函數(shù)用于處理數(shù)組,大概可分為以下幾類:
- 修改數(shù)組形狀
- 翻轉(zhuǎn)數(shù)組
- 修改數(shù)組維度
- 連接數(shù)組
- 分割數(shù)組
- 數(shù)組元素的添加與刪除
修改數(shù)組形狀
| reshape | 不改變數(shù)據(jù)的條件下修改形狀 |
| flat | 數(shù)組元素迭代器 |
| flatten | 返回一份數(shù)組拷貝,對(duì)拷貝所做的修改不會(huì)影響原始數(shù)組 |
| ravel | 返回展開數(shù)組 |
numpy.reshape
numpy.reshape 函數(shù)可以在不改變數(shù)據(jù)的條件下修改形狀,格式如下: numpy.reshape(arr, newshape, order=‘C’)
- arr:要修改形狀的數(shù)組
- newshape:整數(shù)或者整數(shù)數(shù)組,新的形狀應(yīng)當(dāng)兼容原有形狀
- order:order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原順序,‘k’ – 元素在內(nèi)存中的出現(xiàn)順序。
實(shí)例:
import numpy as npa = np.arange(8) print('原始數(shù)組:') print(a) print('\n')b = a.reshape(4,2) print('修改后的數(shù)組:') print(b)輸出結(jié)果如下:
原始數(shù)組: [0 1 2 3 4 5 6 7] 修改后的數(shù)組: [[0 1][2 3][4 5][6 7]]numpy.ndarray.flat
numpy.ndarray.flat是一個(gè)數(shù)組元素迭代器,實(shí)例如下:
實(shí)例:
輸出結(jié)果如下:
原始數(shù)組: [0 1 2] [3 4 5] [6 7 8] 迭代后的數(shù)組: 0 1 2 3 4 5 6 7 8numpy.ndarray.flatten
numpy.ndarray.flatten 返回一份數(shù)組拷貝,對(duì)拷貝所做的修改不會(huì)影響原始數(shù)組,格式如下:
ndarray.flatten(order='C')參數(shù)說明:
order:‘C’–按行,‘F’–按列,‘A’–原順序,‘K’–元素在內(nèi)存中的出現(xiàn)順序。
輸出結(jié)果如下:
原數(shù)組: [[0 1 2 3][4 5 6 7]] 展開的數(shù)組: [0 1 2 3 4 5 6 7] 以 F 風(fēng)格順序展開的數(shù)組: [0 4 1 5 2 6 3 7]numpy.ravel
numpy.ravel() 展平的數(shù)組元素,順序通常是"C風(fēng)格",返回的是數(shù)組視圖(view,有點(diǎn)類似 C/C++引用reference的意味),修改會(huì)影響原始數(shù)組。
該函數(shù)接收兩個(gè)參數(shù):
numpy.ravel(a, order='C')參數(shù)說明:
order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原順序,‘K’ – 元素在內(nèi)存中的出現(xiàn)順序。
實(shí)例:
import numpy as npa = np.arange(8).reshape(2, 4)print('原數(shù)組:') print(a) print('\n')print('調(diào)用 ravel 函數(shù)之后:') print(a.ravel()) print('\n')print('以 F 風(fēng)格順序調(diào)用 ravel 函數(shù)之后:') print(a.ravel(order='F'))運(yùn)行結(jié)果:
原數(shù)組: [[0 1 2 3][4 5 6 7]] 調(diào)用 ravel 函數(shù)之后: [0 1 2 3 4 5 6 7] 以 F 風(fēng)格順序調(diào)用 ravel 函數(shù)之后: [0 4 1 5 2 6 3 7]翻轉(zhuǎn)數(shù)組:
| transpose | 對(duì)換數(shù)組的維度 |
| ndarray.T | 和self.transpose()相同 |
| rollaxis | 向后滾動(dòng)指定的軸 |
| swapaxes | 對(duì)換數(shù)組的兩個(gè)軸 |
numpy.transpose
numpy.transpose 函數(shù)用于對(duì)換數(shù)組的維度,格式如下:
numpy.transpose(arr, axes)參數(shù)說明:
- arr:要操作的數(shù)組
- axes:整數(shù)列表,對(duì)應(yīng)維度,通常所有維度都會(huì)對(duì)換。
實(shí)例:
輸出結(jié)果如下:
原數(shù)組: [[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]] 對(duì)換數(shù)組: [[ 0 4 8][ 1 5 9][ 2 6 10][ 3 7 11]]numpy.ndarray.T 類似 numpy.transpose:
實(shí)例:
輸出結(jié)果如下:
原數(shù)組: [[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]] 轉(zhuǎn)置數(shù)組: [[ 0 4 8][ 1 5 9][ 2 6 10][ 3 7 11]]numpy.rollaxis
numpy.rollaxis 函數(shù)向后滾動(dòng)特定的軸到一個(gè)特定位置,格式如下:
numpy.rollaxis(arr, axis, start)參數(shù)說明:
- arr:數(shù)組
- axis:要向后滾動(dòng)的軸,其它軸的相對(duì)位置不會(huì)改變
- start:默認(rèn)為零,表示完整的滾動(dòng)。會(huì)滾動(dòng)到特定位置。
實(shí)例:
import numpy as np# 創(chuàng)建了三維的 ndarray a = np.arange(8).reshape(2, 2, 2)print('原數(shù)組:') print(a) print('\n') # 將軸 2 滾動(dòng)到軸 0(寬度到深度)print('調(diào)用 rollaxis 函數(shù):') print(np.rollaxis(a, 2)) # 將軸 0 滾動(dòng)到軸 1:(寬度到高度) print('\n')print('調(diào)用 rollaxis 函數(shù):') print(np.rollaxis(a, 2, 1))輸出結(jié)果如下:
原數(shù)組: [[[0 1][2 3]][[4 5][6 7]]] 調(diào)用 rollaxis 函數(shù): [[[0 2][4 6]][[1 3][5 7]]] 調(diào)用 rollaxis 函數(shù): [[[0 2][1 3]][[4 6][5 7]]]numpy.swapaxes
numpy.swapaxes 函數(shù)用于交換數(shù)組的兩個(gè)軸,格式如下:
numpy.swapaxes(arr, axis1, axis2)- arr:輸入的數(shù)組
- axis1:對(duì)應(yīng)第一個(gè)軸的整數(shù)
- axis2:對(duì)應(yīng)第二個(gè)軸的整數(shù)
實(shí)例:
輸出結(jié)果如下:
原數(shù)組: [[[0 1][2 3]][[4 5][6 7]]] 調(diào)用swapaxes函數(shù)后的數(shù)組: [[[0 4][2 6]][[1 5][3 7]]]修改數(shù)組維度
| broadcast | 產(chǎn)生模仿廣播的對(duì)象 |
| broadcast_to | 將數(shù)組廣播到新形狀 |
| expand_dims | 擴(kuò)展數(shù)組的形狀 |
| squeeze | 從數(shù)組的形狀中刪除一維條目 |
numpy.broadcast
numpy.broadcast 用于模仿廣播的對(duì)象,它返回一個(gè)對(duì)象,該對(duì)象封裝了將一個(gè)數(shù)組廣播到另一個(gè)數(shù)組的結(jié)果。
該函數(shù)使用兩個(gè)數(shù)組作為輸入?yún)?shù),如下實(shí)例:
import numpy as npx = np.array([[1], [2], [3]]) y = np.array([4, 5, 6]) # 對(duì) y 廣播 x b = np.broadcast(x,y) # 它擁有 iterator 屬性,基于自身組件的迭代器元組print ('對(duì) y 廣播 x:') r,c = b.iters# Python3.x 為 next(context) ,Python2.x 為 context.next() print (next(r), next(c)) print (next(r), next(c)) print ('\n') # shape 屬性返回廣播對(duì)象的形狀print ('廣播對(duì)象的形狀:') print (b.shape) print ('\n') # 手動(dòng)使用 broadcast 將 x 與 y 相加 b = np.broadcast(x,y) c = np.empty(b.shape)print ('手動(dòng)使用 broadcast 將 x 與 y 相加:') print (c.shape) print ('\n') c.flat = [u + v for (u,v) in b]print ('調(diào)用 flat 函數(shù):') print (c) print ('\n') # 獲得了和 NumPy 內(nèi)建的廣播支持相同的結(jié)果print ('x 與 y 的和:') print (x + y)輸出結(jié)果為:
對(duì) y 廣播 x: 1 4 1 5廣播對(duì)象的形狀: (3, 3)手動(dòng)使用 broadcast 將 x 與 y 相加: (3, 3)調(diào)用 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
numpy.broadcast_to 函數(shù)將數(shù)組廣播到新形狀。它在原始數(shù)組上返回只讀視圖。 它通常不連續(xù)。 如果新形狀不符合 NumPy 的廣播規(guī)則,該函數(shù)可能會(huì)拋出ValueError。
numpy.broadcast_to(array, shape, subok)實(shí)例:
import numpy as npa = np.arange(4).reshape(1,4) print('原數(shù)組:') print(a) print('\n')print('調(diào)用broadcast_to函數(shù)之后:') print(np.broadcast_to(a,(4,4)))輸出結(jié)果為:
原數(shù)組: [[0 1 2 3]] 調(diào)用broadcast_to函數(shù)之后: [[0 1 2 3][0 1 2 3][0 1 2 3][0 1 2 3]]numpy.expand_dims
numpy.expand_dims 函數(shù)通過在指定位置插入新的軸來擴(kuò)展數(shù)組形狀,函數(shù)格式如下:
numpy.expand_dims(arr, axis)參數(shù)說明:
- arr:輸入數(shù)組
- axis:新軸插入的位置
輸出結(jié)果為:
數(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.ndim 和 y.ndim: 2 3x.shape 和 y.shape: (2, 2) (2, 1, 2)numpy.squeeze
numpy.squeeze 函數(shù)從給定數(shù)組的形狀中刪除一維的條目,函數(shù)格式如下:
numpy.squeeze(arr, axis)參數(shù)說明:
- arr:輸入數(shù)組
- axis:整數(shù)或整數(shù)元組,用于選擇形狀中一維條目的子集
輸出結(jié)果為:
數(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ù)組
| concatenate | 連接沿現(xiàn)有軸的數(shù)組序列 |
| stack | 沿著新的軸加入一系列數(shù)組。 |
| hstack | 水平堆疊序列中的數(shù)組(列方向) |
| vstack | 豎直堆疊序列中的數(shù)組 (行方向) |
numpy.concatenate
numpy.concatenate 函數(shù)用于沿指定軸連接相同形狀的兩個(gè)或多個(gè)數(shù)組,格式如下:
numpy.concatenate((a1, a2, ...), axis)參數(shù)說明:
- a1,a2,…:相同類型的數(shù)組
- axis:沿著它連接數(shù)組的軸,默認(rèn)為0
實(shí)例:
import numpy as npa = np.array([[1, 2], [3, 4]])print('第一個(gè)數(shù)組:') print(a) print('\n') b = np.array([[5, 6], [7, 8]])print('第二個(gè)數(shù)組:') print(b) print('\n') # 兩個(gè)數(shù)組的維度相同print('沿軸 0 連接兩個(gè)數(shù)組:') print(np.concatenate((a, b))) print('\n')print('沿軸 1 連接兩個(gè)數(shù)組:') print(np.concatenate((a, b), axis=1))輸出結(jié)果為:
第一個(gè)數(shù)組: [[1 2][3 4]]第二個(gè)數(shù)組: [[5 6][7 8]]沿軸 0 連接兩個(gè)數(shù)組: [[1 2][3 4][5 6][7 8]]沿軸 1 連接兩個(gè)數(shù)組: [[1 2 5 6][3 4 7 8]]numpy.stack
numpy.stack 函數(shù)用于沿新軸連接數(shù)組序列,格式如下:
numpy.stack(arrays, axis)參數(shù)說明:
- arrays相同形狀的數(shù)組序列
- axis:返回?cái)?shù)組中的軸,輸入數(shù)組沿著它來堆疊
實(shí)例:
import numpy as npa = np.array([[1,2],[3,4]])print ('第一個(gè)數(shù)組:') print (a) print ('\n') b = np.array([[5,6],[7,8]])print ('第二個(gè)數(shù)組:') print (b) print ('\n')print ('沿軸 0 堆疊兩個(gè)數(shù)組:') print (np.stack((a,b),0)) print ('\n')print ('沿軸 1 堆疊兩個(gè)數(shù)組:') print (np.stack((a,b),1))輸出結(jié)果如下:
第一個(gè)數(shù)組: [[1 2][3 4]]第二個(gè)數(shù)組: [[5 6][7 8]]沿軸 0 堆疊兩個(gè)數(shù)組: [[[1 2][3 4]][[5 6][7 8]]]沿軸 1 堆疊兩個(gè)數(shù)組: [[[1 2][5 6]][[3 4][7 8]]]numpy.hstack
numpy.hstack 是 numpy.stack 函數(shù)的變體,它通過水平堆疊來生成數(shù)組。
import numpy as npa = np.array([[1,2],[3,4]])print ('第一個(gè)數(shù)組:') print (a) print ('\n') b = np.array([[5,6],[7,8]])print ('第二個(gè)數(shù)組:') print (b) print ('\n')print ('水平堆疊:') c = np.hstack((a,b)) print (c) print ('\n')輸出結(jié)果如下:
第一個(gè)數(shù)組: [[1 2][3 4]] 第二個(gè)數(shù)組: [[5 6][7 8]] 水平堆疊: [[1 2 5 6][3 4 7 8]]numpy.vstack
numpy.vstack 是 numpy.stack 函數(shù)的變體,它通過垂直堆疊來生成數(shù)組。
實(shí)例:
輸出結(jié)果為:
第一個(gè)數(shù)組: [[1 2][3 4]]第二個(gè)數(shù)組: [[5 6][7 8]]豎直堆疊: [[1 2][3 4][5 6][7 8]]分割數(shù)組
| split | 將一個(gè)數(shù)組分割為多個(gè)子數(shù)組 |
| hsplit | 將一個(gè)數(shù)組水平分割為多個(gè)子數(shù)組(按列) |
| vsplit | 將一個(gè)數(shù)組垂直分割為多個(gè)子數(shù)組(按行) |
numpy.split
numpy.split 函數(shù)沿特定的軸將數(shù)組分割為子數(shù)組,格式如下:
numpy.split(ary, indices_or_sections, axis)參數(shù)說明:
- ary:被分割的數(shù)組
- indices_or_sections:果是一個(gè)整數(shù),就用該數(shù)平均切分,如果是一個(gè)數(shù)組,為沿軸切分的位置(左開右閉)
- axis:沿著哪個(gè)維度進(jìn)行切向,默認(rèn)為0,橫向切分。為1時(shí),縱向切分
實(shí)例:
import numpy as npa = np.arange(9)print ('第一個(gè)數(shù)組:') print (a) print ('\n')print ('將數(shù)組分為三個(gè)大小相等的子數(shù)組:') b = np.split(a,3) print (b) print ('\n')print ('將數(shù)組在一維數(shù)組中表明的位置分割:') b = np.split(a,[4,7]) print (b)輸出結(jié)果為:
第一個(gè)數(shù)組: [0 1 2 3 4 5 6 7 8] 將數(shù)組分為三個(gè)大小相等的子數(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.hsplit 函數(shù)用于水平分割數(shù)組,通過指定要返回的相同形狀的數(shù)組數(shù)量來拆分原數(shù)組。
import numpy as np# 生成一個(gè)2行,6列的數(shù)組 harr = np.floor(10 * np.random.random((2,6))) print('原array:') print(harr)print('拆分后:') print(np.hsplit(harr,3))輸出結(jié)果為:
原array: [[2. 2. 6. 4. 6. 7.][0. 9. 2. 8. 3. 5.]] 拆分后: [array([[2., 2.],[0., 9.]]), array([[6., 4.],[2., 8.]]), array([[6., 7.],[3., 5.]])]numpy.vsplit
numpy.vsplit 沿著垂直軸分割,其分割方式與hsplit用法相同。
import numpy as npa = np.arange(16).reshape(4,4)print('第一個(gè)數(shù)組:') print(a) print('\n')print('豎直分割:') b = np.vsplit(a,2) print(b)輸出結(jié)果為:
第一個(gè)數(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]])]數(shù)組元素的添加與刪除
| resize | 返回指定形狀的新數(shù)組 |
| append | 將值添加到數(shù)組末尾 |
| insert | 沿指定軸將值插入到指定下標(biāo)之前 |
| delete | 刪掉某個(gè)軸的子數(shù)組,并返回刪除后的新數(shù)組 |
| unique | 查找數(shù)組內(nèi)的唯一元素 |
numpy.resize
numpy.resize 函數(shù)返回指定大小的新數(shù)組。
如果新數(shù)組大小大于原始大小,則包含原始數(shù)組中的元素的副本。
參數(shù)說明:
- arr:要修改大小的數(shù)組
- shape:返回?cái)?shù)組的新形狀
輸出結(jié)果為:
第一個(gè)數(shù)組: [[1 2 3][4 5 6]] 第一個(gè)數(shù)組的形狀: (2, 3) 第二個(gè)數(shù)組: [[1 2][3 4][5 6]] 第二個(gè)數(shù)組的形狀: (3, 2) 修改第二個(gè)數(shù)組的大小: [[1 2 3][4 5 6][1 2 3]]numpy.append
numpy.append 函數(shù)在數(shù)組的末尾添加值。 追加操作會(huì)分配整個(gè)數(shù)組,并把原來的數(shù)組復(fù)制到新數(shù)組中。 此外,輸入數(shù)組的維度必須匹配否則將生成ValueError。
append 函數(shù)返回的始終是一個(gè)一維數(shù)組。
numpy.append(arr, values, axis=None)參數(shù)說明:
- arr:輸入數(shù)組
- values:要向arr添加的值,需要和arr形狀相同(除了要添加的軸)
- axis:默認(rèn)為 None。當(dāng)axis無定義時(shí),是橫向加成,返回總是為一維數(shù)組!當(dāng)axis有定義的時(shí)候,分別為0和1的時(shí)候。當(dāng)axis有定義的時(shí)候,分別為0和1的時(shí)候(列數(shù)要相同)。當(dāng)axis為1時(shí),數(shù)組是加在右邊(行數(shù)要相同)。
實(shí)例:
import numpy as npa = np.array([[1,2,3],[4,5,6]])print ('第一個(gè)數(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))輸出結(jié)果為:
第一個(gè)數(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
numpy.insert 函數(shù)在給定索引之前,沿給定軸在輸入數(shù)組中插入值。
如果值的類型轉(zhuǎn)換為要插入,則它與輸入數(shù)組不同。 插入沒有原地的,函數(shù)會(huì)返回一個(gè)新數(shù)組。 此外,如果未提供軸,則輸入數(shù)組會(huì)被展開。
numpy.insert(arr, obj, values, axis)參數(shù)說明:
- arr:輸入數(shù)組
- obj:在其之前插入值的索引
- values:要插入的值
- axis:沿著它插入的軸,如果未提供,則輸入數(shù)組會(huì)被展開
輸出結(jié)果如下:
第一個(gè)數(shù)組: [[1 2][3 4][5 6]]未傳遞 Axis 參數(shù)。 在插入之前輸入數(shù)組會(huì)被展開。 [ 1 2 3 11 12 4 5 6]傳遞了 Axis 參數(shù)。 會(huì)廣播值數(shù)組來配輸入數(shù)組。 沿軸 0 廣播: [[ 1 2][11 11][ 3 4][ 5 6]]沿軸 1 廣播: [[ 1 11 2][ 3 11 4][ 5 11 6]]numpy.delete
numpy.delete 函數(shù)返回從輸入數(shù)組中刪除指定子數(shù)組的新數(shù)組。 與 insert() 函數(shù)的情況一樣,如果未提供軸參數(shù),則輸入數(shù)組將展開。
Numpy.delete(arr,obj,axis)參數(shù)說明:
- arr:輸入數(shù)組
- obj:可以被切片,整數(shù)或者整數(shù)數(shù)組,表明要從輸入數(shù)組刪除的子數(shù)組
- axis:沿著它刪除給定子數(shù)組的軸,如果未提供,則輸入數(shù)組會(huì)被展開
實(shí)例:
輸出結(jié)果為:
第一個(gè)數(shù)組: [[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]未傳遞 Axis 參數(shù)。 在插入之前輸入數(shù)組會(huì)被展開。 [ 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
numpy.unique 函數(shù)用于去除數(shù)組中的重復(fù)元素。
numpy.unique(arr, return_index, return_inverse, return_counts)- arr:輸入數(shù)組,如果不是一維數(shù)組則會(huì)展開
- return_index:如果為true,返回新列表元素在舊列表中的位置(下標(biāo)),并以列表形式儲(chǔ)
- return_inverse:如果為true,返回舊列表元素在新列表中的位置(下標(biāo)),并以列表形式儲(chǔ)
- return_counts:如果為true,返回去重?cái)?shù)組中的元素在原數(shù)組中的出現(xiàn)次數(shù)
實(shí)例:
import numpy as npa = np.array([5,2,6,2,7,5,6,8,2,9])print('第一個(gè)數(shù)組:') print(a) print('\n')print('第一個(gè)數(shù)組的去重值:') u = np.unique(a) print(u) print('\n')print('去重?cái)?shù)組的索引數(shù)組:') u,indices = np.unique(a,return_index=True) print(u) #即:留下來的元素在原來數(shù)組中的值 print(indices) print('\n')print('我們可以看到每個(gè)和原數(shù)組下標(biāo)對(duì)應(yīng)的數(shù)值:') print(a) print('\n')print('去重?cái)?shù)組的下標(biāo):') u,indices = np.unique(a,return_inverse=True) print(u) print('\n')print('下標(biāo)為(表示:[5 2 6 2 7 5 6 8 2 9],在u中的位置):') print(indices) print('\n')print('使用下標(biāo)重構(gòu)原數(shù)組(通過u和在u中的坐標(biāo)重新構(gòu)造原來的數(shù)組):') print(u[indices])print('返回去重元素的重復(fù)數(shù)量:') u,indices = np.unique(a,return_counts=True) print(u) # 表示u中出現(xiàn)了重復(fù)了元素的次數(shù) print(indices)輸出結(jié)果為:
第一個(gè)數(shù)組: [5 2 6 2 7 5 6 8 2 9] 第一個(gè)數(shù)組的去重值: [2 5 6 7 8 9] 去重?cái)?shù)組的索引數(shù)組: [2 5 6 7 8 9] [1 0 2 4 7 9] 我們可以看到每個(gè)和原數(shù)組下標(biāo)對(duì)應(yīng)的數(shù)值: [5 2 6 2 7 5 6 8 2 9] 去重?cái)?shù)組的下標(biāo): [2 5 6 7 8 9] 下標(biāo)為(表示:[5 2 6 2 7 5 6 8 2 9],在u中的位置): [1 0 2 0 3 1 2 4 0 5] 使用下標(biāo)重構(gòu)原數(shù)組(通過u和在u中的坐標(biāo)重新構(gòu)造原來的數(shù)組): [5 2 6 2 7 5 6 8 2 9] 返回去重元素的重復(fù)數(shù)量: [2 5 6 7 8 9] [3 2 2 1 1 1]總結(jié)
以上是生活随笔為你收集整理的B10_NumPy数组操作、修改数组形状、翻转数组、修改数组维度、连接数组、分割数组、数组元素的添加与删除的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 进口芯片涨价20% 全球缺货愈演愈烈
- 下一篇: 强制执行影响信用卡吗