Numpy学习---Task03---数组的操作
task03---數(shù)組的操作
1)更改形狀
? 在對(duì)數(shù)組進(jìn)行操作時(shí),為了滿足格式和計(jì)算的要求通常會(huì)改變其形狀。
?`numpy.ndarray.shape`
? 表示數(shù)組的維度,返回一個(gè)元組,這個(gè)元組的長(zhǎng)度就是維度的數(shù)目,即 `ndim` 屬性(秩)。
import numpy as npx = np.array([1, 2, 9, 4, 5, 6, 7, 8]) print(x.shape) # (8,) x.shape = [2, 4] print(x) # [[1 2 9 4] # [5 6 7 8]]`numpy.ndarray.flat`
將數(shù)組轉(zhuǎn)換為一維的迭代器,可以用for訪問(wèn)數(shù)組每一個(gè)元素。
import numpy as npx = np.array([[11, 12, 13, 14, 15],[16, 17, 18, 19, 20],[21, 22, 23, 24, 25],[26, 27, 28, 29, 30],[31, 32, 33, 34, 35]]) y = x.flat print(y) # <numpy.flatiter object at 0x0000020F9BA10C60> for i in y:print(i, end=' ') # 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35y[3] = 0 print(end='\n') print(x) # [[11 12 13 0 15] # [16 17 18 19 20] # [21 22 23 24 25] # [26 27 28 29 30] # [31 32 33 34 35]]? ? ? ? 忽然發(fā)現(xiàn)在對(duì)x進(jìn)行.flat函數(shù)處理后,y內(nèi)保存的地址在經(jīng)過(guò)for循環(huán)打印輸出一次之后無(wú)法再次實(shí)現(xiàn)打印輸出。緣由目前我還不是很清楚。需要向其他大佬請(qǐng)教一下,回頭解釋。
`numpy.ndarray.flatten([order='C'])`
將數(shù)組的副本轉(zhuǎn)換為一維數(shù)組,并返回。
- ndarray.flatten(order='C')
- Return a copy of the array collapsed into one dimension.
Parameters:? order?: {‘C’, ‘F’, ‘A’, ‘K’}, optional
‘C’ means to flatten in row-major (C-style) order. ‘F’ means to flatten in column-major (Fortran- style) order. ‘A’ means to flatten in column-major order if?a?is Fortran?contiguous?in memory, row-major order otherwise. ‘K’ means to flatten?a?in the order the elements occur in memory. The default is ‘C’.
Returns: A copy of the input array, flattened to one dimension
.
`numpy.ravel(a, order='C')`
- Return a contiguous flattened array.
- order='C'時(shí)返回的是視圖;
- order=’F‘ 時(shí)是拷貝。
`numpy.reshape(a, newshape[, order='C'])`
- ?在不更改數(shù)據(jù)的情況下為數(shù)組賦予新的形狀。
?`reshape()`函數(shù)當(dāng)參數(shù)`newshape = [rows,-1]`時(shí),將根據(jù)行數(shù)自動(dòng)確定列數(shù)。
x = np.arange(10) y = np.reshape(x,[2,5]) print(y.dtype) print(y)#int32 #[[0 1 2 3 4] # [5 6 7 8 9]]y = np.reshape(x,[2,-1]) print(y)#[[0 1 2 3 4] # [5 6 7 8 9]]y = np.reshape(x,[-1,2]) print(y)#[[0 1] # [2 3] # [4 5] # [6 7] # [8 9]]y[0,0] = 10 print(x)#[10 1 2 3 4 5 6 7 8 9](改變x去reshape后y中的值,x對(duì)應(yīng)元素也改變)`reshape()`函數(shù)當(dāng)參數(shù)`newshape = -1`時(shí),表示將數(shù)組降為一維。
x = np.random.randint(1,100,[2,2,3]) print(x) y = np.reshape(x,-1) print(y)#[[[47 76 25] # [52 66 34]] # # [[29 76 56] # [25 67 60]]] #[47 76 25 52 66 34 29 76 56 25 67 60]2)更改維度
`numpy.newaxis = None`
- ?`None`的別名,對(duì)索引數(shù)組很有用。
? ? ? ?很多工具包在進(jìn)行計(jì)算時(shí)都會(huì)先判斷輸入數(shù)據(jù)的維度是否滿足要求,如果輸入數(shù)據(jù)達(dá)不到指定的維度時(shí),可以使用`newaxis`參數(shù)來(lái)增加一個(gè)維度。
x = np.array([1,2,3,4,5,6,7,8,9]) print(x.shape) print(x)#(9,) #[1 2 3 4 5 6 7 8 9]y = x[np.newaxis,:] print(y.shape) print(y)#(1, 9) #[[1 2 3 4 5 6 7 8 9]]y = x[:,np.newaxis] print(y.shape) print(y)#(9, 1) #[[1] # [2] # [3] # [4] # [5] # [6] # [7] # [8] # [9]]`numpy.squeeze(a, axis=None)`?
- ? `a`表示輸入的數(shù)組;
- ? `axis`用于指定需要?jiǎng)h除的維度,但是指定的維度必須為單維度,否則將會(huì)報(bào)錯(cuò)
? ? ? ? 在機(jī)器學(xué)習(xí)和深度學(xué)習(xí)中,通常算法的結(jié)果是可以表示向量的數(shù)組(即包含兩對(duì)或以上的方括號(hào)形式[[]]),如果直接利用這個(gè)數(shù)組進(jìn)行畫(huà)圖可能顯示界面為空(見(jiàn)后面的示例)。我們可以利用`squeeze()`函數(shù)將表示向量的數(shù)組轉(zhuǎn)換為秩為1的數(shù)組,這樣利用 matplotlib 庫(kù)函數(shù)畫(huà)圖時(shí),就可以正常的顯示結(jié)果了。
import matplotlib.pyplot as plt x = np.array([[1,1,2,3,5,8,13,21]]) print(x.shape) plt.plot(x) plt.show()(1, 8)
x = np.squeeze(x) print(x.shape) plt.plot(x) plt.show()(8,)
3)數(shù)組組合
`numpy.concatenate((a1, a2, ...), axis=0, out=None)`
- Join a sequence of arrays along an existing axis.
`numpy.stack(arrays, axis=0, out=None)`
- Join a sequence of arrays along a new axis.
`numpy.hstack(tup)`
- Stack arrays in sequence horizontally (column wise).?
操作數(shù).ndim > 1時(shí)等價(jià)于np.concatenate((a1, a2, ...), axis=1), 操作數(shù).ndim = 1時(shí)則可視作np.concatenate((a1, a2, ...), axis=0)
?
注:操作數(shù)為一維數(shù)組時(shí).hstack并不完全等價(jià)于.concatenate
a = np.hstack([np.array([1, 2, 3, 4]), 5]) print(a) # [1 2 3 4 5]a = np.concatenate([np.array([1, 2, 3, 4]), 5]) print(a)# all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)`numpy.vstack(tup)`
- Stack arrays in sequence vertically (row wise).
操作數(shù).ndim > 1時(shí)等價(jià)于np.concatenate((a1, a2, ...), axis=1),操作數(shù).ndim =1時(shí)等價(jià)于np.stack(arrays, axis=0, out=None)
x = np.array([1,2,3]) y = np.array([4,5,6])z = np.vstack([x,y]) print(z)#[[1 2 3] # [4 5 6]]z = np.stack([x,y]) print(z)#[[1 2 3] # [4 5 6]]x = np.random.randint(0,10,(3,3)) y = np.random.randint(0,10,(3,3)) print(x) print(y)#[[8 8 0] # [4 2 6] # [3 0 7]]#[[0 0 1] # [4 0 0] # [0 3 1]]z = np.vstack([x,y]) print(z)#[[8 8 0] # [4 2 6] # [3 0 7] # [0 0 1] # [4 0 0] # [0 3 1]]z = np.concatenate([x,y]) print(z)#[[8 8 0] # [4 2 6] # [3 0 7] # [0 0 1] # [4 0 0] # [0 3 1]]4)數(shù)組拆分
`numpy.split(ary, indices_or_sections, axis=0)`?
- Split an array into multiple sub-arrays as views into ary.
`numpy.vsplit(ary, indices_or_sections)`?
- Split an array into multiple sub-arrays vertically (row-wise).
`numpy.hsplit(ary, indices_or_sections)`
- Split an array into multiple sub-arrays horizontally (column-wise).
5)數(shù)組展平
`numpy.tile(A, reps)`
- Construct an array by repeating A the number of times given by reps.
- 'tile'是平鋪或并列顯示的意思
`numpy.repeat(a, repeats, axis=None)`?
- Repeat elements of an array.
- `axis=0`,沿著y軸復(fù)制,實(shí)際上增加了行數(shù)。
- `axis=1`,沿著x軸復(fù)制,實(shí)際上增加了列數(shù)。
- `repeats`,可以為一個(gè)數(shù),也可以為一個(gè)矩陣。
- `axis=None`時(shí)就會(huì)flatten當(dāng)前矩陣,實(shí)際上就是變成了一個(gè)行向量。
6)添加和刪除元素
numpy.insert(arr, obj, values, axis=None)
#參數(shù)axis為默認(rèn)值時(shí)會(huì)將arr展平后進(jìn)行insert操作x = np.array([[1,2],[3,4]]) print(x)#[[1 2] # [3 4]]y = np.insert(x,2,5) print(y)#[1 2 5 3 4]#參數(shù)values與arr中的元素?cái)?shù)據(jù)類型不同時(shí),values會(huì)自動(dòng)轉(zhuǎn)換為arr中元素的類型y = np.insert(x,2,5.0) print(y)#[1 2 5 3 4]#multiple insertionnsy = np.insert(x,[2,3],5) print(y)#[1 2 5 3 5 4]y = np.insert(x,[1,2],[5,6],axis=0) print(y)#[[1 2] # [5 6] # [3 4] # [5 6]]y = np.insert(x,[1,2],[5,6],axis=1) print(y)#[[1 5 2 6] # [3 5 4 6]]numpy.delete(arr, obj, axis=None)
#參數(shù)axis為默認(rèn)值時(shí)會(huì)將arr展平后進(jìn)行delete操作x = np.array([[1,2],[3,4]]) print(x)#[[1 2] # [3 4]]y = np.delete(x,1) print(y)#[1 3 4]y = np.delete(x,(1,2)) print(y)#[1 4]y = np.delete(x,1,axis=0) print(y)#[[1 2]]y = np.delete(x,1,axis=1) print(y)#[[1] # [3]]7)查找元素
`numpy.unique(ar, return_index=False, return_inverse=False,return_counts=False, axis=None)`?
- Find the unique elements of an array.
- return_index:the indices of the input array that give the unique values
- return_inverse:the indices of the unique array that reconstruct the input array
- return_counts:the number of times each unique value comes up in the input array
總結(jié)
以上是生活随笔為你收集整理的Numpy学习---Task03---数组的操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。