NumPy
NumPy is a Python library.
NumPy is used for working with arrays.
NumPy is short for "Numerical Python".
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.
NumPy stands for Numerical Python.
The array object in NumPy is called ndarray, it provides a lot of supporting functions that make
working with ndarray very easy.
Arrays are very frequently used in data science, where speed and resources are very important.
Which Language is NumPy written in?
NumPy is a Python library and is written partially in Python, but most of the parts that require fast
computation are written in C or C++.
NumPy Getting Started
Installation of NumPy
If you have Python and PIP already installed on a system, then installation of NumPy is very easy.
Install it using this command:
C:\Users\Your Name>pip install numpy
If this command fails, then use a python distribution that already has NumPy installed like, Anaconda,
Spyder etc.
Import NumPy
Once NumPy is installed, import it in your applications by adding the import keyword:
import numpy
Now NumPy is imported and ready to use.
Example:
import numpy
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
o/p: [1 2 3 4 5]
NumPy as np
NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same thing.
Create an alias with the as keyword while importing:
import numpy as np
Now the NumPy package can be referred to as np instead of numpy.
Example:
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
o/p: [1 2 3 4 5]
Checking NumPy Version
The version string is stored under __version__ attribute.
import numpy as np
print(np.__version__)
NumPy Creating Arrays
Create a NumPy ndarray Object
NumPy is used to work with arrays. The array object in NumPy is called ndarray.
We can create a NumPy ndarray object by using the array() function.
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
o/p: [1 2 3 4 5]
<class '[Link]'>
Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays).
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.
Create a 0-D array with value 42
import numpy as np
arr = [Link](42)
print(arr)
o/p: 42
1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
These are the most common and basic arrays.
Example
Create a 1-D array containing the values 1,2,3,4,5:
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
o/p: [1 2 3 4 5]