In this post, we are going to learn about the basics of NumPy Library how to install and import, use, what is numpy ndarray.
What is NumPy Library?
Python Numpy is an open-source package for scientific computing. It is used for working with numerical data in Python and also as Numeric Python.NumPy library is used widely in Pandas, SciPy, Matplotlib, sci-kit-learn, scikit-image, and much other data science and scientific Python packages. It a Python package that provides support for:
- A multidimensional array object.
- Masked arrays and matrices.
- Functions for fast operations on arrays.
- Coverage for operations in mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms basic linear algebra, basic statistical operations, random simulation, and much more.
- NumPy fully supports OOPS, ndarray is a class. This class contains methods and attributes. This provides the flexibility to the NumPy library to support several operations on n-Dimensional arrays.
- In NumPy all operations behave with implicit element-by-element fashion, i.e., they broadcast. So operations on one element can broadcast to other elements of the array which makes the operations faster.
Features of NumPy library are:
- NumPy arrays have a fixed size at the time of creation, if we want any change at runtime then ndarray creates a new array and deletes the old one.
- The elements in a NumPy array must be of the same data type and memory size. But if we have objects then it allows different size elements.
- ndarray is designed with Vectorization, which means the absence of explicit looping and indexing etc at the time of operations. This helps in faster code execution, easy code management, fewer bugs.
Installing of NumPy library
The complete instruction of installing NumPy library on your OS detail here
- Download & Install the Python executable binaries on your Windows system from Python.org.
- Run the Python executable installer to complete the installation.
- Install pip on your machine.
- use the below pip commands to install NumPy Library on your PC.
- Open the command prompt and use the command “pip install numpy”
pip install numpy
How to import NumPy Library
To access NumPy and its features we need to import numpy in our code by using below we can import numpy
import numpy as np
We have used shorten import name as np for better readability of code using NumPy.
What is ndarray – NumPy array
The core component of the NumPy library is the ndarray. ndarray object provides the implementation of n-dimensional arrays of homogeneous data types. ndarray is designed and implemented to accomplish high performance. ndarray has several important differences when it’s compared with the standard Python sequences like lists and dictionaries etc.
NumPy arrays are optimized for advanced mathematical and scientific operations with large data sets. These optimizations in ndarray make it superior to Python’s built-in sequences.
- Numpy Array attribute
- View Of Ndarray In NumPy
- NumPy array- Mean, Median, std, var function
- Array Shape Manipulation using flatten() function ‘C’,’F’,’A’ & ‘K’ order
- NumPy using split(), hsplit() & vsplit() functions.
- NumPy Array Creation using identity() , full() and full_like() functions
- NumPy array Attributes-Transpose, Real, Imaginary & flat
How to create ndarray-numpy.array()
The Python NumPy module mainly used in arrays manipulation, array objects in Numpy know as ndarray. The NumPy library array() method to create an array ndarray from sequences like list, lists of the list, tuple, or array_like object.
1. Create 1 Dimensional NumPy array from List
In this example, we are passing a list a sequence type to create a 1D NumPy array.
import numpy as np array_one = np.array([14,15,16,17,18,23]) print("one dimesnional NumPy array from list :",array_one)
one dimesnional NumPy array from list : [14 15 16 17 18 23]
2. Create 1D NumPy Array from tuple
In this code example, we are passing a tuple to np.array() method to create 1D NumPy array from tuple.
import numpy as np tuple = (14,56,16,21,78,25) np_array = np.array(tuple) print("Shape: ", np_array.shape) print("NumPy 1D array from Python Tuple :\n",np_array)
Shape: (6,) NumPy 1D array from Python Tuple : [14 56 16 21 78 25]