mirror of
https://github.com/apachecn/ailearning.git
synced 2026-04-23 18:13:05 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -6,14 +6,14 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
import numpy as np
|
||||
|
||||
```
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = np.array([0,1,2])
|
||||
b = np.array([2,3,4])
|
||||
|
||||
@@ -23,7 +23,7 @@ np.add(a, b)
|
||||
|
||||
Out[2]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([2, 4, 6])
|
||||
```
|
||||
|
||||
@@ -31,14 +31,14 @@ array([2, 4, 6])
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
dir(np.add)
|
||||
|
||||
```
|
||||
|
||||
Out[3]:
|
||||
|
||||
```
|
||||
```py
|
||||
['__call__',
|
||||
'__class__',
|
||||
'__delattr__',
|
||||
@@ -76,7 +76,7 @@ Out[3]:
|
||||
|
||||
## reduce 方法
|
||||
|
||||
```
|
||||
```py
|
||||
op.reduce(a)
|
||||
```
|
||||
|
||||
@@ -86,7 +86,7 @@ add 作用到一维数组上相当于求和:
|
||||
|
||||
$$ \begin{align} y & = add.recuce(a) \\ & = a[0] + a[1] + ... + a[N-1] \\ & = \sum_{n=0}^{N-1} a[n] \end{align} $$In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = np.array([1,2,3,4])
|
||||
|
||||
np.add.reduce(a)
|
||||
@@ -95,7 +95,7 @@ np.add.reduce(a)
|
||||
|
||||
Out[4]:
|
||||
|
||||
```
|
||||
```py
|
||||
10
|
||||
```
|
||||
|
||||
@@ -103,7 +103,7 @@ Out[4]:
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = np.array([[1,2,3],[4,5,6]])
|
||||
|
||||
np.add.reduce(a)
|
||||
@@ -112,7 +112,7 @@ np.add.reduce(a)
|
||||
|
||||
Out[5]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([5, 7, 9])
|
||||
```
|
||||
|
||||
@@ -120,14 +120,14 @@ array([5, 7, 9])
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
np.add.reduce(a, 1)
|
||||
|
||||
```
|
||||
|
||||
Out[6]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ 6, 15])
|
||||
```
|
||||
|
||||
@@ -135,7 +135,7 @@ array([ 6, 15])
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = np.array(['ab', 'cd', 'ef'], np.object)
|
||||
|
||||
np.add.reduce(a)
|
||||
@@ -144,7 +144,7 @@ np.add.reduce(a)
|
||||
|
||||
Out[7]:
|
||||
|
||||
```
|
||||
```py
|
||||
'abcdef'
|
||||
```
|
||||
|
||||
@@ -152,7 +152,7 @@ Out[7]:
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = np.array([1,1,0,1])
|
||||
|
||||
np.logical_and.reduce(a)
|
||||
@@ -161,26 +161,26 @@ np.logical_and.reduce(a)
|
||||
|
||||
Out[8]:
|
||||
|
||||
```
|
||||
```py
|
||||
False
|
||||
```
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
np.logical_or.reduce(a)
|
||||
|
||||
```
|
||||
|
||||
Out[9]:
|
||||
|
||||
```
|
||||
```py
|
||||
True
|
||||
```
|
||||
|
||||
## accumulate 方法
|
||||
|
||||
```
|
||||
```py
|
||||
op.accumulate(a)
|
||||
```
|
||||
|
||||
@@ -192,7 +192,7 @@ $$ \begin{align} y & = add.accumulate(a) \\ & = \left[\sum_{n=0}^{0} a[n], \sum_
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = np.array([1,2,3,4])
|
||||
|
||||
np.add.accumulate(a)
|
||||
@@ -201,13 +201,13 @@ np.add.accumulate(a)
|
||||
|
||||
Out[10]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ 1, 3, 6, 10])
|
||||
```
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = np.array(['ab', 'cd', 'ef'], np.object)
|
||||
|
||||
np.add.accumulate(a)
|
||||
@@ -216,13 +216,13 @@ np.add.accumulate(a)
|
||||
|
||||
Out[11]:
|
||||
|
||||
```
|
||||
```py
|
||||
array(['ab', 'abcd', 'abcdef'], dtype=object)
|
||||
```
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = np.array([1,1,0,1])
|
||||
|
||||
np.logical_and.accumulate(a)
|
||||
@@ -231,26 +231,26 @@ np.logical_and.accumulate(a)
|
||||
|
||||
Out[12]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ True, True, False, False], dtype=bool)
|
||||
```
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
np.logical_or.accumulate(a)
|
||||
|
||||
```
|
||||
|
||||
Out[13]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ True, True, True, True], dtype=bool)
|
||||
```
|
||||
|
||||
## reduceat 方法
|
||||
|
||||
```
|
||||
```py
|
||||
op.reduceat(a, indices)
|
||||
```
|
||||
|
||||
@@ -258,7 +258,7 @@ op.reduceat(a, indices)
|
||||
|
||||
$$ \begin{align} y & = add.reduceat(a, indices) \\ & = \left[\sum_{n=indice[0]}^{indice[1]-1} a[n], \sum_{n=indice[1]}^{indice[2]-1} a[n], ..., \sum_{n=indice[-1]}^{N-1} a[n]\right] \end{align} $$In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = np.array([0, 10, 20, 30, 40, 50])
|
||||
indices = np.array([1,4])
|
||||
|
||||
@@ -268,7 +268,7 @@ np.add.reduceat(a, indices)
|
||||
|
||||
Out[14]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([60, 90])
|
||||
```
|
||||
|
||||
@@ -276,7 +276,7 @@ array([60, 90])
|
||||
|
||||
## outer 方法
|
||||
|
||||
```
|
||||
```py
|
||||
op.outer(a, b)
|
||||
```
|
||||
|
||||
@@ -284,7 +284,7 @@ op.outer(a, b)
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = np.array([0,1])
|
||||
b = np.array([1,2,3])
|
||||
|
||||
@@ -294,7 +294,7 @@ np.add.outer(a, b)
|
||||
|
||||
Out[15]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[1, 2, 3],
|
||||
[2, 3, 4]])
|
||||
```
|
||||
@@ -303,14 +303,14 @@ array([[1, 2, 3],
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
np.add.outer(b, a)
|
||||
|
||||
```
|
||||
|
||||
Out[16]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[1, 2],
|
||||
[2, 3],
|
||||
[3, 4]])
|
||||
|
||||
Reference in New Issue
Block a user