-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconstructors.py
More file actions
executable file
·100 lines (52 loc) · 1.21 KB
/
constructors.py
File metadata and controls
executable file
·100 lines (52 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>
# <codecell>
# data types
from start import *
# <codecell>
# from scratch:
print np.ones((3,)) # default dtype is float64
# <codecell>
print np.zeros((2,3), dtype=np.int32)
# <codecell>
print np.empty((3,3), dtype=np.float32) # not initilized!
# <codecell>
# for a range:
# integers:
print np.arange(10)
# <codecell>
# for floats
print np.linspace(0, 1, 11)
# <codecell>
print np.logspace(0, 3, 4)
# <codecell>
# from existing data:
print np.array([(1, 2),
(3, 4.0),
(5, 6)]) # auto-determined dtype
# <codecell>
# maybe an array?
a = np.arange(5)
b = np.asarray(a)
print a is b
# <codecell>
#or not:
a = range(5)
b = np.asarray(a)
print a is b
# <codecell>
print np.ascontiguousarray(a)# forces contiguous datablock
# <codecell>
# from binary data:
s = 'abcdefg'
a = np.frombuffer(s, dtype=np.uint8)
print a
# <codecell>
print np.fromstring('\x12\x04', dtype=np.uint8) # really should be bytes...
# <codecell>
# from (and to) binary file:
a = np.arange(5, dtype=np.float32) # jsut a binary dump!
a.tofile('junk.dat')
# <codecell>
print np.fromfile('junk.dat', dtype=np.float32) # need dtype!
# <codecell>