Multi Dimensional Arrays:
The arrays with more than one dimension like 2D, 3D,..nD are called multi Dimensional arrays.
A 2D array contains more than one row and one column, and it can be treated as combination of several 1D arrays.
A 2D array also considered as a Matrix. A 2D Array with m rows and n columns is called m x n Matrix.
In Mathematics, a Matric is a collection of elements arranged in several rows and columns.
We can create the multi dimensional arrays using the below functions.
- array()
- ones() , zeros()
- eye()
- reshape()
# Importing arrays from 'numpy'
from numpy import *
array():
We can use the array() function to create the multi dimensional arrays as well. When we pass the one list of elements to this function, it will create a 1D array. Similarly if we pass the two list of elements, then it will create 2D array.
Example:
ary1 = array([1, 2, 3, 5])
ary2 = array([[1, 2, 3, 5], [7, 9, 11, 13]])
print('The one dimensional array is:', ary1)
print('The two dimensional array is:', ary2)
# Output:
The one dimensional array is: [1 2 3 5]
The two dimensional array is: [[ 1 2 3 5]
[ 7 9 11 13]]
reshape():
The reshape() function is useful to convert the 1D array into multi dimensional(2D, 3D..) array.
Example:
ary1 = array([1, 2, 3, 5, 7, 9])
ary2 = reshape(ary1, [3, 2])
ary3 = reshape(ary1, [2, 3])
print('The original 1D array is:', ary1)
print('The re-shaped 2D(3x2) array is:', ary2)
print('The re-shaped 2D(2x3) array is:', ary3)
# Output:
The original 1D array is: [1 2 3 5 7 9]
The re-shaped 2D(3x2) array is: [[1 2]
[3 5]
[7 9]]
The re-shaped 2D(2x3) array is: [[1 2 3]
[5 7 9]]
# converting 1D array into 3D Array with two 2D arrays of rank(3x2)
a = arange(12)
b = reshape(a, (2, 3, 2))
print('The original array:', a)
print('The reshaped 3D array:', b)
# Output:
The original array: [ 0 1 2 3 4 5 6 7 8 9 10 11]
The reshaped 3D array: [[[ 0 1]
[ 2 3]
[ 4 5]]
[[ 6 7]
[ 8 9]
[10 11]]]
ones():
The ones() function is useful to create a 2D array with several rows and columns where all the elements will be taken as 1.
The general format of the function is: arrayname= ones((r, c), dtype)
Here, r represents the no. of rows, c represents the no. of columns, and the 'dtype' represents the data type of the elements in array.
If 'dtype' is omitted, then it will take the 'float' as default datatype.
Example:
a = ones((3, 4), float) # 2D array with rank 3x4
print('The array with ones:', a)
# Output:
The array with ones: [[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
zeros():
Like the ones() function, the zeros() function is useful to create a 2D array with several rows and columns where all the elements will be taken as 0.
Example:
b = zeros((3, 4), int)print('The array with zeros:', b) # 2D array with rank 3x4
# Output:
The array with zeros: [[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
eye():
The eye() function is useful to create a 2D array with the n rows and n columns where all the diagonal elements will be 1. The default datatype is float.
The general format of the function is: arrayname= eye(n, dtype=datatype)
Example:
a = eye(3)print('The array with eye:', a) # 2D array with rank 3x3
# Output:
The array with eye: [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Indexing Multi Dimensional Arrays:
Index represents the location number of the element of an array. The individual elements of a 2D array can be accessed by specifying the location number of row and column of element.
For example, a[0][0] represents 0th row and 0th column of array a. Similarly b[1][3] represents the 1st row and 3rd column of the element.
Examples:
# A Python program to retrieve and display the elements from a 2D Array using For Loop.
a = [[1, 2, 3],[5, 7, 9],[4, 6, 8]] # 2D array with rank 2x3
# display only rows
j = 0
print('The 2D array is:')
for i in range(len(a)):
print(a[i])
# display the elements
print('The all elements in 2D array are:')
for i in range(len(a)):
for j in range(len(a[j])):
print(a[i][j], end=' ')
# Output:
The 2D array is:
[1, 2, 3]
[5, 7, 9]
[4, 6, 8]
The all elements in 2D array are:
1 2 3 5 7 9 4 6 8
# A Python program to retrieve and display the elements from a 3D Array using For Loop.
# 3D array with two 2D arrays of rank 2x3
a = [[[1, 2, 3],
[5, 7, 9]],
[[4, 6, 8],
[10, 13, 15]]]
# display only rows
print('\nThe 3D array is:')
for i in range(len(a)):
print(a[i])
# display the elements
print('The all elements in 3D array are:')
j = 0
k = 0
for i in range(len(a)):
for j in range(len(a[j])):
for k in range(len(a[i][j])):
print(a[i][j][k], end='\t')
print()
# Output:
The 3D array is:
[[1, 2, 3], [5, 7, 9]]
[[4, 6, 8], [10, 13, 15]]
The all elements in 3D array are:
1 2 3
5 7 9
4 6 8
10 13 15
Slicing Multi Dimensional Arrays:
The slicing represents the retrieval of a part or piece of the array. Now will look into the slicing of multi dimensional arrays.
Example 1:
a = [[1, 2, 3], [5, 7, 9], [4, 6, 8]]
# reshaping to a 2D array with rank(3x3)
a = reshape(a, (3, 3))
# to display((below 3 statements are same) all the elements of 2D array
print('The 2D array elements are:\n', a[:, :])
# print('The 2D array elements are:\n', a[::])
# print('The 2D array elements are:\n', a[:])
Output:
The 2D array elements are:
[[1 2 3]
[5 7 9]
[4 6 8]]
print('The 0th row of array is:\n', a[0,:])
print('The 0th column of array is:\n', a[:, 0])
Output:
The 0th row of array is:
[1 2 3]
The 0th column of array is:
[1 5 4]
print('The 1st row of array is:\n', a[1, :])
print('The 1st column of array is:\n', a[:, 1])
Output:
The 1st row of array is:
[5 7 9]
The 1st column of array is:
[2 7 6]
print('The element of 0th row and 0th col of array is:\n', a[0:0, 0:0])
print('The element of 0th row and 1st col of array is:\n', a[0:1, 0:1])
print('The element of 1st row and 2nd col of array is:\n', a[1:2, 1:2])
print('The element of 2nd row and 2nd col of array is:\n', a[2:3, 1:2])
Output:
The element of 0th row and 0th col of array is:
[]
The element of 0th row and 1st col of array is:
[[1]]
The element of 1st row and 2nd col of array is:
[[7]]
The element of 2nd row and 2nd col of array is:
[[6]]
print('The elements from first 2rows till 2nd col of array are:\n', a[0:2, 0:2])
print('The elements from first 2rows till 3rd col of array are:\n', a[1:3, 1:3])
Output:
The elements from first 2rows till 2nd col of array are:
[[1 2]
[5 7]]
The elements from first 2rows till 3rd col of array are:
[[7 9]
[6 8]]
Example 2:
a = [[1, 2, 3], [5, 7, 9], [4, 6, 8]]
a = reshape(arange(11, 36, 1), (5, 5))
print('The 2D array with rank 5x5 is:\n', a)
b = a[0:2, 0:3]
print('The elements from top 2 rows and 3 cols is:\n', b)
b = a[0:2, 2:]
print('The elements from right top 2 rows and 3 cols is:\n', b)
b = a[2:, 3:]
print('The elements from lower right 2 rows and 3 cols is:\n', b)
Output:
The 2D array with rank 5x5 is:
[[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]]
The elements from top 2 rows and 3 cols is:
[[11 12 13]
[16 17 18]]
The elements from right top 2 rows and 3 cols is:
[[13 14 15]
[18 19 20]]
The elements from lower right 2 rows and 3 cols is:
[[24 25]
[29 30]
[34 35]]
Process finished with exit code 0
#--------------------------------------------------------------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.