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 支持 ...
随机推荐
- 创建hive与hbase关联的hive表与hbase表
创建hive与hbase的关联表 create external table hive_hbase(rowkey string,name string,addr string,topic string ...
- pager-taglib2.0中文传参乱码问题
1.重现问题 在web项目中有时会用到pager-taglib来作为分页的标签,如上图红色框标识所示,当我们需要把页面参数保持的时候我们会在<pg:param />标签中把参数进行传递. ...
- spring mvc 详细配置
转自: http://www.cnblogs.com/superjt/p/3309255.html 现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是 ...
- Scrum立会报告+燃尽图(十月十五日总第六次):视频上传及选题介绍工作
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2196 Scrum立会master:田良 一.小组介绍 组长:付佳 组员: ...
- c# 调用c++dll二次总结
1.pinvoke结构不对称,添加语句(网上有) 2.含回调函数,成员参数的结构体必须完全,尽管自己用不到. 3.加深对c++指针的理解.一般情况下,类型加*等效于c++中的ref.但对于short* ...
- c# 窗体与窗体外的文件互动(拖拽)
大部分控件都有此事件drag相关. 以picturebox为例: pictureBox1.AllowDrop = true;//此属性需要全打出来,不会自动识别添加 private void pict ...
- ImportError: No module named examples.tutorials.mnist
Traceback (most recent call last): File "nearest_neighbor.py", line 14, in <module> ...
- app测试更多机型系统解决方法
手头上测试机有限,不可能每个机型每个系统都 有一部手机,此时寻求一个什么都有的测试平台就显得尤为重要了. 作为小白的我刚刚使用了一波腾讯优测,简单粗暴有效给力,而且新注册认证用户还有60min免费使用 ...
- 奇异值分解(SVD)原理详解及推导(转载)
转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/43053513 在网上看到有很多文章介绍SVD的,讲的也都不错,但是感觉还是有 ...
- PAT L1-032 Left-pad
https://pintia.cn/problem-sets/994805046380707840/problems/994805100684361728 根据新浪微博上的消息,有一位开发者不满NPM ...