網站首頁 編程語言 正文
np.reshape()基本用法
常用于矩陣規格變換,將矩陣轉換為特定的行和列的矩陣
格式:a1.reshape(x,y,z,…)
注意:將矩陣a1轉變成(x, y,z,…)---->一維長度x,二維長度y,三維長度z,…的矩陣。
場景:matlibplot畫圖時x、y軸需要傳入的是一維,可以用reshape()實現;再例如需要將多維的變成行向量或列向量時也經常要用
numpy.reshape(a, newshape, order='C')[source],參數`newshape`是啥意思?
根據Numpy文檔(https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html#numpy-reshape)的解釋:
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, **the value is inferred from the length of the array and remaining dimensions**.
大意是說,數組新的shape屬性應該要與原來的配套,如果等于-1的話,那么Numpy會根據剩下的維度計算出數組的另外一個shape屬性值。
舉幾個例子或許就清楚了,有一個數組z,它的shape屬性是(4, 4)
z = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
z.shape
(4, 4)
z.reshape(-1)
z.reshape(-1)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
z.reshape(-1, 1)
也就是說,先前我們不知道z的shape屬性是多少,但是想讓z變成只有一列,行數不知道多少,通過`z.reshape(-1,1)`,Numpy自動計算出有12行,新的數組shape屬性為(16, 1),與原來的(4, 4)配套。
z.reshape(-1,1)
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12],
[13],
[14],
[15],
[16]])
z.reshape(-1, 2)
newshape等于-1,列數等于2,行數未知,reshape后的shape等于(8, 2)
z.reshape(-1, 2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12],
[13, 14],
[15, 16]])
同理,只給定行數,newshape等于-1,Numpy也可以自動計算出新數組的列數。
原文鏈接:https://lujinyu.blog.csdn.net/article/details/78966192
相關推薦
- 2022-02-27 Web server failed to start. Port XXXX was already
- 2022-10-15 C語言利用UDP實現群聊聊天室的示例代碼_C 語言
- 2022-07-19 詳解的wc find xargs zip gzip bzip2 xz tar sftp命令或者協議
- 2022-09-18 Python中np.linalg.norm()用法實例總結_python
- 2022-09-05 Shiro 和 Spring Security 的比較
- 2022-09-09 python實現0到1之間的隨機數方式_python
- 2024-02-01 webstorm中Line comment at first column,Block commen
- 2022-07-13 Linux下網絡配置和ifconfig命令的使用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支