1. Dimensions:
The dimensions of array represents the arrangement of elements in the array. The elements can be arranged horizontally called a row, and vertically called a column.
When an array contains only 1 row or 1 column of elements is called a one dimensional array or single dimensional array.
When an array contains more than 1 row and 1 column of elements is called a multi dimensional array. it can be 2Dimensional or 3D or 4D....n Dimensional.
Examples of One Dimensional Arrays:
ary1 = array([1, 2, 3, 4, 5], int)
print('The one dimensional array1 with single row:', ary1)
# Output:
The array with single row: [1 2 3 4 5]
ary2 = array([10,
20,
30,
40,
50])
print('The one dimensional array2 with single column:', ary2)
# Output:
The array with single column: [10 20 30 40 50]
Examples of Multi(say 2D or 3D..nD) Dimensional Arrays:
# the 2 dimensional array rank(2rowsx3cols)
ary3 = array([[1, 2, 3],
[4, 5, 6]])
print('The 2 dimensional array3 with 2 rows and 3 columns:', ary3)
# Output:
[4 5 6]]
# the 3 dimensional array with 2 two-dimensional arrays having rank(2x3)
ary4 = array([[[1, 2, 3], [4, 5, 6]],
[[1, 0, 1], [0, 1, 0]]])
# Output:
The 3 dimensional array4 with 2 rows and 3 columns: [[[1 2 3]
[4 5 6]]
[[1 0 1]
[0 1 0]]]
Notes:
From the above examples, we can understand that, a 2D array is a combination of several 1D arrays. Similarly a 3D is a combination of several 2D arrays.
2. Attributes:
The numpy's array class is called ndarray , known by its alias name array. Please note that there is a another 'array' class in Python which is different from the Numpy's array class.
This class contains the following important attributes(or variables).
- ndim()
- shape()
- size()
- itemsize()
- dtype()
- nbytes()
ndim():
The ndim() attribute represents the no. of dimensions or rank(no.of rows, no.of columns) of an array.
The rank of one dimensional array is 1, two dimensional array rank is 2.....
Example:
Now lets check the dimensions of the above arrays.
print('The dimensions of array1: ', ary1.ndim)
print('The dimensions of array2: ', ary2.ndim)
print('The dimensions of array3: ', ary3.ndim)
print('The dimensions of array4: ', ary4.ndim)
# Output:
The dimensions of array1: 1
The dimensions of array2: 1
The dimensions of array3: 2
The dimensions of array4: 3
shape():
The shape() attribute gives the shape of any array. The shape is tuple listing the number of elements along each dimension.
For one dimensional array, the shape gives the no. of elements in that row or column. Similarly, for 2 dimensional array, it specifies the no. of rows and columns.
Example:
Now lets check the shape of the above arrays.
print('The Shape of array1: ', ary1.shape)
print('The Shape of array2: ', ary2.shape)
print('The Shape of array3: ', ary3.shape)
print('The Shape of array4: ', ary4.shape)
# Changing the shape of the array
ary3.shape = (3, 2)
print('The array3 after re-shape: ', ary3)
# Output:
The Shape of array1: (5,)
The Shape of array2: (5,)
The Shape of array3: (2, 3)
The Shape of array4: (2, 2, 3)
The array3 after re-shape: [[1 2]
[3 4]
[5 6]]
size():
The size() attribute gives total no. of elements in that array. For multi dimensional array, the size is (no. of rows * no. of columns).
Example:
Now lets check the size of the above arrays.
print('The Size of array1: ', ary1.size)
print('The Size of array2: ', ary2.size)
print('The Size of array3: ', ary3.size)
print('The Size of array4: ', ary4.size)
# Output:
The Size of array1: 5
The Size of array2: 5
The Size of array3: 6
The Size of array4: 12
itemsize():
The itemsize() attribute gives the memory size of the array element in bytes.
Example:
ary1 = array([1, 2, 3, 4, 5], int)
ary2 = array([1.5, 4.2, 3.0, 5.4, 7.5], float)
print('The Size of array1: ', ary1.itemsize)
print('The Size of array2: ', ary2.itemsize)
# Output:
The Size of array1: 4
The Size of array2: 8
dtype():
The dtype() attribute gives the data type of the array elements.
Example:
ary1 = array([1, 2, 3, 4, 5], int)
ary2 = array([1.5, 4.2, 3.0, 5.4, 7.5], float)
print('The Size of array1: ', ary1.dtype)
print('The Size of array2: ', ary2.dtype)
# Output:
The Dataype of array1: int32
The Dataype of array2: float64
nbytes():
The nbytes() attribute gives the total number of bytes occupied by an array in the memory.
The total no. of bytes=(size of an array)*(item size of each element in the array)
Example:
ary1 = array([1, 2, 3, 4, 5], int)
ary2 = array([1.5, 4.2, 3.0, 5.4, 7.5], float)
print('The nbytes of array1: ', ary1.nbytes)
print('The nbytes of array2: ', ary2.nbytes)
# Output:
The nbytes of array1: 20
The nbytes of array2: 40
3. Methods:
The Methods of the array are useful to convert or re-shape the array from one dimensional to the multi dimensional.
- reshape()
- flatten()
reshape():
The reshape() method is useful to change the shape of the array. The re-shaped array will have the same no. of elements with a new dimensions.
Example:
ary1 = array([1, 2, 3, 4, 5,7], int)
ary2 = ary1.reshape(2, 3)
print('The original array1: ', ary1)
print('The re-shaped array1: ', ary2)
print('The of shape of array1: ', ary1.shape)
print('The re-shape of array1: ', ary2.shape)
# Output:
The original array1: [1 2 3 4 5 7]
The re-shaped array1: [[1 2 3]
[4 5 7]]
The shape of array1: (6,)
The re-shape of array1: (2, 3)
flatten():
The flatten() method is useful collapse the multi dimensional array into a one dimension.
Example:
ary1 = array([[1, 2, 3], [4, 5, 7]], int)
ary2 = ary1.flatten()
print('The original array1: ', ary1)
print('The flatten array1: ', ary2)
print('The shape of original array1: ', ary1.shape)
print('The re-shape of flatten array1: ', ary2.shape)
# Output:
The original array1: [[1 2 3]
[4 5 7]]
The flatten array1: [1 2 3 4 5 7]
The shape of original array1: (2, 3)
The new shape of flatten array1: (6,)
#--------------------------------------------------------------Thanks--------------------------------------------------------------#
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.