For testing purposes it is often necessary to generate random arrays:
Vector indexing
Once you have your data in the array, NumPy is brilliant at providing
easy ways of giving it back:
All of the indexing methods presented above except fancy indexing are
actually so-called “views”: They don’t store the data and reflect the
changes in the original array if it happens to get changed after being
indexed.
All of those methods including fancy indexing are mutable: They allow
modification of the original array contents through assignment, as
shown above. This feature breaks the habit of copying arrays by slicing
them:
Another super-useful way of getting data from NumPy arrays is boolean
indexing, which allows using all kinds of logical operators:
any and all act just like their python peers, but don’t short-circuit
Careful though; Python “ternary” comparisons like 3<=a<=5 don’t
work here.
向量索引
对于数组数据的访问,numpy提供了便捷的访问方式:
上图中,除“fancy indexing”外,其他所有索引方法本质上都是`views`:它
们并不存储数据,如果原数组在被索引后发生更改,则会反映出原始数组中的
更改。
上述所有这些方法都可以改变原始数组,即允许通过分配新值改变原数组的内
容。这导致无法通过切片来复制数组:
此外,还可以通过布尔索引从NumPy数组中获取数据,这意味着可以使用各种
逻辑运算符:
any和all与其他 使用类似
注意,不可以使用`3 <= a <= 5`这样的Python“三元”比较。
如上所述,布尔索引是可写的。如下图np.where和np.clip两个专有函数。
Python
04