numpy数组中冒号和负号的含义
numpy数组中":"和"-"的意义
觉得有用的话,欢迎一起讨论相互学习~




在实际使用numpy时,我们常常会使用numpy数组的-1维度和":"用以调用numpy数组中的元素。也经常因为数组的维度而感到困惑。
- 总体来说,":"用以表示当前维度的所有子模块
- "-1"用以表示当前维度所有子模块最后一个,"负号用以表示从后往前数的元素,-n即是表示从后往前数的第n个元素"
分片功能
a[1: ] 表示该列表中的第1个元素到最后一个元素,而,a[ : n]表示从第0个元素到第n个元素(不包括n)
import numpy as np
POP_SIZE = 3
total_size = 10
idx = np.arange(total_size)
good_idx_1 = idx[-POP_SIZE:]
good_idx_2 = idx[:-POP_SIZE]
good_idx_3 = idx[POP_SIZE:]
good_idx_4 = idx[:POP_SIZE]
print("good_idx_1", good_idx_1)
print("good_idx_2", good_idx_2)
print("good_idx_3", good_idx_3)
print("good_idx_4", good_idx_4)
# good_idx_1 [7 8 9]
# good_idx_2 [0 1 2 3 4 5 6]
# good_idx_3 [3 4 5 6 7 8 9]
# good_idx_4 [0 1 2]
测试代码
import numpy as np
b = np.arange(start=0, stop=24, dtype=int)
print('b.shape', b.shape)
# b.shape (24,)
b1 = b.reshape((4, 2, 3))
print('the value of b1\n', b1)
# the value of b1
# [[[ 0 1 2]
# [ 3 4 5]]
#
# [[ 6 7 8]
# [ 9 10 11]]
#
# [[12 13 14]
# [15 16 17]]
#
# [[18 19 20]
# [21 22 23]]]
print('b1[-1]\n', b1[-1])
# 从最外层的维度分解出最后一个模块
# b1[-1]
# [[18 19 20]
# [21 22 23]]
for a in b1[-1]:
print('s')
# 在这个模块中有两个小的模块,所以程序运行两次
# s
# s
print('b1[:-1]\n', b1[:-1])
# 从最外层的模块中分解出除最后一个子模块后其余的模块
# b1[:-1]
# [[[ 0 1 2]
# [ 3 4 5]]
#
# [[ 6 7 8]
# [ 9 10 11]]
#
# [[12 13 14]
# [15 16 17]]]
for a1 in b1[:-1]:
print('s')
# 在这个模块中有三个小的模块,所以程序运行两次
# s
# s
# s
print('b1[-1:]\n', b1[-1:])
# 写在最后一个维度的":"没有实质性作用,此处表示的意思和b1[-1]相同
# b1[-1:]
# [[[18 19 20]
# [21 22 23]]]
print('b1[:,-1]\n', b1[:, -1])
# 表示取出最外层的所有维度后每一个子模块中选择最后一个子模块
# b1[:,-1]
# [[ 3 4 5]
# [ 9 10 11]
# [15 16 17]
# [21 22 23]]
print('b1[:,:,-1]\n', b1[:, :, -1])
# 表示取最里层维度的最后一个元素重新组成新的元组
# b1[:,:,-1]
# [[ 2 5]
# [ 8 11]
# [14 17]
# [20 23]]
numpy数组中冒号和负号的含义的更多相关文章
- 统计numpy数组中每个值出现的个数
统计numpy数组中某一个值或某几个值出现的个数:sum(data==4) # 统计出现了几个cluster include0Cluster = sum(res == 0) include1Clust ...
- python numpy数组中的复制问题
vector = numpy.array([5, 10, 15, 20]) equal_to_ten_or_five = (vector == 10) | (vector == 5) vector[e ...
- 统计numpy数组中每个值的个数
import numpy as np from collections import Counter data = np.array([1.1,2,3,4,4,5]) Counter(data) #简 ...
- python numpy 数组中元素大于等于0的元素
>>> import numpy as np >>> a = np.random.randint(-5, 5, (5, 5)) >>> a arr ...
- numpy 数组中添加新元素
import numpy as npnew_array = np.empty(shape=[0, 3]) # 3列n行for i in range(10): x = i+1 y = i+2 z = i ...
- 统计numpy数组中最频繁出现的值
arr = np.array([[1,2,100,4,5,6],[1,1,100,3,5,5],[2,2,4,4,6,6]]) 方法一: count = np.bincount(arr[:,2]) # ...
- 操作 numpy 数组的常用函数
操作 numpy 数组的常用函数 where 使用 where 函数能将索引掩码转换成索引位置: indices = where(mask) indices => (array([11, 12, ...
- 对Numpy数组按axis运算的理解
Python的Numpy数组运算中,有时会出现按axis进行运算的情况,如 >>> x = np.array([[1, 1], [2, 2]]) >>> x arr ...
- 玩转NumPy数组
一.Numpy 数值类型 1.前言:Python 本身支持的数值类型有 int(整型, long 长整型).float(浮点型).bool(布尔型) 和 complex(复数型).而 Numpy 支持 ...
随机推荐
- ES6的新特性(17)——Generator 函数的异步应用
Generator 函数的异步应用 异步编程对 JavaScript 语言太重要.Javascript 语言的执行环境是“单线程”的,如果没有异步编程,根本没法用,非卡死不可.本章主要介绍 Gener ...
- 王者荣耀交流协会 -- 第4次Scrum会议
Scrum master : 王磊 要求1 : 工作照片 照片由高远博同学拍摄 ,王露芝同学(外援)没有参加本次会议. 要求2 : 时间跨度:2017年10月16日 18:00 - 18:44 共计4 ...
- Java 更改日期格式
import java.util.*; import java.text.*; public class TestDateFormat { public static void main(String ...
- 关于window.open弹出窗口被阻止的问题
原文:http://blog.csdn.net/fanfanjin/article/details/6858168 在web编程过程中,经常会遇到一些页面需要弹出窗口,但是在服务器端用window.o ...
- Docker 技术 介绍
https://github.com/docker/docker 实现用户空间隔离的技术:名称空间(NameSpace),CGroup(控制组) 什么是NameSpace::简单的理解就是,每一个虚拟 ...
- MySQL 基于lvm2的备份实战演练 (快照备份)
前言: lvm2实现热备的原理是基于lvm2的快照功能,lvm2可以实现数据集不大的情况下的热备. 实战过程如下:这里的演示是在一台Mariadb服务器上进行创建快照,将快照中的文件scp到备份服务器 ...
- HDU 2154 跳舞毯
http://acm.hdu.edu.cn/showproblem.php?pid=2154 Problem Description 由于长期缺乏运动,小黑发现自己的身材臃肿了许多,于是他想健身,更准 ...
- PAT 甲级 1046 Shortest Distance
https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...
- 找xpath好用的工具(比较少用,针对只能在IE上打开的网站)
有一些网站只能在IE浏览器里打开,不像firefox那样有好多好用的插件来找元素的xpath,css path等. 当然现在IE也可以,F12出现像firebug那样的窗口,来查看元素. 这里呢在介绍 ...
- Windows搭建Log4Net+FileBeat+ELK日志分析系统过程
参考博客:http://udn.yyuap.com/thread-54591-1-1.html ; https://www.cnblogs.com/yanbinliu/p/6208626.html ; ...