[Python Cookbook] Numpy Array Slicing and Indexing
1-D Array
Indexing
Use bracket notation [ ] to get the value at a specific index. Remember that indexing starts at 0.
import numpy as np
a=np.arange(12)
a
# start from index 0
a[0]
# the last element
a[-1]
Output:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
0
11
Slicing
Use :
to indicate a range.
array[start:stop]
A second :
can be used to indicate step-size.
array[start:stop:stepsize]
Leaving
start
or stop
empty will default to the beginning/end of the array.
a[1:4]
a[-4:]
a[-5::-2] #starting 5th element from the end, and counting backwards by 2 until the beginning of the array is reached
Output:
array([1, 2, 3, 4])
array([ 8, 9, 10, 11])
array([7, 5, 3, 1])
Multidimensional Array
r = np.arange(36)
r.resize((6, 6))
r
Output:
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35]])
Use bracket notation to index:
array[row, column]
and use : to select a range of rows or columns
r[2, 2]
r[3, 3:6]
r[:2, :-1]#selecting all the rows up to (and not including) row 2, and all the columns up to (and not including) the last column
r[-1, ::2]#selecting the last row, and only every other element
Output:
14
array([21, 22, 23])
array([[ 0, 1, 2, 3, 4],
[ 6, 7, 8, 9, 10]])
array([30, 32, 34])
We can also select nonadjacent elements by
r[[2,3],[4,5]]
Output:
array([16, 23])
Conditional Indexing
r[r > 30]
Output:
array([31, 32, 33, 34, 35])
Note that if you change some elements in the slice of an array, the original array will also be change. You can see the following example:
r2 = r[:3,:3]
print(r2)
print(r)
r2[:] = 0
print(r2)
print(r)
Output:
[[ 0 1 2]
[ 6 7 8]
[12 13 14]]
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]
[24 25 26 27 28 29]
[30 31 32 33 34 35]]
[[0 0 0]
[0 0 0]
[0 0 0]]
[[ 0 0 0 3 4 5]
[ 0 0 0 9 10 11]
[ 0 0 0 15 16 17]
[18 19 20 21 22 23]
[24 25 26 27 28 29]
[30 31 32 33 34 35]]
To avoid this, use r.copy
to create a copy that will not affect the original array.
r_copy = r.copy()
print(r_copy, '\n')
r_copy[:] = 10
print(r_copy, '\n')
print(r)
Output:
[[ 0 0 0 3 4 5]
[ 0 0 0 9 10 11]
[ 0 0 0 15 16 17]
[18 19 20 21 22 23]
[24 25 26 27 28 29]
[30 31 32 33 34 35]]
[[10 10 10 10 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]]
[[ 0 0 0 3 4 5]
[ 0 0 0 9 10 11]
[ 0 0 0 15 16 17]
[18 19 20 21 22 23]
[24 25 26 27 28 29]
[30 31 32 33 34 35]]
[Python Cookbook] Numpy Array Slicing and Indexing的更多相关文章
- [Python Cookbook] Numpy Array Manipulation
1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...
- [Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate
数组拼接方法一 思路:首先将数组转成列表,然后利用列表的拼接函数append().extend()等进行拼接处理,最后将列表转成数组. 示例1: import numpy as np a=np.arr ...
- Python 将numpy array由浮点型转换为整型
Python 将numpy array由浮点型转换为整型 ——使用numpy中的astype()方法可以实现,如:
- [Python Cookbook] Numpy: Multiple Ways to Create an Array
Convert from list Apply np.array() method to convert a list to a numpy array: import numpy as np myl ...
- python的numpy.array
为什么要用numpy Python中提供了list容器,可以当作数组使用.但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3].就需要三个指针和三 ...
- 【python】numpy array特殊数据统一处理
array中的某些数据坏掉,想要统一处理,找到了这个方法,做个笔记. 比如,把数组中所有小于0的数字置为0 import numpy as np t = np.array([-2, -1, 0, 1, ...
- python 中 numpy array 中的维度
简介 numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数.有时候我们可能需要知道某一维的特定维数. 二维情况 >>> import numpy as np ...
- [Python Cookbook] Numpy: Iterating Over Arrays
1. Using for-loop Iterate along row axis: import numpy as np x=np.array([[1,2,3],[4,5,6]]) for i in ...
- [Python Cookbook] Numpy: How to Apply a Function to 1D Slices along the Given Axis
Here is a function in Numpy module which could apply a function to 1D slices along the Given Axis. I ...
随机推荐
- Android 简历+面试题 汇总
1.教你写简历 1.1.你真的会写简历吗? 1.2.80%以上简历都是不合格的 1.3.推荐两个技术简历模板 1.4.关于程序员求职简历 1.5.程序员简历模板列表 2.面试题 2.1.国内一线互联网 ...
- P3386 【模板】二分图匹配(匈牙利&最大流)
P3386 [模板]二分图匹配 题目背景 二分图 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+1行,每行两个正 ...
- luoguT30204 偷上网
\(n=1\) 时特判四角,其余时刻圆的面积和必小于正方形面积,随机点出来判断就行了. stm 随机算法-- #include <iostream> #include <cstdli ...
- luogu1829 [国家集训队]Crash的数字表格
被 bs 了姿势水平--好好学习数学QAQQAQQAQ ref #include <iostream> #include <cstring> #include <cstd ...
- Webapp和后端交互检查测试
除了功能,我们可以使用下面方法,查看交互过程,页面不能发现的问题: 什么是json 什么是json,json是什么,json如何使用 JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能 ...
- sklearn快速入门
原创博文,转载请注明出处. (为了节约空间,打印结果常用"..."表示省略) 一.加载数据集 1. 加载sklearn自带的数据集 scikit-learn有一些自带的标准数据集, ...
- CSS简单的四种引入方式
CSS一共有四种引入方式 (1)最简单的两种方式是直接在html标签里面引入,或者在html文件前面声明,以下是简单的代码示例 <!DOCTYPE html> <html lang= ...
- hdu 4289 网络流拆点,类似最小割(可做模板)邻接矩阵实现
Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 12c可插拔数据库CDB与PDB管理总结
12c可插拔数据库CDB与PDB管理总结 创建pdb1.直接创建 CREATE PLUGGABLE DATABASE pdb2 ADMIN USER boswll IDENTIFIED BY orac ...
- POJ 3155 Hard Life(最大密度子图+改进算法)
Hard Life Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 9012 Accepted: 2614 Case Ti ...