当前位置:网站首页>[numpy] array index and slice
[numpy] array index and slice
2022-07-27 20:53:00 【When camellia flowers bloom.】

If the array is one-dimensional , Then index and slice are and Python The list of is the same
import numpy as np
# Index and slice of one-dimensional array
arr1 = np.arange(10) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
## Index operation
arr1[5] # 5
arr1[-1] # 9
## Slice
arr1[4:6] # array([4, 5])
arr1[::2] # array([0, 2, 4, 6, 8])If the array is multidimensional ( Here's a two-dimensional array ), Then in brackets [] in , Give two values , The two values are passed comma , separated , Before the comma is the line , Column after comma ; If brackets [] Only one value in , Then the representative bank
arr2

Be careful :arr2 Is random
import numpy as np
# Index and slice of multidimensional array
arr2 = np.random.randint(0,10,size=(4,6))
# For the first 0 Row data
arr2[0] # array([8, 8, 0, 0, 9, 2])
# For the first 1,2 Row data
'''
array([[9, 2, 9, 7, 5, 0],
[6, 0, 8, 3, 4, 7]])
'''
arr2[1:3]
# Get multiple rows of data example 0,2,3 Row data
'''
array([[8, 8, 0, 0, 9, 2],
[6, 0, 8, 3, 4, 7],
[2, 7, 6, 5, 0, 9]])
'''
arr2[[0,2,3]]
# For the first 2 Xing di 1 Column data
arr2[2,1] # 0
# Get multiple data Example No 1 Xing di 4 Column 、 The first 2 Xing di 5 Column data
arr2[[1,2],[4,5]] # array([5, 7])
# Get multiple data Example No 1、2 OK, No 4、5 Columns of data
'''
array([[5, 0],
[4, 7]])
'''
arr2[1:3,4:6]
# Get a column of data Example No 1 All data of the column
arr2[:,1] # array([8, 2, 0, 7])
# Get multiple columns of data Example No 1、3 All data of the column
'''
array([[8, 0],
[2, 7],
[0, 3],
[7, 5]])
'''
arr2[:,[1,3]] Expand and supplement knowledge : Boolean index
The Boolean index uses Boolean operations ( Such as : Comparison operator ) To get an array of elements that meet the specified criteria
import numpy as np
# Generate 1-24 Of 4 That's ok 6 Two dimensional array of columns
arr = np.arange(24).reshape((4,6))
# array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr[arr < 10]
# array([ 0, 1, 2, 3, 4, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,23])
arr[(arr < 5)|(arr > 10)]example 1: Get greater than 5 The elements of
import numpy as np
arr = np.array([[0,1,2],[3,4,5],[6,7,8],[9,10,11]])
# Get greater than 5 The elements of
# array([ 6, 7, 8, 9, 10, 11])
arr[arr > 5]example 2: Filter NaN
import numpy as np
arr = np.array([np.nan,1,2,np.nan,3,4,5])
# array([1., 2., 3., 4., 5.])
arr[~np.isnan(arr)]example 3: Use & Connect two conditions , Screening is greater than 5 And less than 10 The elements of
import numpy as np
arr = np.array([[0,1,2],[3,4,5],[6,7,8],[9,10,11]])
# Screening is greater than 5 And less than 10 The elements of
# array([6, 7, 8, 9])
arr[(arr > 5) & (arr < 10)]Tips
1. Boolean index is through the same data True and False To extract
2. The extraction conditions can be one or more , Use when there are multiple extraction conditions & Represents and , Use | For or on behalf of
3. When there are multiple extraction conditions , Each condition should be enclosed in parentheses
边栏推荐
猜你喜欢

金仓数据库 Oracle 至 KingbaseES 迁移最佳实践 (4. Oracle数据库移植实战)
![[dart] a programming language for cross end development](/img/e1/1167a322bb9f276f2e00fb12414d17.png)
[dart] a programming language for cross end development

程序中的地址如何转换?
![[Numpy] 数组索引和切片](/img/ce/34db7aef3fefe8a03e638d0838492f.png)
[Numpy] 数组索引和切片

One week activity express | in simple terms, issue 8; Meetup Chengdu station registration in progress
![[deep learning] video classification technology sorting](/img/bf/422d4ef342199966bbdaae06977699.png)
[deep learning] video classification technology sorting

五大知名人士对于AI的忧虑

【Dart】一门为跨端开发而生的编程语言

Things about stack migration

做测试, 就得去大厂,内部披露BAT大厂招聘“潜规则”
随机推荐
Software test interview question: if a string is known as "hello_world_yoyo", how to get a queue ["hello", "world", "yoyo"]
金仓数据库 Oracle至KingbaseES迁移最佳实践(2. 概述)
金仓数据库 KingbaseES异构数据库移植指南 (3. KingbaseES移植能力支撑体系)
MySQL log query log
R语言使用t.test函数执行t检验验证总体均值是否是某个特定的值(从样本集推论总体均值)
RK3399平台入门到精通系列讲解(导读篇)21天学习挑战介绍
Onion group joined hands with oceanbase to realize distributed upgrading, and global data has achieved cross cloud integration for the first time
SQL高级技巧CTE和递归查询
The variable "lattice" or class "lattice.latticeeasy" (matlab) is not defined
一文了解Pycharm快捷键
Technology blog and tutorial
How to monitor the running status and usage of NVIDIA Jetson
IM即时通讯开发如何提升移动网络下图片传输速度和成功率
【数据集显示标注】VOC文件结构+数据集标注可视化+代码实现
Write bootloader from 0
好开不贵,舒适安全!深度体验比亚迪宋Pro DM-i
一周活动速递|深入浅出第8期;Meetup成都站报名进行中
Is it safe for CICC fortune to open an account? What is the use of opening an account
Common ways to keep requests idempotent
How does the industrial switch enter the web management interface?