NumPy is a python library used for working with arrays. It stands for Numerical Python. Numpy provides many functions to work on linear algebra, arrays, and matrices making it a widely used tool in data analysis.
The main advantage is that it is incredibly fast as it has bindings to C-libraries.
This is the first of a 3-part series of blog posts on Numpy Library. The topics included respectively are – Initialization of a Numpy Array, Numpy Operations, and Indexing, Numpy Functions.
arange()
zeros()
ones()
linspace()
eye()
empty()
np.random.rand()
np.random.randn()
np.random.randint()
For using the Numpy functions, first, you need to download the numpy
library.
If you are using anaconda, then you can download it using conda install numpy
command in the anaconda prompt.
If not, then pip install numpy
can be used in the command prompt.
Then after installing, to use it in your program, you have to import the numpy library
.
pip install numpy
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (1.18.5)
import numpy as np
Now we are all set and ready to work with the library. First, let’s start with numpy arrays
.
A NumPy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers.
If the arrays are 1-dimensional, then they are referred to as vectors. If they are 2-dimensional, then they are referred to as matrices.
np.array()
the function is used to create the array. Pass a list of values as arguments to the function.
arr = np.array([1,2,3,4,5])
arr
array([1, 2, 3, 4, 5])
type(arr)
numpy.ndarray
See that the array type comes out to be numpy.ndarray
which refers to numpy n-dimensional array
.
The above one is a 1-dimensional array. The same can be used for creating 2d/3d arrays too.
arr = np.array([[1,2,3,4,5],[6,7,8,9,10]])
arr
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
arange()
arange()
a function in NumPy is similar to the range()
function in python. It creates an array of evenly spaced values. You need to pass the initial value, final value, and step size as arguments to the function.
arr = np.arange(1,10,1)
arr
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
See that the initial value is 1 and it is increasing at a step size of 1. Please note that the final value is not included. If you want to count numbers from 0 to 10, then you need to use np.arange(0,11,1)
.
arr = np.arange(1,10,0.2)
arr
array([1\. , 1.2, 1.4, 1.6, 1.8, 2\. , 2.2, 2.4, 2.6, 2.8, 3\. , 3.2, 3.4,
3.6, 3.8, 4\. , 4.2, 4.4, 4.6, 4.8, 5\. , 5.2, 5.4, 5.6, 5.8, 6\. ,
6.2, 6.4, 6.6, 6.8, 7\. , 7.2, 7.4, 7.6, 7.8, 8\. , 8.2, 8.4, 8.6,
8.8, 9\. , 9.2, 9.4, 9.6, 9.8])
zeros()
np.zeros()
the function is used to create an array with the specified dimension that is passed as an argument filled with the values as zero(0)
.
np.zeros(5)
will create a 1d array with 5 elements with values as zero.
np.zeros(5)
array([0., 0., 0., 0., 0.])
For creating a 2d-array, you need to pass the shape of the array. Like pass, the argument as (5,5)
, to get a 5×5 array filled with zeros.
np.zeros((5,5))
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
Shape here represents the dimensions of the array. If the array has a shape of (2,5)
, then it means that the array has 2 rows and 5 columns.
To get the shape of an array, use the arr.shape
command.
arr = np.array([[1,2,3],[4,5,6]])
arr.shape
(2, 3)
ones()
Similar to zeros()
if you want an array filled with values as 1, then use the ones()
function.
arr = np.ones((2,3))
arr
array([[1., 1., 1.],
[1., 1., 1.]])
linspace()
In arange()
function, you will be specifying the start, end, and the step size of the array you want. But in linspace()
function, you will be providing the start, end, and the number of values you want between them.
If you use np.linspace(1,10,50)
, then this will output 50 evenly spaced points between 1 and 10.
arr = np.linspace(1,10,50)
arr
array([ 1\. , 1.18367347, 1.36734694, 1.55102041, 1.73469388,
1.91836735, 2.10204082, 2.28571429, 2.46938776, 2.65306122,
2.83673469, 3.02040816, 3.20408163, 3.3877551 , 3.57142857,
3.75510204, 3.93877551, 4.12244898, 4.30612245, 4.48979592,
4.67346939, 4.85714286, 5.04081633, 5.2244898 , 5.40816327,
5.59183673, 5.7755102 , 5.95918367, 6.14285714, 6.32653061,
6.51020408, 6.69387755, 6.87755102, 7.06122449, 7.24489796,
7.42857143, 7.6122449 , 7.79591837, 7.97959184, 8.16326531,
8.34693878, 8.53061224, 8.71428571, 8.89795918, 9.08163265,
9.26530612, 9.44897959, 9.63265306, 9.81632653, 10\. ])
#numpy #python #programming #developer