-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBasic_operations.py
More file actions
434 lines (304 loc) · 9.34 KB
/
Basic_operations.py
File metadata and controls
434 lines (304 loc) · 9.34 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import numpy as np
# Original array
array = np.array([1, 2, 3])
# Append a new element
array = np.append(array, 4)
print("Updated Array:", array) # Output: [1 2 3 4]
# -------------------------------------
# Original array
array = np.array([1, 2, 3])
# New elements to add
new_elements = np.array([4, 5])
# Concatenate arrays
array = np.concatenate((array, new_elements))
print("Updated Array:", array) # Output: [1 2 3 4 5]
# -------------------------------------
# Original array
array = np.array([1, 2, 3])
# Insert a new element at index 1
array = np.insert(array, 1, 4)
print("Updated Array:", array) # Output: [1 4 2 3]
# ======================================================================
#Dimensions
# zero dimension
n=np.array(34)
print(n)
print("Dimension=",n.ndim)
# One dimension
n=np.array([1,2,3])
print(n)
print("Dimension=",n.ndim)
# Two dimension
n=np.array([[1,2,3],[4,5,6]])
print(n)
print("Dimension=",n.ndim)
# Three dimension
n=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[1,2,3],[4,5,6]]])
print(n)
print("Dimension=",n.ndim)
# -------------------
# Dimenstion expanding and squeezing
# Original 1D array
array_1d = np.array([1, 2, 3, 4, 5])
print("Original 1D array:")
print(array_1d)
print("Shape:", array_1d.shape)
# Output:
# Original 1D array:
# [1 2 3 4 5]
# Shape: (5,)
# Add a new axis to create a 2D array (axis=0)
expanded_array = np.expand_dims(array_1d, axis=0)
print("\nExpanded 2D array (new axis at position 0):")
print(expanded_array)
print("Shape:", expanded_array.shape)
# Output:
# Expanded 2D array (new axis at position 0):
# [[1 2 3 4 5]]
# Shape: (1, 5)
# Add a new axis to create a 2D array along the other dimension (axis=1)
expanded_array = np.expand_dims(array_1d, axis=1)
print("\nExpanded 2D array (new axis at position 1):")
print(expanded_array)
print("Shape:", expanded_array.shape)
# Output:
# Expanded 2D array (new axis at position 1):
# [[1]
# [2]
# [3]
# [4]
# [5]]
# Shape: (5, 1)
# Create a 3D array with one dimension of size 1
array_3d = np.array([[[1, 2, 3], [4, 5, 6]]])
print("\nOriginal 3D array:")
print(array_3d)
print("Shape:", array_3d.shape)
# Output:
# Original 3D array:
# [[[1 2 3]
# [4 5 6]]]
# Shape: (1, 2, 3)
# Remove single-dimensional entries
squeezed_array = np.squeeze(array_3d)
print("\nSqueezed array:")
print(squeezed_array)
print("Shape:", squeezed_array.shape)
# Output:
# Squeezed array:
# [[1 2 3]
# [4 5 6]]
# Shape: (2, 3)
# expand_dims(): Use it to add new dimensions to an array.
# squeeze(): Use it to remove dimensions of size 1 from an array.
# ====================================================================================
# Basic Arithematics
n1=np.array([1,2,70,7])
n2=np.array([70,4,5,7])
# sum: will sum two arrays
summed=np.sum([n1,n2])
print(summed)
# row sum
summed=np.sum([n1,n2],axis=0)
print(summed)
# column sum
summed=np.sum([n1,n2],axis=1)
print(summed)
# subtract
subtracted=np.subtract(n1,n2)
print(subtracted)
# multiplication
multiplicated=np.multiply(n1,n2)
print(multiplicated)
# divide
divided=np.divide(n1,n2)
print(divided)
# ==========================================================================
# Data Types
# int data type
n=np.array([1,2,3])
print(n.dtype)
# String data type( unicode string )
n=np.array(["jubair","jaseela","jabit"])
print(n.dtype)
# String data type(explicitly)
n=np.array(["hello","hi","fine","where"],dtype='S9')
print(n.dtype)
print(n)
# Convert data type
n=np.array(["12","34","65","78"])
n2=n.astype('i')
print(n.dtype)
print(n2.dtype)
# =================================================================
# Indexing and Slicing
n1=np.array([[[1,2,3],[4,5,6]],
[[43,54,65],[87,65,56]]
])
print(n1[0,1,1])
print(n1[0,1])
# Create a 2D array
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Slicing rows and columns
print("Rows 1 and 2, columns 1 and 2:")
print(array_2d[1:3, 1:3]) # Output: [[5 6]
# [8 9]]
# Create an array
array = np.array([10, 20, 30, 40, 50])
# Boolean indexing
condition = array > 30
print("Elements greater than 30:", array[condition]) # Output: [40 50]
# -------------------------
# 1. Boolean Indexing
# Boolean indexing allows you to select elements of an array using a boolean array of the same shape. Wherever the boolean array has True, the corresponding element from the original array is selected.
import numpy as np
# Create a 1D NumPy array
arr = np.array([10, 20, 30, 40, 50])
# Create a boolean condition (select elements greater than 30)
condition = arr > 30
# Use boolean indexing
result = arr[condition]
print("Boolean Indexing Result:", result)
# Output: [40 50]
# Here, the condition arr > 30 creates a boolean array [False, False, False, True, True]. When applied to arr, only the elements at the True positions are selected.
# ---------------------------------------------------------------------------------------------------------
# 2. Fancy Indexing
# Fancy indexing allows you to use arrays of indices to access specific elements. You can select elements by their index positions in a flexible and powerful way.
# 1D Fancy Indexing Example:
import numpy as np
# Create a 1D NumPy array
arr = np.array([10, 20, 30, 40, 50])
# Use fancy indexing to select elements at specific positions
indices = [0, 2, 4]
result = arr[indices]
print("Fancy Indexing Result:", result)
# Output: [10 30 50]
# 2D Fancy Indexing Example:
import numpy as np
# Create a 2D NumPy array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Use fancy indexing to select specific rows and columns
rows = [0, 2]
cols = [1, 2]
# arr[0, 1] = 2 (first row, second column)
# arr[2, 2] = 9 (third row, third column)
result = arr[rows, cols]
print("Fancy Indexing on 2D Array:", result)
# Output: [2 9]
# Here, rows = [0, 2] and cols = [1, 2] selects the elements at positions (0,1) and (2,2).
# ----------------------------------------------------------------------------------------------------------
# 3. Advanced Slicing
# 2D Slicing Example:
import numpy as np
# Create a 2D NumPy array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]])
# Slice a submatrix (rows 1 to 3, columns 0 to 2)
result = arr[1:3, 0:2]
print("Advanced Slicing on 2D Array:")
print(result)
# Output:
# [[4 5]
# [7 8]]
# Here, we sliced rows from index 1 to 3 (excluding index 3) and columns from index 0 to 2 (excluding index 2), extracting a submatrix.
# ----------------------------------------------------------------------------------------------------------
# Combining Indexing and Slicing
import numpy as np
# Create a 2D NumPy array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Boolean condition
condition = arr > 4
# Fancy indexing with slicing
rows = [1, 2]
cols = slice(1, 3)
# Apply boolean indexing followed by fancy slicing
result = arr[rows, cols][condition[rows, cols]]
print("Combined Indexing and Slicing Result:", result)
# Output: [5 6 8 9]
# Here, we first used boolean indexing to filter elements greater than 4, then applied fancy indexing and slicing to extract specific rows and columns.
# --------------------------------------------------------------------------------------------------------
# Modifying Arrays with Boolean Indexing
import numpy as np
# Create a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Modify elements greater than 5
arr[arr > 5] = 0
print("Modified 2D Array (Boolean Indexing):")
print(arr)
# Output:
# [[1 2 3]
# [4 5 0]
# [0 0 0]]
# ------------------
# 2. Modifying Arrays with Fancy Indexing
import numpy as np
# Create a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Modify elements at specific row and column positions
arr[[0, 2], [1, 2]] = [99, 88]
print("Modified 2D Array (Fancy Indexing):")
print(arr)
# Output:
# [[ 1 99 3]
# [ 4 5 6]
# [ 7 8 88]]
# ---------------------------
# 3. Modifying Arrays with Advanced Slicing
import numpy as np
# Create a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Modify a slice of the array (submatrix)
arr[1:3, 1:3] = [[99, 88], [77, 66]]
print("Modified 2D Array (Slicing):")
print(arr)
# Output:
# [[ 1 2 3]
# [ 4 99 88]
# [ 7 77 66]]
# ------------------
# Combining Boolean Indexing and Slicing:
import numpy as np
# Create a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Create a condition to select elements in the first two rows greater than 2
arr[:2, :][arr[:2, :] > 2] = 99
print("Modified Array (Boolean Indexing and Slicing):")
print(arr)
# Output:
# [[ 1 2 99]
# [99 99 99]
# [ 7 8 9]]
# ================================================================================
# Array Initialization
# initialze ex1
n=np.zeros([2,3])# 2 rows and 3 columns
print(n)
print(type(n))
# initialze ex2
n=np.zeros([4,4])# 4 rows and 4 columns
print(n)
print(type(n))
# It will give one , instead of zero
n1=np.ones([2,3])
print(n1)
# This is like range() function, but it will give output of array instead of object
n1=np.arange(1,11).reshape(5,2)
print(n1)
n2=np.arange(10)
print(n2)
# -------------------------------------
# -------------------------------------