婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

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

熱門標簽:ai地圖標注 n400電話申請多少錢 如何在地圖標注文字 地圖標注推廣單頁 西藏快速地圖標注地點 廈門crm外呼系統如何 女王谷地圖標注 百應ai電銷機器人鄭州 長春人工智能電銷機器人官網

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

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)

輸出結果:

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

np.squeeze降低矩陣維度:

"""
 squeeze 函數:從數組的形狀中刪除單維度條目,即把shape中為1的維度去掉
 用法:numpy.squeeze(a,axis = None)
  1)a表示輸入的數組;
  2)axis用于指定需要刪除的維度,但是指定的維度必須為單維度,否則將會報錯;
  3)axis的取值可為None 或 int 或 tuple of ints, 可選。若axis為空,則刪除所有單維度的條目;
  4)返回值:數組
  5) 不會修改原數組;
"""
import numpy as np 
print("#" * 40, "原始數據", "#" * 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)

輸出結果:

######################################## 原始數據 ########################################
(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擴充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法的文章就介紹到這了,更多相關Numpy擴充矩陣維度和刪除維度內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

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

標簽:亳州 廊坊 拉薩 綿陽 黔東 興安盟 內江 渭南

巨人網絡通訊聲明:本文標題《詳解Numpy擴充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法》,本文關鍵詞  詳解,Numpy,擴充,矩陣,維度,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《詳解Numpy擴充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法》相關的同類信息!
  • 本頁收集關于詳解Numpy擴充矩陣維度(np.expand_dims, np.newaxis)和刪除維度(np.squeeze)的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 吉林市| 西宁市| 类乌齐县| 罗源县| 台江县| 乐清市| 兴和县| 永安市| 桦川县| 上杭县| 保定市| 石屏县| 新丰县| 临邑县| 宁武县| 东阳市| 隆尧县| 陇西县| 景东| 郎溪县| 阳泉市| 镇巴县| 犍为县| 浦城县| 布尔津县| 石景山区| 安吉县| 安塞县| 尼勒克县| 铅山县| 襄垣县| 甘肃省| 宜城市| 邢台市| 喀什市| 县级市| 招远市| 郴州市| 莲花县| 滦南县| 元氏县|