網站首頁 編程語言 正文
numpy.insert()主要用于向矩陣中插入行或列。對于多維矩陣,可以沿任意一個軸插入元素。
1. 參數說明
numpy.insert(arr, obj, values, axis=None)
arr:輸入矩陣,numpy.array類型。注意:該方法并不改變直接arr的值,而是返回一個
obj:索引,整數或整數串。例如可以只插入一行元素,也可以插入多行元素,多行可以是連續的(如第0行和第1行),也可以是分立的(如第2行和第4行)。
values:插入的值,numpy.array類型
axis:插入的軸,整型
返回值:得到的矩陣,numpy.array類型
2. 示例
2.1. 插入一列,值為標量
a = np.array([[1, 1], [2, 2], [3, 3]])
np.insert(a, 1, 5, axis=1)
結果
array([[1, 5, 1],
? ? ? ?[2, 5, 2],
? ? ? ?[3, 5, 3]])
2.2. 插入一列,值為一維矩陣
np.insert(a, [1], [[1],[2],[3]], axis=1)
2.3. 插入多列,值為標量
注意:索引值不能超過維度的最大索引,也就是說只能在某一維的中間插值,不能在末尾插入。
x = np.arange(8).reshape(2, 4)
idx = (1, 3)
np.insert(x, idx, 999, axis=1)
結果
array([[ ?0, 999, ? 1, ? 2, 999, ? 3],
? ? ? ?[ ?4, 999, ? 5, ? 6, 999, ? 7]])
2.4. 輸入為一維向量
在向量 [1,2,3,4] 的第1個元素前面的位置插入5
print(np.insert([1,2,3,4],1,5))
[1 5 2 3 4]
在向量 [1,2,3,4] 的第1個元素前面的位置插入5,第2個元素前面的位置插入7
print(np.insert([1,2,3,4],[1,2],[5,7]))
[1 5 2 7 3 4]
2.5. 輸入為矩陣
插入一整行
import numpy as np
a = a = np.array([[1, 1], [2, 2], [3, 3]])
print('a=',a)
print('after insertion:\n',np.insert(a,[1],[6,6],axis=0))
結果
a= [[1 1]
?[2 2]
?[3 3]]
after insertion:
?[[1 1]
?[6 6]
?[2 2]
?[3 3]]
參考文獻
numpy.insert — NumPy v1.24 Manual
原文鏈接:https://blog.csdn.net/Jinyindao243052/article/details/107227787
相關推薦
- 2022-06-23 Android?Socket實現多個客戶端聊天布局_Android
- 2023-01-14 C#實現啟動項管理的示例代碼_C#教程
- 2023-04-19 yarn : 無法加載文件 D:xx\yarn.ps1,因為在此系統上禁止運行腳本。有關詳細信息,請
- 2022-09-04 go語言中的面向對象_Golang
- 2022-11-07 react-native?實現漸變色背景過程_React
- 2022-11-30 詳解Golang中字符串的使用_Golang
- 2022-08-29 Python正則表達式?r'(.*)?are?(.*?)?.*'的深入理解_python
- 2022-09-12 python中的集合及集合常用的使用方法_python
- 最近更新
-
- 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同步修改后的遠程分支