mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-11 11:07:31 +08:00
2020-10-19 21:08:55
This commit is contained in:
216
docs/da/038.md
216
docs/da/038.md
@@ -62,7 +62,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
from numpy import *
|
||||
|
||||
```
|
||||
@@ -71,7 +71,7 @@ from numpy import *
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = array([[0, 1, 2, 3], [4, 5, 6, 7]])
|
||||
a
|
||||
|
||||
@@ -79,7 +79,7 @@ a
|
||||
|
||||
Out[2]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[0, 1, 2, 3],
|
||||
[4, 5, 6, 7]])
|
||||
```
|
||||
@@ -88,14 +88,14 @@ array([[0, 1, 2, 3],
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.dtype
|
||||
|
||||
```
|
||||
|
||||
Out[3]:
|
||||
|
||||
```
|
||||
```py
|
||||
dtype('int32')
|
||||
```
|
||||
|
||||
@@ -103,14 +103,14 @@ dtype('int32')
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.shape
|
||||
|
||||
```
|
||||
|
||||
Out[4]:
|
||||
|
||||
```
|
||||
```py
|
||||
(2L, 4L)
|
||||
```
|
||||
|
||||
@@ -118,14 +118,14 @@ Out[4]:
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.size
|
||||
|
||||
```
|
||||
|
||||
Out[5]:
|
||||
|
||||
```
|
||||
```py
|
||||
8
|
||||
```
|
||||
|
||||
@@ -133,14 +133,14 @@ Out[5]:
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.itemsize
|
||||
|
||||
```
|
||||
|
||||
Out[6]:
|
||||
|
||||
```
|
||||
```py
|
||||
4
|
||||
```
|
||||
|
||||
@@ -148,14 +148,14 @@ Out[6]:
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.nbytes
|
||||
|
||||
```
|
||||
|
||||
Out[7]:
|
||||
|
||||
```
|
||||
```py
|
||||
32
|
||||
```
|
||||
|
||||
@@ -163,14 +163,14 @@ Out[7]:
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.ndim
|
||||
|
||||
```
|
||||
|
||||
Out[8]:
|
||||
|
||||
```
|
||||
```py
|
||||
2
|
||||
```
|
||||
|
||||
@@ -178,13 +178,13 @@ Out[8]:
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
for row in a:
|
||||
print row
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[0 1 2 3]
|
||||
[4 5 6 7]
|
||||
|
||||
@@ -194,13 +194,13 @@ for row in a:
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
for elt in a.flat:
|
||||
print elt
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
0
|
||||
1
|
||||
2
|
||||
@@ -216,27 +216,27 @@ for elt in a.flat:
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.flatten()
|
||||
|
||||
```
|
||||
|
||||
Out[11]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([0, 1, 2, 3, 4, 5, 6, 7])
|
||||
```
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.ravel()
|
||||
|
||||
```
|
||||
|
||||
Out[12]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([0, 1, 2, 3, 4, 5, 6, 7])
|
||||
```
|
||||
|
||||
@@ -244,7 +244,7 @@ array([0, 1, 2, 3, 4, 5, 6, 7])
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.resize((4,2))
|
||||
a
|
||||
|
||||
@@ -252,7 +252,7 @@ a
|
||||
|
||||
Out[13]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[0, 1],
|
||||
[2, 3],
|
||||
[4, 5],
|
||||
@@ -263,14 +263,14 @@ array([[0, 1],
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.swapaxes(0,1)
|
||||
|
||||
```
|
||||
|
||||
Out[14]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[0, 2, 4, 6],
|
||||
[1, 3, 5, 7]])
|
||||
```
|
||||
@@ -279,14 +279,14 @@ array([[0, 2, 4, 6],
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.transpose()
|
||||
|
||||
```
|
||||
|
||||
Out[15]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[0, 2, 4, 6],
|
||||
[1, 3, 5, 7]])
|
||||
```
|
||||
@@ -295,21 +295,21 @@ array([[0, 2, 4, 6],
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.T
|
||||
|
||||
```
|
||||
|
||||
Out[16]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[0, 2, 4, 6],
|
||||
[1, 3, 5, 7]])
|
||||
```
|
||||
|
||||
In [17]:
|
||||
|
||||
```
|
||||
```py
|
||||
a2 = array([1,2,3])
|
||||
a2.shape
|
||||
|
||||
@@ -317,13 +317,13 @@ a2.shape
|
||||
|
||||
Out[17]:
|
||||
|
||||
```
|
||||
```py
|
||||
(3L,)
|
||||
```
|
||||
|
||||
In [18]:
|
||||
|
||||
```
|
||||
```py
|
||||
a2.resize((1,3,1))
|
||||
a2.shape
|
||||
|
||||
@@ -331,7 +331,7 @@ a2.shape
|
||||
|
||||
Out[18]:
|
||||
|
||||
```
|
||||
```py
|
||||
(1L, 3L, 1L)
|
||||
```
|
||||
|
||||
@@ -339,7 +339,7 @@ Out[18]:
|
||||
|
||||
In [19]:
|
||||
|
||||
```
|
||||
```py
|
||||
a2 = a2.squeeze()
|
||||
a2.shape
|
||||
|
||||
@@ -347,7 +347,7 @@ a2.shape
|
||||
|
||||
Out[19]:
|
||||
|
||||
```
|
||||
```py
|
||||
(3L,)
|
||||
```
|
||||
|
||||
@@ -357,7 +357,7 @@ Out[19]:
|
||||
|
||||
In [20]:
|
||||
|
||||
```
|
||||
```py
|
||||
b = a.copy()
|
||||
b
|
||||
|
||||
@@ -365,7 +365,7 @@ b
|
||||
|
||||
Out[20]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[0, 1],
|
||||
[2, 3],
|
||||
[4, 5],
|
||||
@@ -376,7 +376,7 @@ array([[0, 1],
|
||||
|
||||
In [21]:
|
||||
|
||||
```
|
||||
```py
|
||||
b[0][0] = -1
|
||||
b # First value changed
|
||||
|
||||
@@ -384,7 +384,7 @@ b # First value changed
|
||||
|
||||
Out[21]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[-1, 1],
|
||||
[ 2, 3],
|
||||
[ 4, 5],
|
||||
@@ -393,14 +393,14 @@ array([[-1, 1],
|
||||
|
||||
In [22]:
|
||||
|
||||
```
|
||||
```py
|
||||
a # original not changed because b is a copy
|
||||
|
||||
```
|
||||
|
||||
Out[22]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[0, 1],
|
||||
[2, 3],
|
||||
[4, 5],
|
||||
@@ -411,7 +411,7 @@ array([[0, 1],
|
||||
|
||||
In [23]:
|
||||
|
||||
```
|
||||
```py
|
||||
b.fill(4)
|
||||
b
|
||||
|
||||
@@ -419,7 +419,7 @@ b
|
||||
|
||||
Out[23]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[4, 4],
|
||||
[4, 4],
|
||||
[4, 4],
|
||||
@@ -432,14 +432,14 @@ array([[4, 4],
|
||||
|
||||
In [24]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.tolist()
|
||||
|
||||
```
|
||||
|
||||
Out[24]:
|
||||
|
||||
```
|
||||
```py
|
||||
[[0, 1], [2, 3], [4, 5], [6, 7]]
|
||||
```
|
||||
|
||||
@@ -447,14 +447,14 @@ Out[24]:
|
||||
|
||||
In [25]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.tostring()
|
||||
|
||||
```
|
||||
|
||||
Out[25]:
|
||||
|
||||
```
|
||||
```py
|
||||
'\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07\x00\x00\x00'
|
||||
```
|
||||
|
||||
@@ -462,14 +462,14 @@ Out[25]:
|
||||
|
||||
In [26]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.astype(float)
|
||||
|
||||
```
|
||||
|
||||
Out[26]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[ 0., 1.],
|
||||
[ 2., 3.],
|
||||
[ 4., 5.],
|
||||
@@ -478,7 +478,7 @@ array([[ 0., 1.],
|
||||
|
||||
In [27]:
|
||||
|
||||
```
|
||||
```py
|
||||
b = a.copy()
|
||||
b.byteswap(False)
|
||||
|
||||
@@ -486,7 +486,7 @@ b.byteswap(False)
|
||||
|
||||
Out[27]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[ 0, 16777216],
|
||||
[ 33554432, 50331648],
|
||||
[ 67108864, 83886080],
|
||||
@@ -497,14 +497,14 @@ array([[ 0, 16777216],
|
||||
|
||||
In [28]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.view(dtype=int16)
|
||||
|
||||
```
|
||||
|
||||
Out[28]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[0, 0, 1, 0],
|
||||
[2, 0, 3, 0],
|
||||
[4, 0, 5, 0],
|
||||
@@ -517,7 +517,7 @@ array([[0, 0, 1, 0],
|
||||
|
||||
In [29]:
|
||||
|
||||
```
|
||||
```py
|
||||
b = array([1+2j, 3+4j, 5+6j])
|
||||
b.real
|
||||
|
||||
@@ -525,7 +525,7 @@ b.real
|
||||
|
||||
Out[29]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ 1., 3., 5.])
|
||||
```
|
||||
|
||||
@@ -533,14 +533,14 @@ array([ 1., 3., 5.])
|
||||
|
||||
In [30]:
|
||||
|
||||
```
|
||||
```py
|
||||
b.imag
|
||||
|
||||
```
|
||||
|
||||
Out[30]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ 2., 4., 6.])
|
||||
```
|
||||
|
||||
@@ -548,27 +548,27 @@ array([ 2., 4., 6.])
|
||||
|
||||
In [31]:
|
||||
|
||||
```
|
||||
```py
|
||||
b.conj()
|
||||
|
||||
```
|
||||
|
||||
Out[31]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ 1.-2.j, 3.-4.j, 5.-6.j])
|
||||
```
|
||||
|
||||
In [32]:
|
||||
|
||||
```
|
||||
```py
|
||||
b.conjugate()
|
||||
|
||||
```
|
||||
|
||||
Out[32]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ 1.-2.j, 3.-4.j, 5.-6.j])
|
||||
```
|
||||
|
||||
@@ -578,7 +578,7 @@ array([ 1.-2.j, 3.-4.j, 5.-6.j])
|
||||
|
||||
In [33]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.dump("file.txt")
|
||||
|
||||
```
|
||||
@@ -587,14 +587,14 @@ a.dump("file.txt")
|
||||
|
||||
In [34]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.dumps()
|
||||
|
||||
```
|
||||
|
||||
Out[34]:
|
||||
|
||||
```
|
||||
```py
|
||||
'\x80\x02cnumpy.core.multiarray\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x01\x8a\x01\x04\x8a\x01\x02\x86cnumpy\ndtype\nq\x04U\x02i4K\x00K\x01\x87Rq\x05(K\x03U\x01<NNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00tb\x89U \x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07\x00\x00\x00tb.'
|
||||
```
|
||||
|
||||
@@ -602,7 +602,7 @@ Out[34]:
|
||||
|
||||
In [35]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.tofile('foo.csv', sep=',', format="%s")
|
||||
|
||||
```
|
||||
@@ -613,14 +613,14 @@ a.tofile('foo.csv', sep=',', format="%s")
|
||||
|
||||
In [36]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.nonzero()
|
||||
|
||||
```
|
||||
|
||||
Out[36]:
|
||||
|
||||
```
|
||||
```py
|
||||
(array([0, 1, 1, 2, 2, 3, 3], dtype=int64),
|
||||
array([1, 0, 1, 0, 1, 0, 1], dtype=int64))
|
||||
```
|
||||
@@ -629,7 +629,7 @@ Out[36]:
|
||||
|
||||
In [37]:
|
||||
|
||||
```
|
||||
```py
|
||||
b = array([3,2,7,4,1])
|
||||
b.sort()
|
||||
b
|
||||
@@ -638,7 +638,7 @@ b
|
||||
|
||||
Out[37]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([1, 2, 3, 4, 7])
|
||||
```
|
||||
|
||||
@@ -646,7 +646,7 @@ array([1, 2, 3, 4, 7])
|
||||
|
||||
In [38]:
|
||||
|
||||
```
|
||||
```py
|
||||
b = array([2,3,1])
|
||||
b.argsort(axis=-1)
|
||||
|
||||
@@ -654,7 +654,7 @@ b.argsort(axis=-1)
|
||||
|
||||
Out[38]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([2, 0, 1], dtype=int64)
|
||||
```
|
||||
|
||||
@@ -662,7 +662,7 @@ array([2, 0, 1], dtype=int64)
|
||||
|
||||
In [39]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = array([1,3,4,6])
|
||||
b = array([0,2,5])
|
||||
a.searchsorted(b)
|
||||
@@ -671,7 +671,7 @@ a.searchsorted(b)
|
||||
|
||||
Out[39]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([0, 1, 3], dtype=int64)
|
||||
```
|
||||
|
||||
@@ -681,7 +681,7 @@ array([0, 1, 3], dtype=int64)
|
||||
|
||||
In [40]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = array([[4,1,3],[2,1,5]])
|
||||
a.clip(0,2)
|
||||
|
||||
@@ -689,7 +689,7 @@ a.clip(0,2)
|
||||
|
||||
Out[40]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([[2, 1, 2],
|
||||
[2, 1, 2]])
|
||||
```
|
||||
@@ -698,7 +698,7 @@ array([[2, 1, 2],
|
||||
|
||||
In [41]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = array([1.344, 2.449, 2.558])
|
||||
a.round(decimals=2)
|
||||
|
||||
@@ -706,7 +706,7 @@ a.round(decimals=2)
|
||||
|
||||
Out[41]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ 1.34, 2.45, 2.56])
|
||||
```
|
||||
|
||||
@@ -714,7 +714,7 @@ array([ 1.34, 2.45, 2.56])
|
||||
|
||||
In [42]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = array([[4,1,3],[2,1,5]])
|
||||
a.cumsum(axis=None)
|
||||
|
||||
@@ -722,7 +722,7 @@ a.cumsum(axis=None)
|
||||
|
||||
Out[42]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ 4, 5, 8, 10, 11, 16])
|
||||
```
|
||||
|
||||
@@ -730,14 +730,14 @@ array([ 4, 5, 8, 10, 11, 16])
|
||||
|
||||
In [43]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.cumprod(axis=None)
|
||||
|
||||
```
|
||||
|
||||
Out[43]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ 4, 4, 12, 24, 24, 120])
|
||||
```
|
||||
|
||||
@@ -747,7 +747,7 @@ array([ 4, 4, 12, 24, 24, 120])
|
||||
|
||||
In [44]:
|
||||
|
||||
```
|
||||
```py
|
||||
a = array([[4,1,3],[2,1,5]])
|
||||
a.sum(axis=None)
|
||||
|
||||
@@ -755,7 +755,7 @@ a.sum(axis=None)
|
||||
|
||||
Out[44]:
|
||||
|
||||
```
|
||||
```py
|
||||
16
|
||||
```
|
||||
|
||||
@@ -763,14 +763,14 @@ Out[44]:
|
||||
|
||||
In [45]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.prod(axis=None)
|
||||
|
||||
```
|
||||
|
||||
Out[45]:
|
||||
|
||||
```
|
||||
```py
|
||||
120
|
||||
```
|
||||
|
||||
@@ -778,14 +778,14 @@ Out[45]:
|
||||
|
||||
In [46]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.min(axis=None)
|
||||
|
||||
```
|
||||
|
||||
Out[46]:
|
||||
|
||||
```
|
||||
```py
|
||||
1
|
||||
```
|
||||
|
||||
@@ -793,14 +793,14 @@ Out[46]:
|
||||
|
||||
In [47]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.max(axis=None)
|
||||
|
||||
```
|
||||
|
||||
Out[47]:
|
||||
|
||||
```
|
||||
```py
|
||||
5
|
||||
```
|
||||
|
||||
@@ -808,14 +808,14 @@ Out[47]:
|
||||
|
||||
In [48]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.argmin(axis=None)
|
||||
|
||||
```
|
||||
|
||||
Out[48]:
|
||||
|
||||
```
|
||||
```py
|
||||
1
|
||||
```
|
||||
|
||||
@@ -823,14 +823,14 @@ Out[48]:
|
||||
|
||||
In [49]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.argmax(axis=None)
|
||||
|
||||
```
|
||||
|
||||
Out[49]:
|
||||
|
||||
```
|
||||
```py
|
||||
5
|
||||
```
|
||||
|
||||
@@ -838,14 +838,14 @@ Out[49]:
|
||||
|
||||
In [50]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.ptp(axis=None)
|
||||
|
||||
```
|
||||
|
||||
Out[50]:
|
||||
|
||||
```
|
||||
```py
|
||||
4
|
||||
```
|
||||
|
||||
@@ -853,14 +853,14 @@ Out[50]:
|
||||
|
||||
In [51]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.mean(axis=None)
|
||||
|
||||
```
|
||||
|
||||
Out[51]:
|
||||
|
||||
```
|
||||
```py
|
||||
2.6666666666666665
|
||||
```
|
||||
|
||||
@@ -868,14 +868,14 @@ Out[51]:
|
||||
|
||||
In [52]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.std(axis=None)
|
||||
|
||||
```
|
||||
|
||||
Out[52]:
|
||||
|
||||
```
|
||||
```py
|
||||
1.49071198499986
|
||||
```
|
||||
|
||||
@@ -883,14 +883,14 @@ Out[52]:
|
||||
|
||||
In [53]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.var(axis=None)
|
||||
|
||||
```
|
||||
|
||||
Out[53]:
|
||||
|
||||
```
|
||||
```py
|
||||
2.2222222222222228
|
||||
```
|
||||
|
||||
@@ -898,14 +898,14 @@ Out[53]:
|
||||
|
||||
In [54]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.any(axis=None)
|
||||
|
||||
```
|
||||
|
||||
Out[54]:
|
||||
|
||||
```
|
||||
```py
|
||||
True
|
||||
```
|
||||
|
||||
@@ -913,14 +913,14 @@ True
|
||||
|
||||
In [55]:
|
||||
|
||||
```
|
||||
```py
|
||||
a.all()
|
||||
|
||||
```
|
||||
|
||||
Out[55]:
|
||||
|
||||
```
|
||||
```py
|
||||
True
|
||||
```
|
||||
|
||||
@@ -928,7 +928,7 @@ True
|
||||
|
||||
In [56]:
|
||||
|
||||
```
|
||||
```py
|
||||
import os
|
||||
os.remove('foo.csv')
|
||||
os.remove('file.txt')
|
||||
|
||||
Reference in New Issue
Block a user