国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
NumPy基礎(chǔ)教程(四)數(shù)組應(yīng)用

修改數(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:'C' -- 按行,'F' -- 按列,'A' -- 原順序,'k' -- 元素在內(nèi)存中的出現(xiàn)順序。
import numpy as npa = 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

numpy.ndarray.flat 是一個(gè)數(shù)組元素迭代器

import numpy as npa = np.arange(9).reshape(3,3) print ('原始數(shù)組:')for row in a: print (row) #對(duì)數(shù)組中每個(gè)元素都進(jìn)行處理,可以使用flat屬性,該屬性是一個(gè)數(shù)組元素迭代器:print ('迭代后的數(shù)組:')for element in a.flat: print (element)
原始數(shù)組:[0 1 2][3 4 5][6 7 8]迭代后的數(shù)組:012345678

numpy.ndarray.flatten

numpy.ndarray.flatten 返回一份數(shù)組拷貝,對(duì)拷貝所做的修改不會(huì)影響原始數(shù)組,格式如下:

ndarray.flatten(order='C')

參數(shù)說明:

  • order:'C' -- 按行,'F' -- 按列,'A' -- 原順序,'K' -- 元素在內(nèi)存中的出現(xiàn)順序。
import numpy as npa = np.arange(8).reshape(2,4)print ('原數(shù)組:')print (a)print ('\n')# 默認(rèn)按行print ('展開的數(shù)組:')print (a.flatten())print ('\n')print ('以 F 風(fēng)格順序展開的數(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 風(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)順序。

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'))
原數(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ù)組

numpy.transpose

numpy.transpose 函數(shù)用于對(duì)換數(shù)組的維度,格式如下:

numpy.transpose(arr, axes)

參數(shù)說明:

  • arr:要操作的數(shù)組
  • axes:整數(shù)列表,對(duì)應(yīng)維度,通常所有維度都會(huì)對(duì)換。
import numpy as npa = np.arange(12).reshape(3,4)print ('原數(shù)組:')print (a )print ('\n')print ('對(duì)換數(shù)組:')print (np.transpose(a))
原數(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:

import numpy as npa = np.arange(12).reshape(3,4)print ('原數(shù)組:')print (a)print ('\n')print ('轉(zhuǎn)置數(shù)組:')print (a.T)
原數(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)到特定位置。
import numpy as np# 創(chuàng)建了三維的 ndarraya = np.arange(8).reshape(2,2,2)print ('原數(shù)組:')print (a)print ('獲取數(shù)組中一個(gè)值:')print(np.where(a==6)) print(a[1,1,0]) # 為 6print ('\n')# 將軸 2 滾動(dòng)到軸 0(寬度到深度)print ('調(diào)用 rollaxis 函數(shù):')b = np.rollaxis(a,2,0)print (b)# 查看元素 a[1,1,0],即 6 的坐標(biāo),變成 [0, 1, 1]# 最后一個(gè) 0 移動(dòng)到最前面print(np.where(b==6)) print ('\n') # 將軸 2 滾動(dòng)到軸 1:(寬度到高度)print ('調(diào)用 rollaxis 函數(shù):')c = np.rollaxis(a,2,1)print (c)# 查看元素 a[1,1,0],即 6 的坐標(biāo),變成 [1, 0, 1]# 最后的 0 和 它前面的 1 對(duì)換位置print(np.where(c==6)) print ('\n')
原數(shù)組:[[[0 1]  [2 3]] [[4 5]  [6 7]]]獲取數(shù)組中一個(gè)值:(array([1]), array([1]), array([0]))6調(diào)用 rollaxis 函數(shù):[[[0 2]  [4 6]] [[1 3]  [5 7]]](array([0]), array([1]), array([1]))調(diào)用 rollaxis 函數(shù):[[[0 2]  [1 3]] [[4 6]  [5 7]]](array([1]), array([0]), array([1]))

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ù)
import numpy as npbr br# 創(chuàng)建了三維的 ndarraybra = np.arange(8).reshape(2,2,2)br brprint ('原數(shù)組:')brprint (a)brprint ('\n')br# 現(xiàn)在交換軸 0(深度方向)到軸 2(寬度方向)br brprint ('調(diào)用 swapaxes 函數(shù)后的數(shù)組:')brprint (np.swapaxes(a, 2, 0))
原數(shù)組:[[[0 1]  [2 3]] [[4 5]  [6 7]]]調(diào)用 swapaxes 函數(shù)后的數(shù)組:[[[0 4]  [2 6]] [[1 5]  [3 7]]]

修改數(shù)組維度

numpy.broadcast

numpy.broadcast 用于模仿廣播的對(duì)象,它返回一個(gè)對(duì)象,該對(duì)象封裝了將一個(gè)數(shù)組廣播到另一個(gè)數(shù)組的結(jié)果。

import numpy as npx = np.array([[1], [2], [3]])y = np.array([4, 5, 6]) # 對(duì) y 廣播 xb = np.broadcast(x,y) # 它擁有 iterator 屬性,基于自身組件的迭代器元組print ('對(duì) y 廣播 x:')r,c = b.itersbr # 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')br# 手動(dòng)使用 broadcast 將 x 與 y 相加b = np.broadcast(x,y)c = np.empty(b.shape)print ('手動(dòng)使用 broadcast 將 x 與 y 相加:')print (c.shapeprint ('\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)
對(duì) y 廣播 x:1 41 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)

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)))
原數(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ù)組形狀

numpy.expand_dims(arr, axis)

參數(shù)說明:

  • arr:輸入數(shù)組
  • axis:新軸插入的位置
import numpy as npx = 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.ndim 和 y.ndim:2 3x.shape 和 y.shape:(2, 2) (2, 1, 2)

numpy.squeeze

numpy.squeeze 函數(shù)從給定數(shù)組的形狀中刪除一維的條目

numpy.squeeze(arr, axis)

參數(shù)說明:

  • arr:輸入數(shù)組
  • axis:整數(shù)或整數(shù)元組,用于選擇形狀中一維條目的子集
import numpy as npx = 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ù)組

numpy.concatenate

numpy.concatenate 函數(shù)用于沿指定軸連接相同形狀的兩個(gè)或多個(gè)數(shù)組

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

參數(shù)說明:

  • a1, a2, ...:相同類型的數(shù)組
  • axis:沿著它連接數(shù)組的軸,默認(rèn)為 0
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))
第一個(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ù)組沿著它來堆疊
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))
第一個(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')
第一個(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ù)組。

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)brprint ('\n')print ('豎直堆疊:')c = np.vstack((a,b))print (c)
第一個(gè)數(shù)組:[[1 2] [3 4]]第二個(gè)數(shù)組:[[5 6] [7 8]]豎直堆疊:[[1 2] [3 4] [5 6] [7 8]]

分割數(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:設(shè)置沿著哪個(gè)方向進(jìn)行切分,默認(rèn)為 0,橫向切分,即水平方向。為 1 時(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)
第一個(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])]

axis 為 0 時(shí)在水平方向分割,axis 為 1 時(shí)在垂直方向分割:

import numpy as npa = np.arange(16).reshape(4, 4)print('第一個(gè)數(shù)組:')print(a)print('\n')print('默認(rèn)分割(0軸):')b = np.split(a,2)print(b)print('\n')print('沿水平方向分割:')c = np.split(a,2,1)print(c)print('\n')print('沿水平方向分割:')d= np.hsplit(a,2)print(d)
第一個(gè)數(shù)組:[[ 0  1  2  3] [ 4  5  6  7] [ 8  9 10 11] [12 13 14 15]]默認(rèn)分割(0軸):[array([[0, 1, 2, 3],       [4, 5, 6, 7]]), array([[ 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]])]沿水平方向分割:[array([[ 0,  1],       [ 4,  5],       [ 8,  9],       [12, 13]]), array([[ 2,  3],       [ 6,  7],       [10, 11],       [14, 15]])]

numpy.hsplit

numpy.hsplit 函數(shù)用于水平分割數(shù)組,通過指定要返回的相同形狀的數(shù)組數(shù)量來拆分原數(shù)組。

import numpy as npharr = np.floor(10 * np.random.random((2, 6)))print ('原array:')print(harr)print ('拆分后:')print(np.hsplit(harr, 3))
原array:[[4. 8. 9. 9. 3. 6.] [0. 7. 5. 9. 9. 6.]]拆分后:[array([[4., 8.],       [0., 7.]]), array([[9., 9.],       [5., 9.]]), array([[3., 6.],       [9., 6.]])]

numpy.vsplit

numpy.vsplit 沿著垂直軸分割,其分割方式與hsplit用法相同。

import numpy as npba = np.arange(16).reshape(4,4)print ('第一個(gè)數(shù)組:')print (a)print ('\n')print ('豎直分割:')b = np.vsplit(a,2)print (b)
第一個(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ù)組元素的添加與刪除

numpy.resize

numpy.resize 函數(shù)返回指定大小的新數(shù)組。

如果新數(shù)組大小大于原始大小,則包含原始數(shù)組中的元素的副本。

numpy.resize(arr, shape)

參數(shù)說明:

  • arr:要修改大小的數(shù)組
  • shape:返回?cái)?shù)組的新形狀
import numpy as npa = np.array([[1,2,3],[4,5,6]])print ('第一個(gè)數(shù)組:')print (a)print ('\n')print ('第一個(gè)數(shù)組的形狀:')print (a.shape)print ('\n')b = np.resize(a, (3,2))print ('第二個(gè)數(shù)組:')print (b)print ('\n')print ('第二個(gè)數(shù)組的形狀:')print (b.shape)print ('\n') # 要注意 a 的第一行在 b 中重復(fù)出現(xiàn),因?yàn)槌叽缱兇罅?span>print ('修改第二個(gè)數(shù)組的大小:')b = np.resize(a,(3,3))print (b)
第一個(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ù)組的大?。?span>[[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ù)要相同)。
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))
第一個(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ì)被展開
import numpy as npa = np.array([[1,2],[3,4],[5,6]])print ('第一個(gè)數(shù)組:')print (a)print ('\n')print ('未傳遞 Axis 參數(shù)。 在刪除之前輸入數(shù)組會(huì)被展開。')print (np.insert(a,3,[11,12]))print ('\n')print ('傳遞了 Axis 參數(shù)。 會(huì)廣播值數(shù)組來配輸入數(shù)組。')print ('沿軸 0 廣播:')print (np.insert(a,1,[11],axis = 0))print ('\n')print ('沿軸 1 廣播:')print (np.insert(a,1,11,axis = 1))
第一個(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ì)被展開
import numpy as npa = np.arange(12).reshape(3,4)print ('第一個(gè)數(shù)組:')print (a)print ('\n')print ('未傳遞 Axis 參數(shù)。 在插入之前輸入數(shù)組會(huì)被展開。')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]))
第一個(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ù)
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 (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)為:')print (indices)print ('\n')print ('使用下標(biāo)重構(gòu)原數(shù)組:')print (u[indices])print ('\n')print ('返回去重元素的重復(fù)數(shù)量:')u,indices = np.unique(a,return_counts = True)print (u)print (indices)
第一個(gè)數(shù)組:[5 2 6 2 7 5 6 8 2 9]第一個(gè)數(shù)組的去重值:[2 5 6 7 8 9]去重?cái)?shù)組的索引數(shù)組:[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)為:[1 0 2 0 3 1 2 4 0 5]使用下標(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]
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Python numpy的基本操作你一般人都不會(huì)
D02 Numpy常用函數(shù),如何優(yōu)雅的遍歷一個(gè)多維數(shù)組?
清晰易懂的Numpy進(jìn)階教程
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服