Num Py
Num Py
Example:
# creating list
list = [1, 2, 3, 4]
print(type(sample_array))
<class 'list'>
<class 'numpy.ndarray'>
Multi-Dimensional Array:
Data in multidimensional arrays are stored in tabular form.
# creating list
list_1 = [1, 2, 3, 4]
list_2 = [5, 6, 7, 8]
list_3 = [9, 10, 11, 12]
Output
Numpy multi dimensional array in python
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
import numpy as np
[1 2 3 4 5]
<class 'numpy.ndarray'>
Example
[1 2 3 4 5]
Dimensions in Arrays
A dimension in arrays is one level of array depth (nested
arrays).
3
nested array: are arrays that have arrays as their elements.
0-D Arrays
0-D arrays, or Scalars, are the elements in an array.
Each value in an array is a 0-D array.
Example
Create a 0-D array with value 42.
import numpy as np
arr = np.array(42)
print(arr)
1-D Arrays
An array that has 0-D arrays as its elements is called uni-
dimensional or 1-D array.
Example
Create a 1-D array containing the values 1,2,3,4,5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
[1 2 3 4 5]
4
2-D Arrays
An array that has 1-D arrays as its elements is called a
2-D array.
Example
3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D
array.
Example
Create a 3-D array with two 2-D arrays, both containing two
arrays with the values 1,2,3 and 4,5,6:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]],
5
[[1, 2, 3], [4, 5, 6]]])
print(arr)
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
Example
Check how many dimensions the arrays have:
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3],
[4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
0
1
2
3
6
Higher Dimensional Arrays
An array can have any number of dimensions.
Example
Create an array with 5 dimensions and verify that it has 5
dimensions:
import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
print('number of dimensions :', arr.ndim)
[[[[[1 2 3 4]]]]]
number of dimensions : 5
Example
Get the first element from the following array:
7
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[0])
1
Example
Get the second element from the following array.
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[1])
Example
Get third and fourth elements from the following array and
add them.
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])
8
Example
Access the element on the first row, second column:
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st row: ', arr[0, 1])
2nd element on 1st dim: 2
Example
Example
Access the third element of the second array of the first array:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9],
[10, 11, 12]]])
print(arr[0, 1, 2])
9
Example Explained
Negative Indexing
Use negative indexing to access an array from the end.
10
Example
Print the last element from the 2nd dim:
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('Last element from 2nd dim: ', arr[1, -1])
Last element from 2nd dim: 10
Example
Slice elements from index 1 to index 5 from the following array:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
[2 3 4 5]
11
Example
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[4:])
[5 6 7]
Example
Slice elements from the beginning to index 4 (not included):
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[:4])
[1 2 3 4]
Negative Slicing
Use the minus operator to refer to an index from the end:
Example
Slice from the index 3 from the end to index 1 from the end:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[-3:-1])
[5 6]
STEP
Use the step value to determine the step of the slicing:
12
Example
Return every other element from index 1 to index 5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5:2])
[2 4]
Example
Return every other element from the entire array:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[::2])
[1 3 5 7]
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 1:4])
[7 8 9]
Example
From both elements, return index 2:
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
13
print(arr[0:2, 2])
[3 8]
Example
From both elements, slice index 1 to index 4 (not included),
this will return a 2-D array:
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 1:4])
[[2 3 4]
[7 8 9]]
# importing numpy as np
import numpy as np
14
arr = np.array([[2, 4, 6]])
gfg = np.swapaxes(arr, 0, 1)
print (gfg)
Output :
[[2]
[4]
[6]]
Code #2 :
# Python program explaining
# numpy.swapaxes() function
# importing numpy as np
import numpy as np
arr = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])
gfg = np.swapaxes(arr, 0, 2)
print (gfg)
Output :
[[[0 4]
[2 6]]
[[1 5]
[3 7]]]
Parameters:
axes : [None, tuple of ints, or n ints] If anyone wants to pass the
parameter then you can but it’s not all required. But if you want than
remember only pass (0, 1) or (1, 0). Like we have array of shape (2, 3) to
change it (3, 2) you should pass (1, 0) where 1 as 3 and 0 as 2.
Returns: ndarray
15
Example #1 :
In this example we can see that it’s really easy to transpose an array with
just one line.
# before transpose
print(gfg, end ='\n\n')
# after transpose
print(gfg.transpose())
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 4 7]
[2 5 8]
[3 6 9]]
16
Example #2 :
In this example we demonstrate the use of tuples in numpy.transpose().
# before transpose
print(gfg, end ='\n\n')
# after transpose
print(gfg.transpose(1, 0))
Output:
[[1 2]
[4 5]
[7 8]]
[[1 4 7]
[2 5 8]]
# after transpose
print(gfg.T)
Output
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 4 7]
[2 5 8]
[3 6 9]]
18