"As mentioned several times by now, to get good performance we should try to avoid looping over elements in our vectors and matrices, and instead use vectorized algorithms. The first step in converting a scalar algorithm to a vectorized algorithm is to make sure that the functions we write work with vector inputs."
"We can also implement the function to accept a vector input from the beginning (requires more effort but might give better performance):"
"我们也可以实现从一开始就接受矢量输入的函数(需要更多的计算,但可能会有更好的性能):"
]
},
{
@@ -4509,7 +4509,7 @@
"source": [
"def Theta(x):\n",
" \"\"\"\n",
" Vector-aware implemenation of the Heaviside step function.\n",
" Heaviside阶跃函数的矢量感知实现。\n",
" \"\"\"\n",
" return 1 * (x >= 0)"
]
@@ -4581,7 +4581,7 @@
}
],
"source": [
"# still works for scalars as well\n",
"# 同样适用于标量\n",
"Theta(-1.2), Theta(2.6)"
]
},
@@ -4589,14 +4589,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using arrays in conditions"
"## 在条件中使用数组"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When using arrays in conditions,for example `if` statements and other boolean expressions, one needs to use `any` or `all`, which requires that any or all elements in the array evalutes to `True`:"
"Since Numpy arrays are *statically typed*, the type of an array does not change once created. But we can explicitly cast an array of some type to another using the `astype` functions (see also the similar `asarray` function). This always create a new array of new type:"