Python NumPy Example | Getting Started With NumPy
NumPy is the fundamental package for scientific computing with Python. NumPy stands for Numerical Python. It consists of multidimensional array objects and the collection of functions for processing those arrays.
Using NumPy, mathematical and logical operations on arrays can be performed. That is why it is very useful in Data Science and Machine Learning.
Python NumPy Example
NumPy enriches the programming language Python with dominant data structures, implementing multi-dimensional arrays and matrices. These data structures guarantee efficient calculations with matrices and arrays.
If you have installed the software like Anaconda, then you already installed the NumPy library.
Although, a lightweight alternative is to install NumPy using a popular Python package installer, pip.
Go to your terminal and run the following command.
pip3 install numpy
If that gives you permission or IO errors, try using sudo.
sudo pip3 install numpy
Now, you have successfully installed the NumPy package.
NumPy is often used along with packages like SciPy (Scientific Python) and Matplotlib (plotting library). This common combination is widely used as the replacement for MatLab, the popular platform for technical computing.
However, Python’s alternative to MatLab is now seen as a more modern and complete programming language.
NumPy – Ndarray Object
The most important object defined in NumPy is the N-dimensional array type called ndarray.
It describes the collection of elements of the same type. Items in the collection can be accessed using the zero-based index.
Any item extracted from the ndarray object (by slicing) is represented by a Python object of one of array scalar types. See the below example of Ndarray.
# app.py import numpy as np data = np.array([21,22,23]) print(data)
The output of the above example is below.
Shape of Array
You can check the shape of the array with the object shape preceded by the name of the array. In the same way, you can check the type with dtypes.
# app.py import numpy as np data = np.array([21,22,23]) print(data.shape) print(data.dtype)
In the above example, we are printing the shape of an array and dtype of an array.
Let’s see the output below.
Two Dimension Array in NumPy
You can add more dimensions with a “, “coma and it has to be within the bracket .
# app.py import numpy as np data = np.array([ (1, 2, 3), (4, 5, 6), ]) print(data.shape) print(data.dtype)
In the above example, we have taken three rows and two columns. So the output looks like this.
Python NumPy Array v/s List
We use python numpy array instead of a list because of the below three reasons.
- Less Memory
Python NumPy Operations
Let’s see one by one operation.
You can find the dimension of an array, whether it is a two-dimensional array or the single-dimensional array. See the below example.
# app.py import numpy as np data = np.array([ (1, 2, 3), (4, 5, 6), ]) print(data.ndim)
See the output.
We can calculate a byte size of each element using itemsize.
See the below example.
# app.py import numpy as np data = np.array([ (1, 2, 3), (4, 5, 6), ]) print(data.itemsize)
You can find a data type of the elements that are stored in the array.
So, if you want to know the data type of the particular item, you can use the ‘dtype’ function which will print the data type along with the size.
# app.py import numpy as np data = np.array([ (1, 2, 3), (4, 5, 6), ]) print(data.dtype)
We can use the reshape function when we need to change the number of rows and columns, which gives a new view of an object.
# app.py import numpy as np data = np.array([ (1, 2, 3), (4, 5, 6), ]) print('before reshaped: ',data) resphaped_data = data.reshape(3, 2) print('after reshaped: ',resphaped_data)
See the below output.
There are lots of other operations that you can perform using NumPy.
Finally, Python NumPy Tutorial or Getting Started With NumPy is over.