日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Numpy学习---Task03---数组的操作

發(fā)布時(shí)間:2023/12/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Numpy学习---Task03---数组的操作 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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
.

x = np.random.randint(1,100,[5,1,5]) print(x) y = x.flatten() print(y)''' [[[86 89 92 24 55]][[35 75 94 54 92]][[51 52 62 92 50]][[23 81 77 57 88]][[16 69 74 25 9]]] [86 89 92 24 55 35 75 94 54 92 51 52 62 92 50 23 81 77 57 88 16 69 74 259] '''y[3] = 0 print(x)''' [[[86 89 92 24 55]][[35 75 94 54 92]][[51 52 62 92 50]][[23 81 77 57 88]][[16 69 74 25 9]]] '''y = x.flatten(order='F') print(y)''' [86 35 51 23 16 89 75 52 81 69 92 94 62 77 74 24 54 92 57 25 55 92 50 889] '''

`numpy.ravel(a, order='C')`

  • Return a contiguous flattened array.
  • order='C'時(shí)返回的是視圖;
  • order=’F‘ 時(shí)是拷貝。
y = x.ravel() print(y) y[3] = 0 print(x)''' [86 89 92 24 55 35 75 94 54 92 51 52 62 92 50 23 81 77 57 88 16 69 74 259] [[[86 89 92 0 55]][[35 75 94 54 92]][[51 52 62 92 50]][[23 81 77 57 88]][[16 69 74 25 9]]] '''y = x.ravel(order='F') print(y) y[1] = 0 print(x)''' [86 35 51 23 16 89 75 52 81 69 92 94 62 77 74 0 54 92 57 25 55 92 50 889] [[[86 89 92 0 55]][[35 75 94 54 92]][[51 52 62 92 50]][[23 81 77 57 88]][[16 69 74 25 9]]] '''

`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ò)
import numpy as np x = np.arange(10) print(x.shape)#(10,)x = x[np.newaxis,:] print(x.shape)#(1, 10)x = np.squeeze(x) print(x.shape)#(10,)x = np.random.randint(0,100,(1,3,5,1,7)) print(x.shape)#(1, 3, 5, 1, 7)y = np.squeeze(x) print(y.shape)#(3, 5, 7)y = np.squeeze(x,0) print(y.shape)#(3, 5, 1, 7)y = np.squeeze(x,1)#ValueError: cannot select an axis to squeeze out which has size not equal to one

? ? ? ? 在機(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.
x = np.array([1,2,3,4]) y = np.array([2,3,4,5]) z = np.concatenate([x,y]) print(z)#[1 2 3 4 2 3 4 5]# x,y是二維的,拼接后的結(jié)果也是二維的。x = x.reshape(1,4) y = y.reshape(1,4) z = np.concatenate([x,y]) print(z)#[[1 2 3 4] # [2 3 4 5]]z = np.concatenate([x,y],axis=0) print(z)#[[1 2 3 4] # [2 3 4 5]]z = np.concatenate([x,y],axis=1) print(z)#[[1 2 3 4 2 3 4 5]]

`numpy.stack(arrays, axis=0, out=None)`

  • Join a sequence of arrays along a new axis.
x = np.array([1,2,3,4]) y = np.array([2,3,4,5]) z = np.stack([x,y]) print(z.shape) print(z)#(2, 4) #[[1 2 3 4] # [2 3 4 5]]z = np.stack([x,y],axis=0) print(z.shape) print(z)#(2, 4) #[[1 2 3 4] # [2 3 4 5]]z = np.stack([x,y],axis=1) print(z.shape) print(z)#(4, 2) #[[1 2] # [2 3] # [3 4] # [4 5]]x = x.reshape(1,4) y = y.reshape(1,4) z = np.stack([x,y]) print(z.shape) print(z)#(2, 1, 4) #[[[1 2 3 4]] # # [[2 3 4 5]]]z = np.stack([x,y],axis=0) print(z.shape) print(z)#(2, 1, 4) #[[[1 2 3 4]] # # [[2 3 4 5]]]z = np.stack([x,y],axis=1) print(z.shape) print(z)#(1, 2, 4) #[[[1 2 3 4] # [2 3 4 5]]]

`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.
x = np.arange(12,24).reshape(3,-1) print(x)#[[12 13 14 15] # [16 17 18 19] # [20 21 22 23]]#indices_or_sections=a 為實(shí)數(shù)時(shí),數(shù)組沿著axis=0軸均分成a段,不能均分時(shí)報(bào)錯(cuò) y = np.split(x,3) print(y)#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19]]), array([[20, 21, 22, 23]])]y = np.split(x,2)#ValueError: array split does not result in an equal division#indices_or_sections=a[a1,a2,...] 為一維數(shù)組時(shí),數(shù)組沿著axis=0軸按照 [:a1],[a1,a2],[a2:]切割為片段,若數(shù)組中實(shí)數(shù)超過(guò)axis=0軸上的最大索引,生產(chǎn)空數(shù)組片段。 y = np.split(x,[1,3]) print(y)#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19], # [20, 21, 22, 23]]), array([], shape=(0, 4), dtype=int32)]y = np.split(x,[1,3],axis=1) print(y)#[array([[12], # [16], # [20]]), array([[13, 14], # [17, 18], # [21, 22]]), array([[15], # [19], # [23]])]

`numpy.vsplit(ary, indices_or_sections)`?

  • Split an array into multiple sub-arrays vertically (row-wise).
#.vsplit等價(jià)于參數(shù)axis=0的.split函數(shù) x = np.arange(12,24).reshape(3,-1) print(x)#[[12 13 14 15] # [16 17 18 19] # [20 21 22 23]]z = np.vsplit(x,3) print(z)#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19]]), array([[20, 21, 22, 23]])]z = np.split(x,3) print(z)#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19]]), array([[20, 21, 22, 23]])]z = np.vsplit(x,[1,3]) print(z)#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19], # [20, 21, 22, 23]]), array([], shape=(0, 4), dtype=int32)]= np.split(x,[1,3]) print(z)#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19], # [20, 21, 22, 23]]), array([], shape=(0, 4), dtype=int32)]

`numpy.hsplit(ary, indices_or_sections)`

  • Split an array into multiple sub-arrays horizontally (column-wise).
#.hsplit等價(jià)于參數(shù)axis=1的.split函數(shù) x = np.arange(12,24).reshape(3,-1) print(x)#[[12 13 14 15] # [16 17 18 19] # [20 21 22 23]]z = np.hsplit(x,3) print(z)#[array([[12, 13], # [16, 17], # [20, 21]]), array([[14, 15], # [18, 19], # [22, 23]])]z = np.hsplit(x,[1,3]) print(z)#[array([[12], # [16], # [20]]), array([[13, 14], # [17, 18], # [21, 22]]), array([[15], # [19], # [23]])]z = np.split(x,[1,3],axis=1) print(z) #[array([[12], # [16], # [20]]), array([[13, 14], # [17, 18], # [21, 22]]), array([[15], # [19], # [23]])]

5)數(shù)組展平

`numpy.tile(A, reps)`

  • Construct an array by repeating A the number of times given by reps.
  • 'tile'是平鋪或并列顯示的意思
#將原矩陣橫向、縱向地復(fù)制 x = np.array([[1,2],[3,4]]) print(x)#[[1 2] # [3 4]]y = np.tile(x,(1,3)) print(y)#[[1 2 1 2 1 2] # [3 4 3 4 3 4]]y = np.tile(x,(3,1)) print(y)#[[1 2] # [3 4] # [1 2] # [3 4] # [1 2] # [3 4]]y = np.tile(x,(3,3)) print(y)#[[1 2 1 2 1 2] # [3 4 3 4 3 4] # [1 2 1 2 1 2] # [3 4 3 4 3 4] # [1 2 1 2 1 2] # [3 4 3 4 3 4]]

`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è)行向量。
x = np.repeat(3,4) print(x)#[3 3 3 3]x = np.array([[1,2],[3,4]]) print(x)#[[1 2] # [3 4]]y = np.repeat(x,2) print(y)#[1 1 2 2 3 3 4 4]y = np.repeat(x,2,axis=0) print(y)#[[1 2] # [1 2] # [3 4] # [3 4]]y = np.repeat(x,2,axis=1) print(y)#[[1 1 2 2] # [3 3 4 4]]y = np.repeat(x,[2,3],axis=0) print(y)#[[1 2] # [1 2] # [3 4] # [3 4] # [3 4]]y = np.repeat(x,[2,3],axis=1) print(y)#[[1 1 2 2 2] # [3 3 4 4 4]]

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
a=np.array([1,1,2,3,3,4,4]) b=np.unique(a) print(b)#[1 2 3 4]b = np.unique(a,return_index=True) print(b)#(array([1, 2, 3, 4]), array([0, 2, 3, 5], dtype=int64))b = np.unique(a,return_inverse=True) print(b)#(array([1, 2, 3, 4]), array([0, 0, 1, 2, 2, 3, 3], dtype=int64))b = np.unique(a,return_counts=True) print(b)#(array([1, 2, 3, 4]), array([2, 1, 2, 2], dtype=int64))

總結(jié)

以上是生活随笔為你收集整理的Numpy学习---Task03---数组的操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。