-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path18_numpy.py
100 lines (79 loc) · 2.26 KB
/
18_numpy.py
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
100
import numpy as np
np.random.seed(0) # seed for reproducibility
x1 = np.random.randint(10, size=6) # One-dimensional array
x2 = np.random.randint(10, size=(3, 4)) # Two-dimensional array
x3 = np.random.randint(10, size=(3, 4, 5)) # Three-dimensional array
print(x1)
#array([5, 0, 3, 3, 7, 9])
#Multi-Dimensional
print(x2)
"""
something like
array([[3, 5, 2, 4],
[7, 6, 8, 8],
[1, 6, 7, 7]])
"""
print(x2[0, 0])
#3 and more examples
x2[0, 0] = 12
print(x2)
"""
array([[12, 5, 2, 4],
[ 7, 6, 8, 8],
[ 1, 6, 7, 7]])
Keep in mind that, unlike Python lists, NumPy arrays have a fixed type.
This means, for example, that if you attempt to insert a floating-point value to an integer array,
the value will be silently truncated. Don't be caught unaware by this behavior!
"""
x1[0] = 3.14159 # this will be truncated!
print(x1)
#array([3, 0, 3, 3, 7, 9])
print("x3 ndim: ", x3.ndim)
#x3 ndim: 3
print("x3 shape:", x3.shape)
#x3 shape: (3, 4, 5)
print("x3 size: ", x3.size)
#x3 size: 60
print("dtype:", x3.dtype)
#dtype: int64
print("itemsize:", x3.itemsize, "bytes")
#itemsize: 8 bytes
print("nbytes:", x3.nbytes, "bytes")
#nbytes: 480 bytes
#Array Slicing: Accessing Subarrays
#x[start:stop:step]
"""
If any of these are unspecified, they default to the values start=0, stop=size of dimension, step=1.
We'll take a look at accessing sub-arrays in one dimension and in multiple dimensions.
"""
#One-dimensional subarrays
x = np.arange(10)
print(x)
#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
"""
np.arange(3.0)
array([ 0., 1., 2.])
np.arange(3,7)
array([3, 4, 5, 6])
np.arange(3,7,2)
array([3, 5])
"""
print(x[:5]) # first five elements
#array([0, 1, 2, 3, 4])
print(x[5:]) # elements after index 5
#array([5, 6, 7, 8, 9])
print(x[4:7]) # middle sub-array
#array([4, 5, 6])
print(x[::2]) # every other element
#array([0, 2, 4, 6, 8])
print(x[1::2]) # every other element, starting at index 1
#array([1, 3, 5, 7, 9])
"""
A potentially confusing case is when the step value is negative.
In this case, the defaults for start and stop are swapped.
This becomes a convenient way to reverse an array:
"""
print(x[::-1]) # all elements, reversed
#array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
print(x[5::-2]) # reversed every other from index 5
#array([5, 3, 1])