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 支持 ...
随机推荐
- 清空git缓存
git rm -r --cached .git add . git commit -m 'update .gitignore' 读了下git文档,才发现,这些东西其实很简单,很容易理解.cached其 ...
- ZOJ 3946 Highway Project 贪心+最短路
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...
- C语言问卷调查表
你对自己的未来有什么规划?做了哪些准备? 对未来比较迷茫,现在的主要任务是学好专业课 你认为什么是学习?学习有什么用?现在学习动力如何?为什么? 活到老学到老,学习是一辈子的事.在学习的过程 ...
- 不要USB数据线调试Android开发
不管是过去Eclipse还是现在的Android Studio开发Android,运行或者调试时都会利用USB数据线连接电脑和手机,特别是当现在的手机只有一个Type-c接口,意味着,插上后,啥也干不 ...
- alpha冲6
队名:日不落战队 安琪(队长) 今天完成的任务 回收站前端界面. 明天的计划 查看个人信息界面. 还剩下的任务 信息修改前端界面. 设置界面. 遇到的困难 模拟机莫名其妙就崩了,调试了很久,后在队友的 ...
- JAVA之路(一)
距离做下复习JAVA并学好JAVA的决定已经过去一周了,我买了慕课网的JAVA入门视频,在图书馆借了三本关于JAVA的书——两本是JAVA入门经典,一本是JAVA WEB开发宝典.我的计划是短时间内复 ...
- 【acm】杀人游戏(hdu2211)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2211 杀人游戏 Time Limit: 3000/1000 MS (Java/Others) M ...
- Ubuntu 16.04出现:Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/
转自:http://blog.csdn.net/zzq123686/article/details/77454066 出现错误信息: Reading package lists... Done E: ...
- C语言 aabbcc、abc、fabc、aabc
输入一个字符串,匹配字符串中连续出现的字符串.并且连续个数相等 如输入 aabbcc.abc.fabc.aabc.aabbc 分别输出yes还是no #include<stdio.h>#i ...
- javascript+html5+css3下拉刷新 数据效果
文章摘自:suchso.com/projecteactual/javascript-html5-css3-taobao-xiala-data.html segmentfault.com/a/11900 ...