-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from Jingjing092/main
slicing
- Loading branch information
Showing
4 changed files
with
336 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,336 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# 切片与索引\n", | ||
"\n", | ||
"从左往右索引时起始位置从0开始,从右往左索引时起始位置从-1开始。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 一维数组的情况" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([2, 0, 2, 3, 9, 2, 3, 4])" | ||
] | ||
}, | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#生成一维数组a\n", | ||
"a = np.array([2,0,2,3,9,2,3,4])\n", | ||
"a" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 一维数组的索引" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"9" | ||
] | ||
}, | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#索引查找数组a中编号为4的元素,即查找第5个元素\n", | ||
"#注:编号从0开始\n", | ||
"a[4]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 一维数组的切片" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([2, 2, 9])" | ||
] | ||
}, | ||
"execution_count": 12, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#将数组a切片[起始编号:终止编号(不包含):步长]\n", | ||
"a[0:6:2]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 多维数组的情况" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([[[ 0, 1, 2],\n", | ||
" [ 3, 4, 5],\n", | ||
" [ 6, 7, 8],\n", | ||
" [ 9, 10, 11]],\n", | ||
"\n", | ||
" [[12, 13, 14],\n", | ||
" [15, 16, 17],\n", | ||
" [18, 19, 20],\n", | ||
" [21, 22, 23]],\n", | ||
"\n", | ||
" [[24, 25, 26],\n", | ||
" [27, 28, 29],\n", | ||
" [30, 31, 32],\n", | ||
" [33, 34, 35]]])" | ||
] | ||
}, | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#生成多维数组b\n", | ||
"b = np.arange(36).reshape((3,4,3))\n", | ||
"b" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 多维数组的索引" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 14, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"11" | ||
] | ||
}, | ||
"execution_count": 14, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#查找b数组中,编号为0的元素中编号为3的维度下编号为2的元素\n", | ||
"#索引b数组中第1个矩阵中第4行第3个元素\n", | ||
"b[0,3,2]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 15, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"19" | ||
] | ||
}, | ||
"execution_count": 15, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#查找b数组中,编号为1的元素中编号为2的维度下编号为1的元素\n", | ||
"#索引b数组中第2个矩阵中第3行第2个元素\n", | ||
"b[1,2,1]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 16, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"19" | ||
] | ||
}, | ||
"execution_count": 16, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#查找b数组中,编号为-2的元素中编号为-2的维度下编号为-2的元素\n", | ||
"#索引b数组中第2个矩阵中第3行第2个元素,与上述[1,2,1]索引元素一致\n", | ||
"b[-2,-2,-2]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 多维数组的切片" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 17, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([ 6, 18, 30])" | ||
] | ||
}, | ||
"execution_count": 17, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#选取一个维度进行切片\n", | ||
"b[:,2,-3] #选择每个矩阵中从上往下第3行,从右往左第3个元素" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 18, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([[[ 3, 4, 5],\n", | ||
" [ 9, 10, 11]],\n", | ||
"\n", | ||
" [[15, 16, 17],\n", | ||
" [21, 22, 23]],\n", | ||
"\n", | ||
" [[27, 28, 29],\n", | ||
" [33, 34, 35]]])" | ||
] | ||
}, | ||
"execution_count": 18, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#对维度进行切片\n", | ||
"#每个维度切片方法与一维数组相同\n", | ||
"b[:,1:4:2,:] #维度起始值为1,终止值为4(不包含),步长为2" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 19, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([[[ 0, 2],\n", | ||
" [ 3, 5],\n", | ||
" [ 6, 8],\n", | ||
" [ 9, 11]],\n", | ||
"\n", | ||
" [[12, 14],\n", | ||
" [15, 17],\n", | ||
" [18, 20],\n", | ||
" [21, 23]],\n", | ||
"\n", | ||
" [[24, 26],\n", | ||
" [27, 29],\n", | ||
" [30, 32],\n", | ||
" [33, 35]]])" | ||
] | ||
}, | ||
"execution_count": 19, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#对每个维度切片\n", | ||
"#使用步长跳跃切片\n", | ||
"b[:,:,::2]" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.11" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.