Building and Examining NumPy Arrays 构建和检查 NumPy 数组

2.2.4: Building and Examining NumPy Arrays 构建和检查 NumPy 数组

NumPy provides a couple of ways to construct arrays with fixed,start, and end values, such that the other elements are uniformly spaced between them.
NumPy提供了两种方法来构造具有固定值、起始值和结束值的数组,以便其他元素在它们之间均匀分布。
To construct an array of 10 linearly spaced elements starting with 0 and ending with 100, we can use the NumPy linspace function.
要构造一个由10个线性间隔元素组成的数组,从0开始到100结束,我们可以使用NumPy linspace函数。
In this case, I’m going to type np.linspace.
在本例中,我将键入np.linspace。
The first argument is the starting point, which is 0.
第一个参数是起点,即0。
The second is the ending point, which will be included in the NumPy array that gets generated.
第二个是结束点,它将包含在生成的NumPy数组中。
And the final argument is the number of points I would like to have in my array.
最后一个参数是数组中的点数。
In this case, NumPy has created a linearly spaced array starting at 0 and ending at 100.
在本例中,NumPy创建了一个从0开始到100结束的线性间隔阵列。
Now, to construct an average of 10 logarithmically spaced elements between 10 and 100, we can do the following.
现在,要构造10个10到100之间的对数间隔元素的平均值,我们可以执行以下操作。
In this case we use the NumPy logspace command.
在本例中,我们使用NumPy logspace命令。
But now careful, the first argument that goes into logspace is going to be the log of the starting point.
但是现在要小心,进入日志空间的第一个参数将是起点的日志。
If you want the sequence to start at 10, the first argument has to be the log of 10 which is 1.
如果希望序列从10开始,则第一个参数必须是10的log,即1。
The second argument is the endpoint of the array, which is 100.
第二个参数是数组的端点,它是100。
And again, we need to put in the log of that, which is 2.
再一次,我们需要把它放到日志中,也就是2。
And the third argument as before, is the number of elements in our array.
和前面一样,第三个参数是数组中的元素数。
in this case, what NumPy has constructed is an array consisting of 10 elements where the first element is 10 and the last element is 100.
在本例中,NumPy构造了一个由10个元素组成的数组,其中第一个元素是10,最后一个元素是100。
All of the other elements are uniformly spaced between those two extreme points in the logarithmic space.
所有其他元素均匀分布在对数空间的两个端点之间。
To construct array of ten logarithmically spaced elements between numbers say 250 and 500, we first need to take the base 10 logarithm of the numbers, 250 and 500,and then feed those into the logspace function.
为了构造一个由10个对数间隔的元素组成的数组,比如250和500之间的数字,我们首先需要取250和500这两个数字的以10为底的对数,然后将它们输入对数空间函数。
So let’s try that out.
让我们试试看。
I’m going to start by typing np.logspace.
我将首先键入np.logspace。
And now we need to take the base 10 logarithm of the number 250.
现在我们需要取数字250的以10为底的对数。
To do that, I will type np.log10 and the number is 250.
为此,我将键入np.log10,数字是250。
For the second argument, I’m again going to be taking the logarithm base 10, in this case of the number 500, and I would like to have 10 elements in my array.
对于第二个参数,我将再次取以10为底的对数,在这个数字为500的情况下,我希望数组中有10个元素。
When I run this, the output is exactly what’s expected.
当我运行它时,输出正好是预期的。
Often we need to know the shape of an array or the number of elements in an array.
通常我们需要知道数组的形状或数组中元素的数量。
You can check the shape of an array using shape.
可以使用shape检查数组的形状。
I’m going to define the two dimensional array x,and to find out the shape of the array I can type x.shape.
我将定义二维数组x,为了找出数组的形状,我可以输入x.shape。
You can check the number of elements of an array with size.
可以使用大小检查数组的元素数。
So in this case, I can type x.size and I find out that I have six elements in my array.
在这个例子中,我可以输入x.size,我发现我的数组中有六个元素。
Notice that you don’t have parentheses following the shape or size in the above examples.
请注意,在上述示例中,形状或大小后面没有括号。
This is because shape and size are data attributes, not methods of the arrays.
这是因为形状和大小是数据属性,而不是数组的方法。
Sometimes we need to examine whether any or all elements of an array fulfill some logical condition.
有时我们需要检查数组的任何或所有元素是否满足某种逻辑条件。
Let’s generate a small one d array and check two things.
让我们生成一个小的一维数组并检查两件事。
First, if any of the entries are greater than 0.9,and second, if all of the entries are greater than or equal to 0.1.
首先,如果任何条目大于0.9,然后,如果所有条目都大于或等于0.1。
You may remember how we generated random numbers using the random module.
您可能还记得我们是如何使用随机模块生成随机数的。
NumPy has its own random module.
NumPy有自己的随机模块。
And in this case, we’re going to be generating 10 random numbers drawn from the standard uniform distribution, meaning from the interval from 0 to 1.
在这种情况下,我们将从标准均匀分布中生成10个随机数,也就是从0到1的区间。
I’m going to call this vector or array “x”.
我将把这个向量或数组称为“x”。
And now we can use the np.any function to find out if any of the elements of x are greater than 0.9.
现在我们可以使用np.any函数来确定x的元素是否大于0.9。
And the answer is this case true.
答案是这种情况是正确的。
I can then use the np.all function to find out if all of the elements in the array are greater than or equal to 0.1.
然后我可以使用np.all函数来确定数组中的所有元素是否大于或等于0.1。
In this case, the answer is true.
在这种情况下,答案是正确的。
To make sense of these results, we can print out the content of x.
为了理解这些结果,我们可以打印出x的内容。
By looking at the second element on the second row, which is to 0.9216 and so on, we have an element which is greater than 0.9.
通过查看第二行的第二个元素,即0.9216,依此类推,我们得到了一个大于0.9的元素。
Therefore, we have at least one of those elements,and therefore np.any returns true to us.
因此,我们至少有一个元素,因此np.any返回给我们。
If you look at all of the elements in our vector x, you see that all of them are indeed greater than 0.1.
如果你看向量x中的所有元素,你会发现它们都大于0.1。
And that’s why np.all also returns true.
这就是为什么np.all也返回true。
Note that the output is either true or false for the whole array.
请注意,整个数组的输出为true或false。
Either there is or is not one or more entries that are greater than 0.9.
存在或不存在一个或多个大于0.9的条目。
Also, either all entries are greater or equal to 0.1 or they are not.
此外,所有条目要么大于或等于0.1,要么不大于或等于0.1。
This is why the output is just a single true or false.
这就是为什么输出只有一个true或false。


 上一篇
Introduction to Matplotlib and Pyplot-Matplotlib 和 Pyplot 介绍 Introduction to Matplotlib and Pyplot-Matplotlib 和 Pyplot 介绍
2.3.1: Introduction to Matplotlib and Pyplot-Matplotlib 和 Pyplot 介绍Matplotlib is a Python plotting library that produces
下一篇 
Customizing Your Plots-自定义绘图 Customizing Your Plots-自定义绘图
2.3.2: Customizing Your Plots-自定义绘图There are a few important elements that can be easily added to plots.有几个重要元素可以轻松添加到绘图
  目录