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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/14 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Numpy学习---Task03---数组的操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

task03---數組的操作

1)更改形狀

? 在對數組進行操作時,為了滿足格式和計算的要求通常會改變其形狀。

?`numpy.ndarray.shape`

? 表示數組的維度,返回一個元組,這個元組的長度就是維度的數目,即 `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`

將數組轉換為一維的迭代器,可以用for訪問數組每一個元素。

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]]

? ? ? ? 忽然發現在對x進行.flat函數處理后,y內保存的地址在經過for循環打印輸出一次之后無法再次實現打印輸出。緣由目前我還不是很清楚。需要向其他大佬請教一下,回頭解釋。

`numpy.ndarray.flatten([order='C'])`

將數組的副本轉換為一維數組,并返回。

  • 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'時返回的是視圖;
  • order=’F‘ 時是拷貝。
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'])`

  • ?在不更改數據的情況下為數組賦予新的形狀。

?`reshape()`函數當參數`newshape = [rows,-1]`時,將根據行數自動確定列數。

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對應元素也改變)

`reshape()`函數當參數`newshape = -1`時,表示將數組降為一維。

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`的別名,對索引數組很有用。

? ? ? ?很多工具包在進行計算時都會先判斷輸入數據的維度是否滿足要求,如果輸入數據達不到指定的維度時,可以使用`newaxis`參數來增加一個維度。

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`表示輸入的數組;
  • ? `axis`用于指定需要刪除的維度,但是指定的維度必須為單維度,否則將會報錯
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

? ? ? ? 在機器學習和深度學習中,通常算法的結果是可以表示向量的數組(即包含兩對或以上的方括號形式[[]]),如果直接利用這個數組進行畫圖可能顯示界面為空(見后面的示例)。我們可以利用`squeeze()`函數將表示向量的數組轉換為秩為1的數組,這樣利用 matplotlib 庫函數畫圖時,就可以正常的顯示結果了。

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)數組組合

`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是二維的,拼接后的結果也是二維的。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).?

操作數.ndim > 1時等價于np.concatenate((a1, a2, ...), axis=1), 操作數.ndim = 1時則可視作np.concatenate((a1, a2, ...), axis=0)

?

注:操作數為一維數組時.hstack并不完全等價于.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).

操作數.ndim > 1時等價于np.concatenate((a1, a2, ...), axis=1),操作數.ndim =1時等價于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)數組拆分

`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 為實數時,數組沿著axis=0軸均分成a段,不能均分時報錯 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,...] 為一維數組時,數組沿著axis=0軸按照 [:a1],[a1,a2],[a2:]切割為片段,若數組中實數超過axis=0軸上的最大索引,生產空數組片段。 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等價于參數axis=0的.split函數 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等價于參數axis=1的.split函數 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)數組展平

`numpy.tile(A, reps)`

  • Construct an array by repeating A the number of times given by reps.
  • 'tile'是平鋪或并列顯示的意思
#將原矩陣橫向、縱向地復制 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軸復制,實際上增加了行數。
  • `axis=1`,沿著x軸復制,實際上增加了列數。
  • `repeats`,可以為一個數,也可以為一個矩陣。
  • `axis=None`時就會flatten當前矩陣,實際上就是變成了一個行向量。
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)

#參數axis為默認值時會將arr展平后進行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]#參數values與arr中的元素數據類型不同時,values會自動轉換為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)

#參數axis為默認值時會將arr展平后進行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))

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。