3.10 Introduction to NumPy
Introduction to NUMPY: -
- NumPy stands for Numerical Python.
- It is a Python library used for scientific and mathematical computing.
- It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on them.
- It is faster than regular Python lists.
- It uses less memory.
- It is compatible with many data science libraries (like Pandas, Matplotlib, SciPy, etc.).
Features
of NumPy: - NumPy has various useful features:
- It contains N-dimensional array object.
- It is compatible with many hardware and platforms.
- It has a tool that enable integration with FORTRAN, C or C++ code.
- It can perform high-level mathematical functions like statistics, sorting, searching linear algebra and etc.
- It supports scientific and financial calculations.
Installing
NUMPY: - to install NumPy, we have to follow below steps:
- Step 1: - open command prompt.
- Step 2: - write command “py – m pip install numpy”.
- Step 3: - don’t close the window until the installation complete.
- This command will search for latest version of NumPy on the internet and download it.
Creating
NumPy arrays: -
- NumPy means creating homogenous n-dimensional arrays.
- All the elements of NumPy array should be of same type.
- It is designed to deal with large arrays.
- To use arrays in python, we need to import the “NumPy” module.
- Here, “numpy” represents the module name. “np” is an alias name for “numpy”.
- After importing NumPy module, it can able to get any class and object of the module and array class from “numpy” module.
- NumPy provides many functions to create arrays. To create array in python we use most frequently used functions.
1. Creating arrays using array( ) function.
2. Creating array using arrange( ) function.
1) Creating
arrays using array( ) function: -
- The function array( ) of NumPy module is used to create an array.
- It converts a list to an array.
- We should take only same type of data.
Output: -
Example 2: - two-dimensional array
Output: -
Example 3: - shape of an array
Output: -
2) Creating
array using arrange( ) function: -
- The arrange( ) function create arrays with regularly incrementing values.
- It returns evenly spaced values in the given interval.
- It is similar to python range( ) function.
- The format of the function is as:
- Start: - The starting value. If omitted, defaults to 0.
- Stop: - The ending value. (not including this number)
- Step: - spacing between values, default is 1.
Example: -
Output: -