What is Numpy and Why to Use it in Python ???

What is Numpy and Why to Use it in Python ???

By reading this blog , you will be able to understand basics of Numpy Library in Python. I will try to cover basics of Numpy using below content

  1. What is Numpy
  2. Why Use NumPy?
  3. Difference between NumPy array and standard array
  4. Why is NumPy Fast?
  5. Why is NumPy Faster Than Lists?
  6. Which Language is NumPy written in?
  7. Where is the NumPy Codebase?
  8. Installation of NumPy
  9. Test Whether NumPy is Installed or Not
  10. Checking NumPy Version

Let's get started

numpy.png

What is Numpy ?

According to the documentation on NumPy

a. NumPy is the fundamental package for scientific computing in Python.

b. NumPy was created in 2005 by Travis Oliphant.

c. Numpy is an open source project and you can use it freely.

d. NumPy stands for Numerical Python.

Numpy is a Python library that provides

  1. a multidimensional array object,
  2. various derived objects (such as masked arrays and matrices), and
  3. an assortment of routines for fast operations on arrays, including

    a. mathematical

    b. logical

    c. shape manipulation

    d. sorting

    e. selecting

    f. I/O

    g. discrete Fourier transforms

    h. basic linear algebra

    i. basic statistical operations

    j. random simulation and much more.

At the core of the NumPy package, is the ndarray object. This encapsulates n-dimensional arrays of homogeneous data types, with many operations being performed in compiled code for performance.

Why Use NumPy?

In Python we have lists that serve the purpose of arrays, but they are slow to process. NumPy aims to provide an array object that is up to 50x faster than traditional Python lists. 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.

Difference between NumPy array and standard array

There are several important differences between NumPy arrays and the standard Python sequences:

NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the original.

• The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory. The exception: one can have arrays of (Python, including NumPy) objects, thereby allowing for arrays of different sized elements.

NumPy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operations are executed more efficiently and with less code than is possible using Python’s built-in sequences.

• A growing plethora of scientific and mathematical Python-based packages are using NumPy arrays; though these typically support Python-sequence input, they convert such input to NumPy arrays prior to processing, and they often output NumPy arrays.

In other words, in order to efficiently use much (perhaps even most) of today’s scientific/mathematical Python-based software, just knowing how to use Python’s built-in sequence types is insufficient - one also needs to know how to use NumPy arrays.

Why is NumPy Fast?

NumPy’s features which are the basis of much of its power: vectorization and broadcasting . Due to these two features Numpy is much much faster .

Vectorization

Vectorization describes the absence of any explicit looping, indexing, etc., in the code - these things are taking place, of course, just “behind the scenes” in optimized, pre-compiled C code.

Vectorized code has many advantages, among which are:

• vectorized code is more concise and easier to read

• fewer lines of code generally means fewer bugs

• the code more closely resembles standard mathematical notation (making it easier, typically, to correctly code mathematical constructs)

• vectorization results in more “Pythonic” code. Without vectorization, our code would be littered with inefficient and difficult to read for loops.

Broadcasting

Broadcasting is the term used to describe the implicit element-by-element behavior of operations; generally speaking, in NumPy all operations, not just arithmetic operations, but logical, bit-wise, functional, etc., behave in this implicit element-by-element fashion, i.e., they broadcast.

Why is NumPy Faster Than Lists?

NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently. This behavior is called locality of reference in computer science.

This is the main reason why NumPy is faster than lists. Also it is optimized to work with latest CPU architectures.

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++.

Where is the NumPy Codebase?

The source code for NumPy is located at this github repository github repository

Installation of NumPy

If Python and PIP are already installed on a system, then installation of NumPy is very easy. Install it using this command:

C:\Users\Your Name>pip install numpy

Test Whether Numpy is Installed or Not

To test whether NumPy module is properly installed, try to import it from Python prompt.

import numpy

If it is not installed, the following error message will be displayed.

Traceback (most recent call last): 
   File "<pyshell#0>", line 1, in <module> 
      import numpy 
ImportError: No module named 'numpy'

Alternatively, NumPy package is imported using the following syntax −

import numpy as np

Checking NumPy Version

The version string is stored under version attribute.

import numpy as np
print(np.__version__)