2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -12,7 +12,7 @@
In [1]:
```
```py
import numpy as np
```
@@ -25,7 +25,7 @@ import numpy as np
In [2]:
```
```py
a = np.array([1.0,2.0,3.0,4.0], np.float32)
```
@@ -34,14 +34,14 @@ a = np.array([1.0,2.0,3.0,4.0], np.float32)
In [3]:
```
```py
a.view(np.complex64)
```
Out[3]:
```
```py
array([ 1.+2.j, 3.+4.j], dtype=complex64)
```
@@ -63,21 +63,21 @@ array([ 1.+2.j, 3.+4.j], dtype=complex64)
In [4]:
```
```py
my_dtype = np.dtype([('mass', 'float32'), ('vol', 'float32')])
```
In [5]:
```
```py
a.view(my_dtype)
```
Out[5]:
```
```py
array([(1.0, 2.0), (3.0, 4.0)],
dtype=[('mass', '<f4'), ('vol', '<f4')])
```
@@ -90,14 +90,14 @@ array([(1.0, 2.0), (3.0, 4.0)],
In [6]:
```
```py
my_data = np.array([(1,1), (1,2), (2,1), (1,3)], my_dtype)
print my_data
```
```
```py
[(1.0, 1.0) (1.0, 2.0) (2.0, 1.0) (1.0, 3.0)]
```
@@ -106,14 +106,14 @@ print my_data
In [7]:
```
```py
my_data[0]
```
Out[7]:
```
```py
(1.0, 1.0)
```
@@ -121,14 +121,14 @@ Out[7]:
In [8]:
```
```py
my_data[0]['vol']
```
Out[8]:
```
```py
1.0
```
@@ -136,14 +136,14 @@ Out[8]:
In [9]:
```
```py
my_data['mass']
```
Out[9]:
```
```py
array([ 1., 1., 2., 1.], dtype=float32)
```
@@ -151,14 +151,14 @@ array([ 1., 1., 2., 1.], dtype=float32)
In [10]:
```
```py
my_data.sort(order=('vol', 'mass'))
print my_data
```
```
```py
[(1.0, 1.0) (2.0, 1.0) (1.0, 2.0) (1.0, 3.0)]
```
@@ -167,7 +167,7 @@ print my_data
In [11]:
```
```py
person_dtype = np.dtype([('name', 'S10'), ('age', 'int'), ('weight', 'float')])
```
@@ -176,14 +176,14 @@ person_dtype = np.dtype([('name', 'S10'), ('age', 'int'), ('weight', 'float')])
In [12]:
```
```py
person_dtype.itemsize
```
Out[12]:
```
```py
22
```
@@ -191,7 +191,7 @@ Out[12]:
In [13]:
```
```py
people = np.empty((3,4), person_dtype)
```
@@ -200,7 +200,7 @@ people = np.empty((3,4), person_dtype)
In [14]:
```
```py
people['name'] = [['Brad', 'Jane', 'John', 'Fred'],
['Henry', 'George', 'Brain', 'Amy'],
['Ron', 'Susan', 'Jennife', 'Jill']]
@@ -209,7 +209,7 @@ people['name'] = [['Brad', 'Jane', 'John', 'Fred'],
In [15]:
```
```py
people['age'] = [[33, 25, 47, 54],
[29, 61, 32, 27],
[19, 33, 18, 54]]
@@ -218,7 +218,7 @@ people['age'] = [[33, 25, 47, 54],
In [16]:
```
```py
people['weight'] = [[135., 105., 255., 140.],
[154., 202., 137., 187.],
[188., 135., 88., 145.]]
@@ -227,12 +227,12 @@ people['weight'] = [[135., 105., 255., 140.],
In [17]:
```
```py
print people
```
```
```py
[[('Brad', 33, 135.0) ('Jane', 25, 105.0) ('John', 47, 255.0)
('Fred', 54, 140.0)]
[('Henry', 29, 154.0) ('George', 61, 202.0) ('Brain', 32, 137.0)
@@ -244,14 +244,14 @@ print people
In [18]:
```
```py
people[-1,-1]
```
Out[18]:
```
```py
('Jill', 54, 145.0)
```
@@ -261,7 +261,7 @@ Out[18]:
In [19]:
```
```py
%%writefile people.txt
name age weight
amy 11 38.2
@@ -270,7 +270,7 @@ bill 12 21.2
```
```
```py
Writing people.txt
```
@@ -279,7 +279,7 @@ Writing people.txt
In [20]:
```
```py
person_dtype = np.dtype([('name', 'S10'), ('age', 'int'), ('weight', 'float')])
people = np.loadtxt('people.txt',
@@ -292,7 +292,7 @@ people
Out[20]:
```
```py
array([('amy', 11, 38.2), ('john', 10, 40.3), ('bill', 12, 21.2)],
dtype=[('name', 'S10'), ('age', '<i4'), ('weight', '<f8')])
```
@@ -301,14 +301,14 @@ array([('amy', 11, 38.2), ('john', 10, 40.3), ('bill', 12, 21.2)],
In [21]:
```
```py
people['name']
```
Out[21]:
```
```py
array(['amy', 'john', 'bill'],
dtype='|S10')
```
@@ -317,7 +317,7 @@ array(['amy', 'john', 'bill'],
In [22]:
```
```py
import os
os.remove('people.txt')
@@ -327,7 +327,7 @@ os.remove('people.txt')
In [23]:
```
```py
%%writefile wood.csv
item,material,number
100,oak,33
@@ -337,7 +337,7 @@ item,material,number
```
```
```py
Writing wood.csv
```
@@ -346,7 +346,7 @@ Writing wood.csv
In [24]:
```
```py
tree_to_int = dict(oak = 1,
maple=2,
birch=3)
@@ -360,7 +360,7 @@ def convert(s):
In [25]:
```
```py
data = np.genfromtxt('wood.csv',
delimiter=',', # 逗号分隔
dtype=np.int, # 数据类型
@@ -372,14 +372,14 @@ data = np.genfromtxt('wood.csv',
In [26]:
```
```py
data
```
Out[26]:
```
```py
array([(100, 1, 33), (110, 2, 14), (120, 1, 7), (145, 3, 3)],
dtype=[('item', '<i4'), ('material', '<i4'), ('number', '<i4')])
```
@@ -388,14 +388,14 @@ array([(100, 1, 33), (110, 2, 14), (120, 1, 7), (145, 3, 3)],
In [27]:
```
```py
data['material']
```
Out[27]:
```
```py
array([1, 2, 1, 3])
```
@@ -403,7 +403,7 @@ array([1, 2, 1, 3])
In [28]:
```
```py
os.remove('wood.csv')
```
@@ -419,7 +419,7 @@ os.remove('wood.csv')
In [29]:
```
```py
particle_dtype = np.dtype([('position', [('x', 'float'),
('y', 'float')]),
('mass', 'float')
@@ -431,7 +431,7 @@ particle_dtype = np.dtype([('position', [('x', 'float'),
In [30]:
```
```py
%%writefile data.txt
2.0 3.0 42.0
2.1 4.3 32.5
@@ -440,7 +440,7 @@ In [30]:
```
```
```py
Overwriting data.txt
```
@@ -449,21 +449,21 @@ Overwriting data.txt
In [31]:
```
```py
data = np.loadtxt('data.txt', dtype=particle_dtype)
```
In [32]:
```
```py
data
```
Out[32]:
```
```py
array([((2.0, 3.0), 42.0), ((2.1, 4.3), 32.5), ((1.2, 4.6), 32.3),
((4.5, -6.4), 23.3)],
dtype=[('position', [('x', '<f8'), ('y', '<f8')]), ('mass', '<f8')])
@@ -473,14 +473,14 @@ array([((2.0, 3.0), 42.0), ((2.1, 4.3), 32.5), ((1.2, 4.6), 32.3),
In [33]:
```
```py
data['position']['x']
```
Out[33]:
```
```py
array([ 2\. , 2.1, 1.2, 4.5])
```
@@ -488,7 +488,7 @@ array([ 2\. , 2.1, 1.2, 4.5])
In [34]:
```
```py
os.remove('data.txt')
```