Welcome to Python NumPy tutorial. In our previous tutorial, we learned about Python switch case. In this tutorial we will install NumPy and look into NumPy array and some matrix operations such as addition, subtraction, multiplication etc.
Python NumPy
Python NumPy is the core library for scientific computing in Python. NumPy provides a high-performance multidimensional array object and tools for working with these arrays.
If you are already familiar with MATLAB, you might find python numpy tutorial easier to understand. To execute the following codes of this tutorial, you need to import numpy
module. This package doesn’t come with default python setup, so let’s see how you can install NumPy module.
Python install NumPy
You can look for the instruction to install NumPy from here for different types of operating systems.
I am on Mac OS X and using python 3.6, I used below command to install NumPy module for my python 3 setup.
$pip3.6 install --user numpy
Below image shows the terminal output for installing numpy module for python 3.
Python NumPy Array
Python numpy array is a grid of values, all of the same type. We can initialize Python NumPy array with nested Python List. Then we can access them using their index. Also there are some function in NumPy through which you can create different types of Array.
See the following code to understand python numpy array declaration and accessing elements.
import numpy
# Create a rank 1 array
a = numpy.array([3, 2, 3])
print('print rank 1 array:')
# access the array using their index
print('print using their index: a[0]= ', a[0])
a[0] = 5 # modify the array
print('print using slicing : ', a[1:]) # slicing can be used also
# print the whole list
print('Print the whole array : ', a)
# Create a rank 2 array using nested Python list
b = numpy.array([[10, 20, 30], [40, 50, 60]])
print('nprint rank 2 array')
# access them using their index
print('print using their index: b[0,0]= ', b[0, 0], ' b[0,1]= ',b[0, 1])
print('print using slicing ', b[1:, :2]) # 1st slice for row, 2nd for column
# initialize a zero matrix
a = numpy.zeros((2, 2))
print('nprint a 2-by-2 zero matrix:n', a)
# Create an array of all ones
b = numpy.ones((2, 2))
print('nprint a 2-by-2 all one matrix:n', b)
# Create a constant array
c = numpy.full((3, 3), 6)
print('nprint a 3-by-3 constant matrix for value = 6:n', c)
# Create a 3x3 identity matrix
d = numpy.eye(3)
print('nprint a 3-by-3 identity matrix:n', d)
The output of the python numpy array example code will be:
print rank 1 array:
print using their index: a[0]= 3
print using slicing : [2 3]
Print the whole array : [5 2 3]
print rank 2 array
print using their index: b[0,0]= 10 b[0,1]= 20
print using slicing [[40 50]]
print a 2-by-2 zero matrix:
[[ 0. 0.]
[ 0. 0.]]
print a 2-by-2 all one matrix:
[[ 1. 1.]
[ 1. 1.]]
print a 3-by-3 constant matrix for value = 6:
[[6 6 6]
[6 6 6]
[6 6 6]]
print a 3-by-3 identity matrix:
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
Python NumPy Tutorial – Arithmetic Operation on Matrix
You can do arithmetic operations such as add, subtract, multiplication and division between matrices. In the following example, you can see some examples of arithmetic operations between matrices.
import numpy
# initialize two array
x = numpy.array([[1, 2], [3, 4]], dtype=numpy.float64)
y = numpy.array([[3, 4], [5, 6]], dtype=numpy.float64)
print('Print the two matrices')
print('X = n', x)
print('Y = n', y)
# Elementwise sum; both produce the array
print('nElementwise addition of two matrices: ( X + Y of Matlab )')
print('Add using add operator: n', x + y)
print('Add using add function: n', numpy.add(x, y))
# Elementwise difference; both produce the array
print('nElementwise subtraction of two matrices: ( X - Y of Matlab )')
print('Subtract using operator: n', x - y)
print('Subtract using function: n', numpy.subtract(x, y))
# Elementwise product; both produce the array
print('nElementwise Multiplication of two matrices: ( X .* Y of Matlab )')
print('Multiply using operator: n', x * y)
print('Multiply using function: n', numpy.multiply(x, y))
# Elementwise division; both produce the array
print('nElementwise division of two matrices: ( X ./ Y of Matlab )')
print('Division using operator: n', x / y)
print('Division using function: n', numpy.divide(x, y))
# Elementwise square root; produces the array
print('nSquare root each element of X matrixn', numpy.sqrt(x))
# Matrix Multiplication
print('nMatrix Multiplication of two matrices: ( X * Y of Matlab )')
print(x.dot(y))
Below is the output produced by the above numpy matrix program.
X =
[[ 1. 2.]
[ 3. 4.]]
Y =
[[ 3. 4.]
[ 5. 6.]]
Elementwise addition of two matrices: ( X + Y of Matlab )
Add using add operator:
[[ 4. 6.]
[ 8. 10.]]
Add using add function:
[[ 4. 6.]
[ 8. 10.]]
Elementwise subtraction of two matrices: ( X - Y of Matlab )
Subtract using operator:
[[-2. -2.]
[-2. -2.]]
Subtract using function:
[[-2. -2.]
[-2. -2.]]
Elementwise Multiplication of two matrices: ( X .* Y of Matlab )
Multiply using operator:
[[ 3. 8.]
[ 15. 24.]]
Multiply using function:
[[ 3. 8.]
[ 15. 24.]]
Elementwise division of two matrices: ( X ./ Y of Matlab )
Division using operator:
[[ 0.33333333 0.5 ]
[ 0.6 0.66666667]]
Division using function:
[[ 0.33333333 0.5 ]
[ 0.6 0.66666667]]
Square root each element of X matrix
[[ 1. 1.41421356]
[ 1.73205081 2. ]]
Matrix Multiplication of two matrices: ( X * Y of Matlab )
[[ 13. 16.]
[ 29. 36.]]
So, that’s all for Python NumPy tutorial. Hope that you learnt well. For any further query, use the comment section below.
Reference: Official Reference