Skip to content

更新以适配python3 #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions docs/article/basics/understanding_numpy.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,31 @@ NumPy提供的最重要的数据结构是一个称为NumPy数组的强大对象
```python
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])
print my_array
print(my_array)
```

在上面的简单示例中,我们首先使用import numpy作为np导入NumPy库。然后,我们创建了一个包含5个整数的简单NumPy数组,然后我们将其打印出来。继续在自己的机器上试一试。在看 “NumPy安装” 部分下面的步骤的时候,请确保已在计算机中安装了NumPy。

现在让我们看看我们可以用这个特定的NumPy数组能做些什么。

```python
print my_array.shape
print(my_array.shape)
```

它会打印我们创建的数组的形状:``(5, )``。意思就是 my_array 是一个包含5个元素的数组。

我们也可以打印各个元素。就像普通的Python数组一样,NumPy数组的起始索引编号为0。

```python
print my_array[0]
print my_array[1]
print(my_array[0])
print(my_array[1])
```

上述命令将分别在终端上打印1和2。我们还可以修改NumPy数组的元素。例如,假设我们编写以下2个命令:

```python
my_array[0] = -1
print my_array
print(my_array)
```

我们将在屏幕上看到:``[-1,2,3,4,5]``。
Expand All @@ -66,22 +66,23 @@ print my_array

```python
my_new_array = np.zeros((5))
print my_new_array
print(my_new_array)
```

我们将看到输出了 ``[0., 0., 0., 0., 0.] ``。与 ``np.zeros`` 类似,我们也有 ``np.ones``。 如果我们想创建一个随机值数组怎么办?

```python
my_random_array = np.random.random((5))
print my_random_array
print(my_random_array)
```

我们得到的输出看起来像 [0.22051844 0.35278286 0.11342404 0.79671772 0.62263151] 这样的数据。你获得的输出可能会有所不同,因为我们使用的是随机函数,它为每个元素分配0到1之间的随机值。

现在让我们看看如何使用NumPy创建二维数组。

```python
my_2d_array = np.zeros((2, 3)) print my_2d_array
my_2d_array = np.zeros((2, 3))
print(my_2d_array)
```

这将在屏幕上打印以下内容:
Expand All @@ -95,7 +96,8 @@ my_2d_array = np.zeros((2, 3)) print my_2d_array
猜猜以下代码的输出结果如何:

```python
my_2d_array_new = np.ones((2, 4)) print my_2d_array_new
my_2d_array_new = np.ones((2, 4))
print(my_2d_array_new)
```

这里是:
Expand All @@ -110,15 +112,15 @@ my_2d_array_new = np.ones((2, 4)) print my_2d_array_new

```python
my_array = np.array([[4, 5], [6, 1]])
print my_array[0][1]
print(my_array[0][1])
```

上面的代码片段的输出是5,因为它是索引0行和索引1列中的元素。

你还可以按如下方式打印my_array的形状:

```python
print my_array.shape
print(my_array.shape)
```

输出为(2, 2),表示数组中有2行2列。
Expand All @@ -133,7 +135,7 @@ NumPy提供了一种提取多维数组的行/列的强大方法。例如,考

```python
my_array_column_2 = my_array[:, 1]
print my_array_column_2
print(my_array_column_2)
```

注意,我们使用了冒号(``:``)而不是行号,而对于列号,我们使用了值``1``,最终输出是:``[5, 1]``。
Expand All @@ -152,10 +154,10 @@ sum = a + b
difference = a - b
product = a * b
quotient = a / b
print "Sum = \n", sum
print "Difference = \n", difference
print "Product = \n", product
print "Quotient = \n", quotient
print("Sum = \n", sum)
print("Difference = \n", difference)
print("Product = \n", product)
print("Quotient = \n", quotient)

# The output will be as follows:

Expand All @@ -169,7 +171,7 @@ Quotient = [[0.2 0.33333333] [0.42857143 0.5 ]]

```python
matrix_product = a.dot(b)
print "Matrix Product = ", matrix_product
print("Matrix Product = ", matrix_product)
```

输出将是:
Expand All @@ -188,4 +190,4 @@ print "Matrix Product = ", matrix_product

## 文章出处

由NumPy中文文档翻译,原作者为 [Vijay Singh](https://dzone.com/users/3404598/vijayhackr.html),翻译至:[https://dzone.com/article/understanding-numpy](https://dzone.com/article/understanding-numpy)。
由NumPy中文文档翻译,原作者为 [Vijay Singh](https://dzone.com/users/3404598/vijayhackr.html),翻译至:[https://dzone.com/article/understanding-numpy](https://dzone.com/article/understanding-numpy)。