Skip to content

Commit 93c895a

Browse files
committed
Numpy Array Manipulation operations
1 parent 785a0b0 commit 93c895a

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

Numpy/P05_NumpyArrayManipulation.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Author: OMKAR PATHAK
2+
3+
# This example shows various array manipulation operations
4+
import numpy as np
5+
6+
# numpy.reshape(array_to_reshape, tuple_of_new_shape) gives new shape (dimension) to our array
7+
myArray = np.arange(0, 30, 2)
8+
print(myArray) # [ 0  2  4  6  8 10 12 14 16 18 20 22 24 26 28]
9+
10+
myArrayReshaped = myArray.reshape(5, 3)
11+
print(myArrayReshaped)
12+
13+
# [[ 0  2  4]
14+
#  [ 6  8 10]
15+
#  [12 14 16]
16+
#  [18 20 22]
17+
#  [24 26 28]]
18+
19+
# numpy.ndarray.flat() returns an 1-D iterator
20+
print(myArray.flat[5]) # 10
21+
22+
# numpy.ndarray.flatten() restores the reshaped array into a 1-D array
23+
print(myArrayReshaped.flatten())
24+
25+
# numpy.tranpose() this helps to find the tranpose of the given array
26+
print(myArrayReshaped.transpose())
27+
28+
# [[ 0  6 12 18 24]
29+
#  [ 2  8 14 20 26]
30+
#  [ 4 10 16 22 28]]
31+
32+
# numpy.swapaxes(array, axis1, axis2) interchanges the two axes of an array
33+
originalArray = np.arange(8).reshape(2,2,2)
34+
print(originalArray)
35+
36+
# [[[0 1]
37+
#   [2 3]]
38+
#  
39+
#  [[4 5]
40+
#   [6 7]]]
41+
42+
print(np.swapaxes(originalArray, 2, 0))
43+
44+
# [[[0 4]
45+
#   [2 6]]
46+
#  
47+
#  [[1 5]
48+
#   [3 7]]]
49+
50+
# numpy.rollaxis(arr, axis, start) rolls the specified axis backwards, until it lies in a specified position
51+
print(np.rollaxis(originalArray, 2))
52+
53+
# [[[0 2]
54+
#   [4 6]]
55+
#  
56+
#  [[1 3]
57+
#   [5 7]]]
58+
59+
# numpy.resize(arr, shape) returns a new array with the specified size. If the new size is greater than
60+
# the original, the repeated copies of entries in the original are contained
61+
62+
myArray = np.array([[1,2,3],[4,5,6]])
63+
print(myArray)
64+
65+
# [[1 2 3]
66+
#  [4 5 6]]
67+
68+
print(np.resize(myArray, (3, 2)))
69+
70+
# [[1 2]
71+
#  [3 4]
72+
#  [5 6]]
73+
74+
# numpy.append(array, values, axis)
75+
myArray = np.array([[1,2,3],[4,5,6]])
76+
print(myArray)
77+
78+
# [[1 2 3]
79+
#  [4 5 6]]
80+
81+
print(np.append(myArray, [7, 8, 9]))
82+
83+
# [1 2 3 4 5 6 7 8 9]

0 commit comments

Comments
 (0)