Numpy (part-1)
What is Numpy?
- Numpy is a python library helps in mathematical, scientific, engineering, and data science programming.
- It is a very useful library to perform mathematical and statistical operations in Python.
- NumPy is a programming language that deals with multi-dimensional arrays and matrices.
- Numpy is Numeric python or Numerical python.
Why to use Numpy?
- NumPy is memory efficiency and it can handle the vast amount of data.
- Numpy is fast as it uses algorithm that are written in C language.
- Clear code without loops.
How to install numpy?
- To install numpy use command:
pip install numpy
- To install numpy on poetry environment
poetry add numpy
How to import numpy and check version?
- To import and check version use:
import numpy as np
print(np.__version__)
It will print version of numpy: for me it’s 1.23.5.
What is numpy array?
- NumPy arrays are a bit like Python lists, but still very much different at the same time.
- And it’s a central data structure of the numpy library.
How to create numpy array from python list?
To create numpy array use np.array function:
list_ = [10, 20, 30, 40]
numpy_list = np.array(list_)
print(f'Type of variable: {type(numpy_list)}')
print(f'Numpy Array: {numpy_list}')
#output:
# Type of variable: <class 'numpy.ndarray'>
# Numpy Array: [10 20 30 40]
Basic operation on numpy:
To add constant value to full numpy array use:
numpy_list = np.array([10, 20, 30, 40])
print(f'Array before adding constant value: {numpy_list}')
constant_add_array = numpy_list + 10
print(f'New array after adding constant: {constant_add_array}')
#Output:
# Array before adding constant value: [10 20 30 40]
# New array after adding constant: [20 30 40 50]
Add two numpy array of same size:
- To add two numpy array use:
numpy_array_a = np.array([10, 20, 30, 40])
numpy_array_b = np.array([10, 20, 30, 40]) + 10
numpy_array_c = numpy_array_a + numpy_array_b
print(f'Numpy array a: {numpy_array_a}')
print(f'Numpy array b: {numpy_array_b}')
print(f'Numpy array c after sum of a and b array: {numpy_array_c}')
# Output:
# Numpy array a: [10 20 30 40]
# Numpy array b: [20 30 40 50]
# Numpy array c after sum of a and b array: [30 50 70 90]
How to get shape of array and type of array?
To get array shape use shape function of numpy:
numpy_array_a = np.array([x for x in range(10, 60, 10)])
print(f'Numpy array a: {numpy_array_a}')
print(f'Numpy array shape: {numpy_array_a.shape}')
print(f'Variable a type {type(numpy_array_a)}')
# Output:
# Numpy array a: [10 20 30 40 50]
# Numpy array shape: (5,)
# Variable a type <class 'numpy.ndarray'>
How to create 2 Dimensional array and 3 dimensional array?
To create 2-d and 3-d array:
array_2_dim = np.array(
[
[10, 20, 30],
[40, 50, 60]
])
print(f'Two dimensional array shape: {np.shape(array_2_dim)}')
array_3_dim = np.array([[[10, 20, 30], [40, 50, 60]], [[10, 20, 30], [40, 50, 60]],])
print(f'Three dimensional array: {np.shape(array_3_dim)}')
# Output:
# Two dimensional array shape: (2, 3). Three dimensional array: (2, 2, 3)
How to create a matrix with all values as 0 in numpy?
- To create 2D zero value array use
np.zeros(shape=(3,3))
- To create 3D zero value array use
np.zeros(shape=(2,2, 2))
How to create matrix with all values as 1 in numpy?
- To create 2D one value array use
np.ones(shape=(3,3))
- To create 3D one value array use
np.ones(shape=(2,2, 2))
- By default, it creates float type matrix but we can define type using
np.zeros(shape=(2,2), dtype=np.int64)
Youtube link.