佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 詳解Numpy擴(kuò)充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法

詳解Numpy擴(kuò)充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法

熱門標(biāo)簽:呼叫中心市場需求 百度AI接口 客戶服務(wù) 語音系統(tǒng) Win7旗艦版 硅谷的囚徒呼叫中心 電話運(yùn)營中心 企業(yè)做大做強(qiáng)

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

1、np.newaxis擴(kuò)充矩陣維度

2、np.expand_dims擴(kuò)充矩陣維度

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

np.newaxis,np.expand_dims擴(kuò)充矩陣維度:

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用于指定需要?jiǎng)h除的維度,但是指定的維度必須為單維度,否則將會(huì)報(bào)錯(cuò);
  3)axis的取值可為None 或 int 或 tuple of ints, 可選。若axis為空,則刪除所有單維度的條目;
  4)返回值:數(shù)組
  5) 不會(huì)修改原數(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這個(gè)維度", "#" * 40)
x_squeeze_0 = np.squeeze(x, axis=0)
print(x_squeeze_0.shape, x_squeeze_0)
 
print("#" * 40, "去掉axis=3這個(gè)維度", "#" * 40)
x_squeeze_3 = np.squeeze(x, axis=3)
print(x_squeeze_3.shape, x_squeeze_3)
 
print("#" * 40, "去掉axis=0, axis=1這兩個(gè)維度", "#" * 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這個(gè)維度 ########################################
(1, 10, 1) [[[0]
  [1]
  [2]
  [3]
  [4]
  [5]
  [6]
  [7]
  [8]
  [9]]]
######################################## 去掉axis=3這個(gè)維度 ########################################
(1, 1, 10) [[[0 1 2 3 4 5 6 7 8 9]]]
######################################## 去掉axis=0, axis=1這兩個(gè)維度 ########################################
(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

參考鏈接

到此這篇關(guān)于詳解Numpy擴(kuò)充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法的文章就介紹到這了,更多相關(guān)Numpy擴(kuò)充矩陣維度和刪除維度內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • numpy的sum函數(shù)的axis和keepdim參數(shù)詳解
  • numpy np.newaxis 的實(shí)用分享
  • numpy:np.newaxis 實(shí)現(xiàn)將行向量轉(zhuǎn)換成列向量
  • np.newaxis 實(shí)現(xiàn)為 numpy.ndarray(多維數(shù)組)增加一個(gè)軸
  • NumPy中的維度Axis詳解
  • 淺談numpy 函數(shù)里面的axis參數(shù)的含義

標(biāo)簽:喀什 濟(jì)南 山西 長沙 崇左 安康 海南 山西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解Numpy擴(kuò)充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    武川县| 报价| 南平市| 兖州市| 平南县| 正安县| 甘谷县| 全椒县| 陇西县| 长兴县| 墨玉县| 吉林市| 丰城市| 鹤山市| 万盛区| 凤山市| 茌平县| 双峰县| 江西省| 泰顺县| 同江市| 衡阳市| 志丹县| 图木舒克市| 铁岭市| 秦皇岛市| 余姚市| 康保县| 江都市| 包头市| 礼泉县| 丰都县| 连云港市| 苏尼特左旗| 临漳县| 贵溪市| 砚山县| 延吉市| 河曲县| 辽阳县| 商丘市|