-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumPy.py
167 lines (137 loc) · 4.38 KB
/
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# NumPy Tutorial
import numpy as np
list1 = [1, 2, 3]
array1 = np.array([1, 2, 3])
print(list1) # [1, 2, 3]
print(array1) # [1 2 3]
# First different between list & array is [array >> Unpacking Elements]
array1 = np.array([1, 2.5, 3, 4])
print(array1) # [1. 2.5 3. 4. ]
# Second is Array Must all elements be the same type [all output float]
# Benefits:
# Array more fast than list
# Offer math Operations on matrices
# Offer statistics operations also
# dtype attribute determine the datatype of the Array
array1 = np.array([1, 2, 3, 4], dtype="float32")
print(array1) # [1. 2. 3. 4.]
# Zeroes & ones Arrays
array1 = np.zeros(10, dtype="int")
array2 = np.ones(10, dtype="int")
print(array1) # [0 0 0 0 0 0 0 0 0 0]
print(array2) # [1 1 1 1 1 1 1 1 1 1]
# 2-D Zeros & Ones Arrays
array1 = np.zeros((3, 5), dtype="int")
print(array1)
# [[0 0 0 0 0]
# [0 0 0 0 0]
# [0 0 0 0 0]]
# You can create Matrix in 3 or 4 or more Dimension
array1 = np.zeros((3, 5, 2, 4), dtype="int")
print(array1)
# Any Value or Array
array1 = np.full((3, 4), 3.14)
print(array1)
# [[3.14 3.14 3.14 3.14]
# [3.14 3.14 3.14 3.14]
# [3.14 3.14 3.14 3.14]]
# Create Array in Range
array1 = np.arange(0, 10)
print(array1) # [0 1 2 3 4 5 6 7 8 9]
# Create Array in Range [default start = 0]
array1 = np.arange(10)
print(array1) # [0 1 2 3 4 5 6 7 8 9]
# Create Array in Range with step size
array1 = np.arange(0, 20, 2)
print(array1) # [ 0 2 4 6 8 10 12 14 16 18]
# Create Array with Random Elements between (0 : 1) :
array1 = np.random.random((3, 3))
print(array1)
# [[0.59843386 0.567184 0.93265006]
# [0.6313945 0.40038906 0.48124954]
# [0.49761184 0.51442482 0.99411807]]
# Create Array with Random Elements but normal distribution:
array1 = np.random.normal(0, 1, (3, 3)) # [1 is mean, 3 is variance]
print(array1)
# [[ 1.11893306 -0.15063746 0.61778381]
# [ 0.27574819 -0.04094748 -1.46619869]
# [-0.43030399 0.19351541 -0.50513429]]
# Create Array with Random integers:
array1 = np.random.randint(0, 10, (3, 3))
print(array1)
# [[7 0 9]
# [8 5 9]
# [4 5 3]]
# Create identity matrix
array1 = np.eye(4)
print(array1)
# [[1. 0. 0. 0.]
# [0. 1. 0. 0.]
# [0. 0. 1. 0.]
# [0. 0. 0. 1.]]
# Create empty Array:
array1 = np.empty((2, 3))
print(array1)
# [[6.24649640e-312 6.24644453e-312 6.24644453e-312]
# [6.24644454e-312 6.24644450e-312 6.24644454e-312]]
# Array Attributes
array1 = np.zeros((3, 4, 5))
print(f"dim = {array1.ndim}, and Shape is {array1.shape}, and size is {array1.size}")
print(f"Datatype of Array is : {array1.dtype}")
# Output: dim = 3, and Shape is (3, 4, 5), and size is 60
# Datatype of Array is : float64
# Array Indexing:
array1 = np.array([1, 3, 5, 7])
print(array1[2]) # 5
# 2D Array Indexing & above:
array1 = np.array([[1, 3, 5, 7], [2, 4, 6, 8], [0, 3, 6, 9]])
print(array1[1, -1]) # 8
# Array Slicing: Accessing Sub-arrays [Like Matlab]
array1 = np.random.randint(2, 20, 20)
print(array1) # [ 4 5 14 5 11 10 14 14 7 13 17 19 14 8 2 15 9 5 10 5]
print(array1[1:12:2]) # [ 5 5 10 14 13 19]
# Accessing array rows and columns:
array1 = np.array([[12, 5, 2, 4], [7, 6, 8, 8], [1, 6, 7, 7]])
print(array1[0, :]) # access row : [12 5 2 4]
print(array1[0]) # access row : [12 5 2 4]
print(array1[:, 0]) # access column : [12 7 1]
# Sub-arrays as no-copy views
array1 = np.array([[12, 5, 2, 4], [7, 6, 8, 8], [1, 6, 7, 7]])
array1_sub = array1[:2, :2]
print(array1_sub)
array1_sub[0, 0] = 99
print(array1_sub)
print(array1)
# Change in array1 or array1_sub happen in both
# Sub-arrays as copy views
array1 = np.array([[12, 5, 2, 4], [7, 6, 8, 8], [1, 6, 7, 7]])
array1_sub = array1[:2, :2].copy()
print(array1_sub)
array1_sub[0, 0] = 99
print(array1_sub)
print(array1)
# Change in array1 or array1_sub does not depend on each others
# Reshaping of array
array1 = np.array([12, 5, 2, 4, 7, 6, 8, 8, 1, 6, 7, 7]).reshape((6, 2))
print(array1)
# row vector via reshape
array1 = np.array([1, 2, 3])
print(array1.reshape((1, 3)).shape)
# Array Concatenation
array1 = np.array([1, 2, 3])
array2 = np.array([3, 2, 1])
print(array1)
print(array2)
print(np.concatenate([array1, array2]))
print(np.concatenate((array1, array2)))
# concatenate along the first axis
array1 = np.full((2, 3), 1)
array2 = np.full((2, 3), 9)
print(np.concatenate([array1, array2], axis=0))
# [[1 1 1]
# [1 1 1]
# [9 9 9]
# [9 9 9]]
print(np.concatenate([array1, array2], axis=1))
# [[1 1 1 9 9 9]
# [1 1 1 9 9 9]]