金点网络-全网资源,一网打尽
  • 网站首页
    • 金点部落
    • 小游戏
    • OpenAPI
    • 设计资产导航
    • 升级会员
  • 技能学习
    • 体育运动
    • 办公教程
    • 口才演讲
    • 小吃技术
    • 建站教程
    • 摄影教程
    • 棋牌教程
    • 网赚教程
      • 爆粉引流
      • 自媒体
      • 贴吧引流
  • 网站源码
    • 商城/淘客/交易
    • 小说/漫画/阅读
    • 影视/音乐/视频
    • 微信/微商/微擎
    • 理财/金融/货币
    • 模板/主题/插件
  • 游戏源码
    • 精品网单
    • 端游源码
    • 手游源码
    • 页游源码
  • 素材资料
    • 电子文档
    • 综合资料
    • 考研资料
    • 设计素材
    • 音频讲座
      • 人文艺术
      • 名师讲座
      • 说书小说
  • 软件工具
    • Windows软件
    • MacOS软件
    • Android软件
  • 寄售资源
    • 游戏源码
    • 网站源码
    • 软件源码
  • 公益服
登录/注册
  • 专享大神特权
立即开通开通会员抄底价

Python数据分析(中英对照)·Indexing NumPy Arrays 索引 NumPy 数组

作者 : jamin 本文共5899个字,预计阅读时间需要15分钟 发布时间: 2020-10-18 共1127人阅读

2.2.3: Indexing NumPy Arrays 索引 NumPy 数组

NumPy arrays can also be indexed with other arrays or other sequence-like objects like lists.
NumPy数组也可以与其他数组或其他类似于序列的对象(如列表)建立索引。
Let’s take a look at a few examples.
让我们来看几个例子。
I’m first going to define my array z1.
我首先要定义我的数组z1。
And let’s put in a few elements in there– 1, 3, 5, 7, and 9, for example.
让我们把一些元素放进去,比如1,3,5,7和9。
I can then define a new array called z2, which is just z1 with one added to every single element of the array.
然后我可以定义一个名为z2的新数组,它只是z1,数组的每个元素都添加了一个。
We can now look at these two arrays to see what their contents are.
现在我们可以看看这两个数组,看看它们的内容是什么。
I can now define a list called n, which I will be using to index my z1 and z2.
我现在可以定义一个名为n的列表,我将使用它来索引我的z1和z2。
So let me put in the elements 0, 2 and 3 in my index.
让我把元素0,2和3放在索引中。
I can now type z1, square bracket, ind, which gives me access to the elements that are located within z1 at the locations that
我现在可以输入z1,方括号,ind,这使我能够访问位于z1内的元素,这些元素位于
are specified by ind.
由ind指定。
In this example, index or ind, was defined as a Python list,but we could also have defined that as a NumPy array.
在本例中,index或ind被定义为Python列表,但我们也可以将其定义为NumPy数组。
So I can take my previous list, 0, 2, 3, turn that into a NumPy array,and I can still do my indexing.
所以我可以把我以前的列表,0,2,3,变成一个NumPy数组,我仍然可以做我的索引。
In other words, we can index NumPy arrays
换句话说,我们可以索引NumPy数组
using either lists or other NumPy arrays.
使用列表或其他NumPy数组。
NumPy arrays can also be indexed using logical indices,but what does that actually mean?
NumPy数组也可以使用逻辑索引进行索引,但这实际上意味着什么?
Just as we can have an array of numbers, we can have an array consisting of true and false, which are two Boolean elements.
正如我们可以有一个数字数组一样,我们也可以有一个由true和false组成的数组,它们是两个布尔元素。
Let’s first construct a Boolean array.
让我们首先构造一个布尔数组。
I’m going to define my z1 again here as before.
我将像以前一样在这里再次定义我的z1。
I can now type something like z1 greater than 6.
我现在可以输入z1大于6的值。
The element 0 of z1 is not greater than 6,and therefore element 0 of the Boolean array that Python returns to you is false.
z1的元素0不大于6,因此Python返回给您的布尔数组的元素0为false。
The same is true for elements 1 and 2 of z1, so we get a false there as well.
z1的元素1和2也是如此,所以我们也得到了一个false。
Now element 3 of Z1 is greater than 6.
现在Z1的元素3大于6。
Its value happens to be 7, so therefore, the corresponding element in the Boolean array is true.
它的值正好是7,因此,布尔数组中的对应元素为true。
And in fact, all subsequent elements are also greater than 6,so therefore all subsequent elements in the Boolean array are also true.
事实上,所有后续元素也都大于6,因此布尔数组中的所有后续元素也是真的。
We can use the Boolean array, also called a logical array, to index another vector.
我们可以使用布尔数组,也称为逻辑数组,来索引另一个向量。
But what are the elements that are returned in that case?
但是在这种情况下返回的元素是什么?
As you may have guessed, Python returns those elements of the array for which the corresponding value in the Boolean vector is true.
正如您可能已经猜到的,Python返回布尔向量中对应值为true的数组元素。
Let’s look at an example.
让我们看一个例子。
Let’s first use the logical index vector to access elements of z1.
让我们首先使用逻辑索引向量访问z1的元素。
I’m interested in accessing elements of z1, I therefore start with z1.
我对访问z1的元素感兴趣,因此我从z1开始。
I need my square brackets as usual.
我像往常一样需要方括号。
Inside the square brackets, I need to put in my logical index vector, which is, in this case, z1 greater than 6.
在方括号内,我需要放入逻辑索引向量,在本例中,z1大于6。
In this case, Python returns to me those elements of the z1 array that happened to be greater than 6 in value.
在本例中,Python向我返回z1数组中那些值恰好大于6的元素。
But I can also do the same to my array z2.
但是我也可以对我的数组z2做同样的处理。
Now let’s think about this, what’s happening here.
现在让我们想想,这里发生了什么。
My index vector is still identifying those elements of z1 that happened to be greater than six.
我的索引向量仍然在识别z1中碰巧大于6的元素。
Now in this case, I am however, using that index vector to access elements in my array z2.
现在在这个例子中,我使用这个索引向量来访问数组z2中的元素。
I can run this and I get an output.
我可以运行它并得到一个输出。
I could also define my logical vector in a slightly different way.
我也可以用稍微不同的方式定义我的逻辑向量。
I could explicitly construct a logical vector.
我可以显式地构造一个逻辑向量。
In this case, I’ve typed ind equals z1 greater than 6.
在本例中,我输入的ind等于大于6的z1。
If we inspect the content of ind, we’ll see that it’s a logical vector.
如果我们检查ind的内容,就会发现它是一个逻辑向量。
I can now type z1, square bracket, ind, and I get the same output as before.
我现在可以输入z1,方括号,ind,得到和以前一样的输出。
Similarly, I can type z2, ind, and the output is the same.
类似地,我可以输入z2,ind,并且输出是相同的。
One final word about indexing NumPy arrays– and this is really important because it can easily lead to subtle programming errors.
关于索引NumPy数组的最后一句话——这非常重要,因为它很容易导致微妙的编程错误。
When you slice an array using the colon operator, you get a view of the object.
使用冒号操作符切片数组时,将获得对象的视图。
This means that if you modify it, the original array will also be modified.
这意味着如果修改它,原始数组也将被修改。
This is in contrast with what happens when you index an array, in which case what is returned to you is a copy of the original data.
这与索引数组时的情况相反,在这种情况下,返回给您的是原始数据的副本。
Let’s clarify this distinction with examples.
让我们用例子来阐明这一区别。
I’m going to define my z2 as before.
我将像以前一样定义我的z2。
Then, I will define a new vector "w" in this case.
然后,在这种情况下,我将定义一个新的向量“w”。
I’m just going to slice that, let’s say, from 0 to 3.
我将把它从0切到3。
I can look at the contents of my w.
我可以看看我的w。
In this case, I choose to modify the element at location zero.
在本例中,我选择修改位置为零的元素。
So I’m just going to type w square brackets 0, is equal to 3.
所以我要输入w,方括号0,等于3。
I can now inspect the content of w, and instead of having 1, 3, and 5,I have 3, 3, and 5, as we would have expected.
我现在可以检查w的内容,而不是1,3和5,我有3,3和5,正如我们所期望的。
However, if I now type z1, you will see that the first element at location is 0 of that array has also been modified.
但是,如果我现在键入z1,您将看到该数组位置0处的第一个元素也已修改。
Let’s see what happens if we use indexing and not slicing to access an array.
让我们看看如果我们使用索引而不是切片来访问数组会发生什么。
As before, I’m going to define my z1 vector.
和前面一样,我将定义z1向量。
Then I need to define my index vector, which I’m going to call 0, 1, and 2.
然后我需要定义我的索引向量,我将其称为0、1和2。
In this case, I’ve defined this as a list,but if I wanted to define that as an array,I simply place NumPy.array outside of my list.
在本例中,我将其定义为一个列表,但如果我想将其定义为一个数组,我只需将NumPy.array放在列表之外。
Now I have two elements defined.
现在我定义了两个元素。
I have my z1 and I have my index.
我有我的z1和我的索引。
I can now do the following– I’m going to create w by taking my object z1 and indexing that using my index vector.
我现在可以做以下事情了——我将通过获取对象z1并使用索引向量对其进行索引来创建w。
The contents of w here are 1, 3, and 5.
这里w的内容是1、3和5。
I can now look at the element 0 of w and I can set that to be equal to 3.
我现在可以看w的元素0,我可以把它设为等于3。
If I now look at w, I I’ll see that the element, the first element,located at position 0 has been modified to 3.
如果我现在看w,我会看到位于位置0的元素,第一个元素,被修改为3。
If I now go back to my array z1, I see that the first element, number 1,has not been modified.
如果我现在回到我的数组z1,我看到第一个元素,数字1,没有被修改。
In summary, for all cases of indexed arrays, what is returned is a copy of the original data, not a view as one gets for slices.
总之,对于索引数组的所有情况,返回的都是原始数据的副本,而不是切片的视图。

Python 数据分析
本站所提供的部分资源来自于网络,版权争议与本站无关,版权归原创者所有!仅限用于学习和研究目的,不得将上述内容资源用于商业或者非法用途,否则,一切后果请用户自负。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源。如果上述内容资对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。本站不保证所提供下载的资源的准确性、安全性和完整性,源码仅供下载学习之用!如用于商业或者非法用途,与本站无关,一切后果请用户自负!本站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。如有侵权、不妥之处,请联系站长以便删除!
金点网络-全网资源,一网打尽 » Python数据分析(中英对照)·Indexing NumPy Arrays 索引 NumPy 数组

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
是否提供免费更新服务?
持续更新,永久免费
是否经过安全检测?
安全无毒,放心食用
jamin

jamin 大神

下一篇
网络协议-组播介绍

相关推荐

Python数据分析(中英对照)·Plotting Using Logarithmic Axes-使用对数轴绘图

Python数据分析(中英对照)·Plotting Using Logarithmic Axes-使用对数轴绘图

Python数据分析(中英对照)·Random Walks 随机游走

Python数据分析(中英对照)·Random Walks 随机游走

Python数据分析(中英对照)·Sequences 序列

Python数据分析(中英对照)·Sequences 序列

Python数据分析(中英对照)·Introduction to Matplotlib and Pyplot-Matplotlib 和 Pyplot 介绍

Python数据分析(中英对照)·Introduction to Matplotlib and Pyplot-Matplotlib 和 Pyplot 介绍

Python数据分析(中英对照)·Lists 列表

Python数据分析(中英对照)·Lists 列表

标签云
Android Atom ExecutorService ForkJoin GM GM后台 GM授权后台 H5 Java Javascript Linux手工服务端 pipbestcom Python ReentrantLock synchronized ThreadLocal volatile Win一键即玩服务端 一键端 传奇 写作 创业 单机 后台 商业端 外网 安卓 安卓苹果双端 工具 手工端 手游 搭建教程 教程 数据分析 文案 游戏源码 端游 经典 网单 职场 自媒体 视频教程 详细搭建教程 运营后台 页游

近期文章

  • 回合手游【逍遥西游之繁华西游】最新整理单机一键既玩镜像服务端_Linux手工端_GM后台_教程
  • 最新整理精品回合制手游【天书奇谈3D混沌完整版】VM一键单机版_linux手工外网端_隐盟视频教程_授权GM后台_双端
  • 典藏修真页游【诸仙列传OL】最新整理Win系服务端_GM工具_详细外网搭建教程
  • MT3换皮MH【浮生若梦尊享挂机修复版】最新整理单机一键即玩镜像端_Linux手工服务端_安卓苹果双端_GM后台_详细搭建教程
  • 大话回合手游【最新引擎之缥缈西游渡劫版】最新整理Linux手工服务端_安卓苹果双端_管理后台_CDK后台_详细搭建教程_视频教程

分类

  • | wordpress插件 |
  • | wordpress模板 |
  • | 其它模板 |
  • | 帝国模板 |
  • | 织梦插件 |
  • | 织梦模板 |
  • A5源码
  • Android软件
  • APP引流
  • E语言
  • H5
  • LUA
  • QQ营销
  • SEO推广
  • Windows软件
  • 体育运动
  • 信息数据
  • 创业专题
  • 办公教程
  • 口才演讲
  • 名师讲座
  • 商城/淘客/交易
  • 小吃技术
  • 小说/漫画/阅读
  • 建站教程
  • 引流脚本
  • 影视/音乐/视频
  • 影视资源
  • 微信/微商/微擎
  • 微信小程序
  • 微信营销
  • 微擎模块
  • 手游源码
  • 技能学习
  • 抖音课程
  • 摄影教程
  • 棋牌教程
  • 模板/主题/插件
  • 游戏源码
  • 爆粉引流
  • 理财/金融/货币
  • 生活老师
  • 电商客
  • 电子文档
  • 电脑教程
  • 社群营销
  • 站长工具
  • 精品网单
  • 系统工具
  • 素材资料
  • 综合资料
  • 编程经验
  • 网站源码
  • 网络安全
  • 网赚教程
  • 网赚源码
  • 考研资料
  • 脚本/AI/智能
  • 自媒体
  • 英语学习
  • 营销软件
  • 设计素材
  • 说书小说
  • 贴吧引流
  • 软件工具
  • 软文营销
  • 逆向软件
  • 音频讲座
  • 页游源码

提供最优质的资源集合

立即加入 友好社区
金点网络-全网资源,一网打尽

新一代全网资源综合门户网(www.pipbest.com-金点网络)专注服务于互联网,提供各类最新最全的免费源码下载(PHP、ASP、JSP、.NET),更提供免费工具,免费源码下载,软件下载,素材下载,赚钱教程下载,交流论坛等网站运营相关的一切内容,为网友搜罗最有价值的网站源码下载与技术教程等服务!

服务目录
  • 金点OpenAPI
  • 金点云
  • 金点支付
友情链接
  • 数媒派
  • 国家电网
快速搜索

本站由Nice强力驱动

声明: 本站部分内容属于原创转载请注明出处 如有侵权行为请严格参照本站【版权声明】与我们联系,我们将在48小时内容进行处理!

本站部分内容属于原创转载请注明出处 如有侵权行为请严格参照本站【版权声明】与我们联系,我们将在48小时内容进行处理!
© 2016-2023 PipBest.Com - 金点网络 & 金点部落. All rights reserved 京ICP备2022005359号-1
  • 关注有礼
  • 签到
  • 客服
    官方QQ群 常见问题 FAQ

    在线客服

    点我联系

    直接说出您的需求!
    切记!带上资源连接与问题!

    工作时间: 9:30-21:30

  • 暗黑
    模式
  • 全屏
  • 投稿
    赚钱
  • 首页

  • 签到

  • 切换

  • 客服

金点网络-全网资源,一网打尽
  • 登录
  • 注册
or
or
忘记密码?
金点网络-全网资源,一网打尽
  • 网站首页 ►
    • 金点部落
    • 小游戏
    • OpenAPI
    • 设计资产导航
    • 升级会员
  • 技能学习 ►
    • 体育运动
    • 办公教程
    • 口才演讲
    • 小吃技术
    • 建站教程
    • 摄影教程
    • 棋牌教程
    • 网赚教程 ►
      • 爆粉引流
      • 自媒体
      • 贴吧引流
  • 网站源码 ►
    • 商城/淘客/交易
    • 小说/漫画/阅读
    • 影视/音乐/视频
    • 微信/微商/微擎
    • 理财/金融/货币
    • 模板/主题/插件
  • 游戏源码 ►
    • 精品网单
    • 端游源码
    • 手游源码
    • 页游源码
  • 素材资料 ►
    • 电子文档
    • 综合资料
    • 考研资料
    • 设计素材
    • 音频讲座 ►
      • 人文艺术
      • 名师讲座
      • 说书小说
  • 软件工具 ►
    • Windows软件
    • MacOS软件
    • Android软件
  • 寄售资源
    ►
    • 游戏源码
    • 网站源码
    • 软件源码
  • 公益服
×
u3** 刚刚下载了 爆款吸金文案训练

    全网资源·一网打尽

  • 金点出品,必属精品!
  • 发布原创内容,获取高额提成!
  • 我们与你共创美好数字生态!
  • 无特殊说明密码默认:pipbest.com