-
Notifications
You must be signed in to change notification settings - Fork 1
/
NumPy.py
157 lines (137 loc) · 2.48 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
# NumPy (SINGLE DIMENSIONAL ARRAY)
import numpy as np
n1 = np.array([10, 20, 30, 40])
print(n1)
print()
print(type(n1))
print()
import numpy as np
n2 = np.array([[10, 20, 30, 40], [40, 30, 20, 10]])
print(n2)
print()
# numpy with zeros
import numpy as np
n1 = np.zeros((1, 2))
print(n1)
print()
import numpy as np
n1 = np.zeros((5, 5))
print(n1)
print()
# NumPy with same number
import numpy as np
n1 = np.full((2, 2), 10)
print(n1)
print()
# NumPy with arrange
import numpy as np
n1 = np.arange(10, 20)
print(n1)
print()
import numpy as np
n1 = np.arange(10, 50, 5)
print(n1)
print()
# NumPy with random numbers
import numpy as np
n1 = np.random.randint(1, 100, 5)
print(n1)
print()
# NumPy with shapes
import numpy as np
n1 = np.array([[1, 2, 3], [4, 5, 6]])
print(n1.shape)
print()
n1.shape = (3, 2)
print(n1)
print()
# Joining NumPy
import numpy as np
n1 = np.array([10, 20, 30])
n2 = np.array([40, 50, 60])
print(np.vstack((n1, n2)))
print()
print(np.hstack((n1, n2)))
print()
print(np.column_stack((n1, n2)))
print()
import numpy as np
n1 = np.array([10, 20, 30, 40, 50])
n2 = np.array([40, 50, 60, 70, 80])
print(np.intersect1d(n1, n2))
print()
print(np.setdiff1d(n1, n2))
print()
print(np.setdiff1d(n2, n1))
print()
# addition in NumPy (BASIC)
import numpy as np
n1 = np.array([10, 20, 30])
n1 = n1 + 1
print(n1)
print()
import numpy as np
n1 = np.array([10, 20, 30])
n1 = n1 - 1
print(n1)
print()
import numpy as np
n1 = np.array([10, 20, 30])
n1 = n1 * 2
print(n1)
print()
import numpy as np
n1 = np.array([10, 20, 30])
n1 = n1 / 2
print(n1)
print()
# MEAN
import numpy as np
n1 = np.array([10, 20, 30, 40, 50, 60])
print(np.mean(n1))
print()
# median
import numpy as np
n1 = np.array([11, 44, 5, 96, 67, 85])
print(np.median(n1))
print()
# standard deviation
import numpy as np
n1 = np.array([1, 5, 3, 100, 4, 48])
print(np.std(n1))
print()
# NumPy Matrix
import numpy as np
n1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(n1)
print()
print(n1[0])
print()
print(n1[1])
print()
print(n1[:, 1])
print()
print(n1[:, 2])
print()
# numpy matrix transpose
n1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(n1)
print()
print(n1.transpose())
print()
# numpy matrix multiplication
n1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(n1)
print()
n2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])
print(n2)
print(n1.dot(n2))
print()
print(n2.dot(n1))
print()
# NumPy Save & Load
import numpy as np
n1 = np.array([10, 20, 30, 40, 50, 60])
np.save('my_numpy', n1)
n2 = np.load('my_numpy.npy')
print(n2)