Professional Documents
Culture Documents
NumPy Introduction
What is NumPy?
NumPy is a Python library used for working with arrays.
It also has functions for working in domain of linear algebra, fourier transform,
and matrices.
NumPy was created in 2005 by Travis Oliphant. It is an open source project and
you can use it freely,
Arrays are very frequently used in data science, where speed and resources are
very important.
1
Unit-5::: DR.UMA MEENA 2022
This is the main reason why NumPy is faster than lists. Also it is optimized to
work with latest CPU architectures.
2
Unit 5:: DR.UMA MEENA |2022
Installation of NumPy
installation of
If you have Python and PIP already installed on a system, then
NumPy is very easy.
Import NumPy
NumPy is installed, import it in your applications by adding
Once
the import keyword:
import numpy
4, 5])
arr numpy.array ([1, 2, 3,
print(arr)
OUTPUT:
3
Unit 5:::: DR.UMA MEENA | 2022
(1 2345]
NumPy as np
alias.
NumPy is usually imported under the np
import numpy as np
Example
import
arr
print(arr)
numpy as np
np.array(([1, 2, 3, 4,
Uma 5])
OUTPUT:
[1 2 34 5]
attribute.
June 22,
stored underversion
The version string is 2012
Example 23 Sreleaenole)
import numpy as np
print(np.version_)
J
1 3 t h NOv, 2 . 2
OUTPUT:
4
Unit 5:: DR.UMA MEENA |2022
1.16.3
Example
import numpy as np
OUTPUT:
[1 2 3 4 5]
<class 'numpy.ndarray'>
type(): This built-in Python function tells us the type of the object passed to it.
Like in above code it shows that arr IS numpy.ndarray typpe.
To create an ndarray, we can pass a list, tuple or any array-like object into
the array () method, and it will be conveted into an ndarray:
s
Unit 5:: DR.UMA MEENA 2022
Example
Use a tuple to create a NumPy array:
import numpy as np
print(arr)
[1 2 3 4 5]
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)
42
6
Unit 5::: DR.UMA MEENA 2022
1-D Arrays
its elements is called uni-dimensional or 1-D
An array that has 0-D arrays as
array.
Example
Create a 1-D array containing the values 1,2,3,4,5:
import numpy as np
[1 2 3 4 5]
2-D Arrays V
An array that has 1-D arrays as its elements is called a 2-D array.
Example
Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np
print (arr )
C[1 2 3]
7
Unit 5:: DR.UMA MEENA 2022
[4 5 6]]
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]1, [[1, 2, 3], [4, 5, 6]]])
print(arr)
CC1 2 3] m a
[4 5 6]]
CU1 2 3]
[45 6]1]
Example
have:
Check how many dimensions the arrays
8
Unit -5: DR.UMA MEENA 2022
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)
Uma
print(d.ndim)
Example
the following array:
Get the first element from
import numpy as np
np.array([1, 2, 3, 4])
arr
print(arr[0])
OUTPUT:
9
Unit-5::: DR.UMA MEENA 2022
Example
Get the second element from the following array.
import numpy as np
print(arr[1])
OUTPUT
Example
Oma
Get third and fourth elements from the following array and add them.
import numpy as np
print(arr[2] + arr[3]) Um
OUTPUT:
Example
Access the element on the first row, second column:
10
Unit 5: DR.UMA MEENA 2022
import numpy as np
Example
Access the element on the
2nd row, 5th column:
import numpy as np
Example
of the first array:
element of the second array
Access the third
import numpy as np
11
Unit-5:: DR.UMA MEENA 2022
and:
C[7, 8, 9], [10, 11, 12]]
Since we selected e, we are left with the first array:
[1, 2, 3], [4, 5, 6]]
tw0
which also contains
The second number represents the second dimension,
arrays
[1, 2, 3]
and:
4, 5, 6]
Since we selected 1, we are left with the second array:
[4, 5, 6]
contains three values:
.
Example
from the 2nd dim:
Print the last element
import numpy as np
o 2 34
[6,7,8,9,10]])
arr np. array ([ [1,2,3,4,5],
"Last element from
2nd dim: ', arr[1, -1])
print(
10
Last element from 2nd dim:
12
Unit - 5:: DR.UMA MEENA 2022
Slicing arrays
from one given index to another given
taking elements
slicing in python means
index.
considered 0
If we don't pass start its
considered 1
If we don't pass step its
Example
index 1 to index 5 from the following array:
Slice elements from
import numpy as np
arr np.array(l11, 2, 3, 4 % 2
print(arr[1:5])
OUTPUT:
[2 3 4 5]
13
Unit 5::: DR.UMA MEENA |2022
Example
end of the array:
Slice elements from index 4 to the
import numpy as np
print(arr[4:])
OUTPUT:
[5 6 7]
Example
to index 4 (not included):
from the beginning
Slice elements
import numpy
as np
Uma
np.array([1, 2, 3, 4, 5, 6, 7])
arr
print(arr[ :4])
Negative Slicing
refer to an index from the end:
operator to
Use the minus
Example
end:
index 1 from the
index 3 from the end to
Slice from the
import numpy as np
7])
[1, 2, 3, 4, 5, 6,
arr np. array(
print(arr[-3:-1])
14
Unit- 5::: DR.UMA MEENA 2022
OUTPUT:
S6
STEP
Use the step value to determine the step of the slicing: Ume
Example
Return every other element from index 1 to index 5:
import numpy as np
print(arr[1:5:2]1)
OUTPUT
[2 4]
15