官术网_书友最值得收藏!

NumPy arrays

After going through the installation of NumPy, it's time to have a look at NumPy arrays. NumPy arrays are more efficient than Python lists when it comes to numerical operations. NumPy arrays are, in fact, specialized objects with extensive optimizations. NumPy code requires less explicit loops than equivalent Python code. This is based on vectorization.

If we go back to high school mathematics, then we should remember the concepts of scalars and vectors. The number 2, for instance, is a scalar. When we add 2 to 2, we are performing scalar addition. We can form a vector out of a group of scalars. In Python programming terms, we will then have a one-dimensional array. This concept can, of course, be extended to higher dimensions. Performing an operation on two arrays, such as addition, can be reduced to a group of scalar operations. In straight Python, we will do that with loops going through each element in the first array and adding it to the corresponding element in the second array. However, this is more verbose than the way it is done in mathematics. In mathematics, we treat the addition of two vectors as a single operation. That's the way NumPy arrays do it too, and there are certain optimizations using low-level C routines that make these basic operations more efficient. We will cover NumPy arrays in more detail in the Chapter 2, NumPy Arrays.

主站蜘蛛池模板: 余姚市| 达日县| 濮阳县| 舟山市| 泰宁县| 牡丹江市| 晋江市| 岚皋县| 淮南市| 陵川县| 岫岩| 浦北县| 泊头市| 河源市| 武义县| 灵丘县| 定西市| 新宁县| 灵台县| 六安市| 景泰县| 通山县| 夹江县| 清新县| 尚义县| 通河县| 永城市| 新龙县| 合阳县| 深水埗区| 台北市| 大埔县| 新郑市| 社旗县| 佳木斯市| 莱西市| 杭州市| 滨海县| 麟游县| 萝北县| 图片|