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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3-numpy 数组操作--修改数组形状、翻转数组、修改数组维度、连接数组、分割数组、数组元素的添加与删除

發(fā)布時間:2024/9/27 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3-numpy 数组操作--修改数组形状、翻转数组、修改数组维度、连接数组、分割数组、数组元素的添加与删除 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、修改數(shù)組形狀

函數(shù)描述
reshape不改變數(shù)據(jù)的條件下修改形狀
flat數(shù)組元素迭代器
flatten返回一份數(shù)組拷貝,對拷貝所做的修改不會影響原始數(shù)組
ravel返回展開數(shù)組

1.1 numpy.reshape

numpy.reshape 函數(shù)可以在不改變數(shù)據(jù)的條件下修改形狀,格式如下: numpy.reshape(arr, newshape, order=‘C’)

  • arr:要修改形狀的數(shù)組
  • newshape:整數(shù)或者整數(shù)數(shù)組,新的形狀應當兼容原有形狀
  • order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原順序,‘k’ – 元素在內(nèi)存中的出現(xiàn)順序。
import numpy as npa = np.arange(8) print('原始數(shù)組 : ') print(a) # [0 1 2 3 4 5 6 7] 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]] """

1.2 numpy.ndarray.flat

numpy.ndarray.flat 是一個數(shù)組元素迭代器,實例如下:

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

1.3 numpy.ndarray.flatten

numpy.ndarray.flatten 返回一份數(shù)組拷貝,對拷貝所做的修改不會影響原始數(shù)組,格式如下:
ndarray.flatten(order=‘C’)
參數(shù)說明:
order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原順序,‘K’ – 元素在內(nèi)存中的出現(xiàn)順序。

a = np.arange(8).reshape(2, 4) print('原數(shù)組:') print(a) print('\n') # 默認按行 print('展開的數(shù)組:') print(a.flatten()) print('\n') print('以 F 風格順序展開的數(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 風格順序展開的數(shù)組: [0 4 1 5 2 6 3 7] """

1.4 numpy.ravel

numpy.ravel() 展平的數(shù)組元素,順序通常是"C風格",返回的是數(shù)組視圖,修改會影響原始數(shù)組
該函數(shù)接收兩個參數(shù):
numpy.ravel(a, order=‘C’)
參數(shù)說明:
order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原順序,‘K’ – 元素在內(nèi)存中的出現(xiàn)順序。

a = np.arange(8).reshape(2, 4) print('原數(shù)組:') print(a) print('\n') print('調(diào)用 ravel 函數(shù)之后:') x = a.ravel() print(x) print('\n') x[1] = 100 print(x) print(a) """ 原數(shù)組: [[0 1 2 3][4 5 6 7]]調(diào)用 ravel 函數(shù)之后: [0 1 2 3 4 5 6 7][ 0 100 2 3 4 5 6 7] [[ 0 100 2 3][ 4 5 6 7]] """

發(fā)現(xiàn)一個問題a.ravel(order=‘F’) 時修改數(shù)據(jù)不會影響原始數(shù)據(jù)

a = np.arange(8).reshape(2, 4)print('原數(shù)組:') print(a) print('\n')print('調(diào)用 ravel 函數(shù)之后:') x = a.ravel(order = 'F') print(x) print('\n') x[1] = 100 print(x) print(a) """ 原數(shù)組: [[0 1 2 3][4 5 6 7]] 調(diào)用 ravel 函數(shù)之后: [0 4 1 5 2 6 3 7] [ 0 100 1 5 2 6 3 7] [[0 1 2 3][4 5 6 7]] """

2、翻轉(zhuǎn)數(shù)組

函數(shù)描述
transpose對換數(shù)組的維度
ndarray.T對換數(shù)組的維度,和 self.transpose() 相同
rollaxis向后滾動指定的軸
swapaxes對換數(shù)組的兩個軸

2.1 numpy.transpose

numpy.transpose 函數(shù)用于對換數(shù)組的維度,格式如下:
numpy.transpose(arr, axes)
參數(shù)說明:
arr:要操作的數(shù)組
axes:整數(shù)列表,對應維度,通常所有維度都會對換。

a = np.arange(12).reshape(3,4) print('原數(shù)組: ') print(a) print('對換數(shù)組: ') print(np.transpose(a)) """ 原數(shù)組: [[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]] 對換數(shù)組: [[ 0 4 8][ 1 5 9][ 2 6 10][ 3 7 11]] """

2.2 ndarray.T

numpy.ndarray.T 類似 numpy.transpose:

a = 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]] """

2.3 numpy.rollaxis

numpy.rollaxis 函數(shù)向后滾動特定的軸到一個特定位置,格式如下:
numpy.rollaxis(arr, axis, start)
參數(shù)說明:
arr:數(shù)組
axis:要向后滾動的軸,其它軸的相對位置不會改變
start:默認為零,表示完整的滾動。會滾動到特定位置。

https://zhuanlan.zhihu.com/p/162874970

2.4 numpy.swapaxes

numpy.swapaxes 函數(shù)用于交換數(shù)組的兩個軸,格式如下:
numpy.swapaxes(arr, axis1, axis2)
arr:輸入的數(shù)組
axis1:對應第一個軸的整數(shù)
axis2:對應第二個軸的整數(shù)

https://zhuanlan.zhihu.com/p/162874970

3、修改數(shù)組維度

https://www.runoob.com/numpy/numpy-array-manipulation.html

4、 連接數(shù)組

https://www.runoob.com/numpy/numpy-array-manipulation.html

5、 分割數(shù)組

https://www.runoob.com/numpy/numpy-array-manipulation.html

6、 數(shù)組元素的添加與刪除

https://www.runoob.com/numpy/numpy-array-manipulation.html

https://www.runoob.com/numpy/numpy-array-manipulation.html

總結(jié)

以上是生活随笔為你收集整理的python3-numpy 数组操作--修改数组形状、翻转数组、修改数组维度、连接数组、分割数组、数组元素的添加与删除的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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