網站首頁 編程語言 正文
數組新的shape屬性應該要與原來的配套,如果等于-1的話,那么Numpy會根據剩下的維度計算出數組的另外一個shape屬性值。
舉個例子:
x = np.array([[2, 0], [1, 1], [2, 3]])
指定新數組行為3,列為,2,則:
y = x.reshape(3,2)
y
Out[43]:
array([[2, 0],
[1, 1],
[2, 3]])
指定新數組列為1,則:
y = x.reshape(-1,1)
y
Out[34]:
array([[2],
[0],
[1],
[1],
[2],
[3]])
指定新數組列為2,則:
y = x.reshape(-1,2)
y
Out[37]:
array([[2, 0],
[1, 1],
[2, 3]])
指定新數組行為1,則:
y = x.reshape(1,-1)
y
Out[39]: array([[2, 0, 1, 1, 2, 3]])
指定新數組行為2,則:
y = x.reshape(2,-1)
y
Out[41]:
array([[2, 0, 1],
[1, 2, 3]])
numpy中reshape(-1,1)與reshape(1,-1)的作用
如果你的數據只有一個特征,可以用reshape(-1,1)改變你的數據形狀;或者如果你的數據只包含一個樣本,可以使用reshape(1,-1)來改變。
e = np.array([1]) #只包含一個數據
f = e.reshape(1,-1) #改變形狀,輸出f之后發現它已經變成了二維數據
import numpy as np
a = np.array([[1,2,3],[4,5,6]]) #是兩行三列的數據,二維
b = np.array([1,2]) #是一維數據
c = b.reshape(-1,1) #c已經變成了二維數據,變成了兩行一列
d = b.reshape(1,-1) #d變成了一行兩列的數據,
print('b.shape is {0}'.format(b.shape))
print(b)
print('c.shape is {0}'.format(c.shape))
print(c)
print('d.shape is {0},d array is {1}'.format(d.shape,d))
可以發現reshape(-1,1)是將一維數據在行上變化,而reshape(1,-1)是將一維數據在列上變化
原文鏈接:https://blog.csdn.net/qq_42804678/article/details/99062431
相關推薦
- 2022-08-07 Python繪制交通流折線圖詳情_python
- 2022-11-17 C#?使用原生?System.IO.Compression?實現?zip?的壓縮與解壓_C#教程
- 2022-12-15 Qt開發之使用socket實現遠程控制_C 語言
- 2022-08-23 C++詳解使用floor&ceil&round實現保留小數點后兩位_C 語言
- 2022-04-15 python中對正則表達式re包的簡單引用方式_python
- 2022-04-22 小程序和網站中無限滾動的實現
- 2022-07-06 ELK收集Nginx日志的項目實戰_nginx
- 2022-10-03 react中使用useEffect及踩坑記錄_React
- 最近更新
-
- 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同步修改后的遠程分支