Skip to content

Commit

Permalink
update for np.sum
Browse files Browse the repository at this point in the history
  • Loading branch information
llinjupt committed May 10, 2019
1 parent d651cf8 commit d78ad4d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Binary file modified imgs/numpy/narraytypes.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified imgs/numpy/sum.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified imgs/numpy/sumaxis1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 29 additions & 2 deletions numpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Matplotlib 是 Python 编程语言及其数值数学扩展包 NumPy 的可视化
>>>
1.13.1
这里不得不提到 Octave(模仿 Matlab 的 GNU 开源软件),它是另一个在科学计算领域应用广泛的开发环境,作者 John W. Eaton。实际上 Octave 之所以易于使用,从而也被广泛应用在教学领域,在于它沿用 Matlab 在各个轴上的定义与笛卡尔坐标系保持一致,从而避免了学习梯度的陡升。掌握 NumPy 的关键就是理解不同维度数组的轴,以及建立在轴上的复杂变换(高维数组上轴的定义与 Octave 不同,这让 NumPy 看起来有些混乱)。
这里不得不提到 Octave(模仿 Matlab 的 GNU 开源软件),它是另一个在科学计算领域应用广泛的开发环境,作者 John W. Eaton。实际上 Octave 之所以易于使用,从而也被广泛应用在教学领域,在于它沿用 Matlab 在各个轴上的定义与笛卡尔坐标系(C语言的多维数组索引)保持一致,从而避免了学习梯度的陡升。掌握 NumPy 的关键就是理解不同维度数组的轴,以及建立在轴上的复杂变换(高维数组上轴的定义与 Octave 不同,这让 NumPy 看起来有些混乱)。

这里使用 2D 数组在 Octave 和 NumPy 上做一对比:

Expand Down Expand Up @@ -64,7 +64,7 @@ Matplotlib 是 Python 编程语言及其数值数学扩展包 NumPy 的可视化
:linenos:
:lineno-start: 0
octave:33> A = reshape ([0, 1, 2, 3, 4, 5, 6, 7], 2, 2, 2)
octave:1> A = reshape ([0, 1, 2, 3, 4, 5, 6, 7], 2, 2, 2)
A =
ans(:,:,1) =
Expand All @@ -79,6 +79,33 @@ Matplotlib 是 Python 编程语言及其数值数学扩展包 NumPy 的可视化
octave 的下标总是从 1 开始,常规思维 [1,1,2](对应 Numpy 的索引为 [0,0,1])的元素是什么?

.. code-block:: python
:linenos:
:lineno-start: 0
# octave 通过圆括号 '()' 进行索引
octave:3> A(1,1,2)
ans = 4
这里不对 octave 如何进行 reshape 进行深入分析。这里的重点在于 [1,1,1] 对应了 x, y 和 z,通过直观思考就可以得出值为 4。而在 Numpy 却不同:

.. code-block:: python
:linenos:
:lineno-start: 0
In [1]: a
Out[1]:
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
In [2]: a[0,0,1]
Out[2]: 1
你可能会认为结果是 4,而事实并非如此,在数组属性一节会分析这一令人迷惑的问题。

NumPy 另一个令人诟病的地方就是不支持列向量,也即只能使用 Nx1 的 2D 数组来模拟,而行向量却是 1D 的,这看起来非常不合理(Stupid!),所以没有任何经验的人使用 Octave 并基于正常思维掌握它是非常迅速的,而要掌握 NumPy,使用直觉思维是不现实的,你在尝试解读代码时必须要经过一个短暂的转换思考过程。

数组属性和类型
Expand Down

0 comments on commit d78ad4d

Please sign in to comment.