溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Numpy擴充矩陣維度和刪除維度的實現(xiàn)示例

發(fā)布時間:2021-03-22 12:30:28 來源:億速云 閱讀:627 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Numpy擴充矩陣維度和刪除維度的實現(xiàn)示例,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

在操作矩陣的時候,不同的接口對于矩陣的輸入維度要求不同,輸入可能為1-D,2-D,3-D等等。下面介紹一下使用Numpy進行矩陣維度變更的相關(guān)方法。主要包括以下幾種:

1、np.newaxis擴充矩陣維度

2、np.expand_dims擴充矩陣維度

3、np.squeeze刪除矩陣中維度大小為1的維度

np.newaxis,np.expand_dims擴充矩陣維度:

import numpy as np
 
x = np.arange(8).reshape(2, 4)
print(x.shape)
 
# 添加第0維,輸出shape -> (1, 2, 4)
x1 = x[np.newaxis, :]
print(x1.shape)
 
# 添加第1維, 輸出shape -> (2, 1, 4)
x2 = np.expand_dims(x, axis=1)
print(x2.shape)

輸出結(jié)果:

(2, 4)
(1, 2, 4)
(2, 1, 4)

np.squeeze降低矩陣維度:

"""
 squeeze 函數(shù):從數(shù)組的形狀中刪除單維度條目,即把shape中為1的維度去掉
 用法:numpy.squeeze(a,axis = None)
  1)a表示輸入的數(shù)組;
  2)axis用于指定需要刪除的維度,但是指定的維度必須為單維度,否則將會報錯;
  3)axis的取值可為None 或 int 或 tuple of ints, 可選。若axis為空,則刪除所有單維度的條目;
  4)返回值:數(shù)組
  5) 不會修改原數(shù)組;
"""
import numpy as np 
print("#" * 40, "原始數(shù)據(jù)", "#" * 40)
x = np.arange(10).reshape(1, 1, 10, 1)
print(x.shape)
print(x)
 
print("#" * 40, "去掉axis=0這個維度", "#" * 40)
x_squeeze_0 = np.squeeze(x, axis=0)
print(x_squeeze_0.shape, x_squeeze_0)
 
print("#" * 40, "去掉axis=3這個維度", "#" * 40)
x_squeeze_3 = np.squeeze(x, axis=3)
print(x_squeeze_3.shape, x_squeeze_3)
 
print("#" * 40, "去掉axis=0, axis=1這兩個維度", "#" * 40)
x_squeeze_0_1 = np.squeeze(x, axis=(0, 1))
print(x_squeeze_0_1.shape, x_squeeze_0_1)
 
print("#" * 40, "去掉所有1維的維度", "#" * 40)
x_squeeze = np.squeeze(x)
print(x_squeeze.shape, x_squeeze)
 
print("#" * 40, "去掉不是1維的維度,拋異常", "#" * 40)
try:
 x_squeeze = np.squeeze(x, axis=2)
 print(x_squeeze.shape, x_squeeze)
except Exception as e:
 print(e)

輸出結(jié)果:

######################################## 原始數(shù)據(jù) ########################################
(1, 1, 10, 1)
[[[[0]
   [1]
   [2]
   [3]
   [4]
   [5]
   [6]
   [7]
   [8]
   [9]]]]
######################################## 去掉axis=0這個維度 ########################################
(1, 10, 1) [[[0]
  [1]
  [2]
  [3]
  [4]
  [5]
  [6]
  [7]
  [8]
  [9]]]
######################################## 去掉axis=3這個維度 ########################################
(1, 1, 10) [[[0 1 2 3 4 5 6 7 8 9]]]
######################################## 去掉axis=0, axis=1這兩個維度 ########################################
(10, 1) [[0]
 [1]
 [2]
 [3]
 [4]
 [5]
 [6]
 [7]
 [8]
 [9]]
######################################## 去掉所有1維的維度 ########################################
(10,) [0 1 2 3 4 5 6 7 8 9]
######################################## 去掉不是1維的維度,拋異常 ########################################
cannot select an axis to squeeze out which has size not equal to one

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Numpy擴充矩陣維度和刪除維度的實現(xiàn)示例”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI