Getting Started With Numpy

🏷 ["basics"]

What is numpy?

Numpy (remember it as numerical python) is a package which is used for scientific computing in python. It contains data structures which could handle many operations which are typically encountered in scientific computation in a fast and efficient way. To name a few, it can perform the following operations


Why numpy?

A simple answer would be

Numpy is fast, efficient, has a wide range of utility functions.

However, a more expository version is as follows.

Numpy arrays are vectorized. Vectorized implementation meaning the user doesn’t have to write any looping constructs like for or while for repetitive operations. All of it is handled behind the scenes in an optimized, pre-compiled C code.

Vectorized implementations are computationally cheap than loops, therefore they are faster. x below is a numpy array of 5 numbers.

Comparison
Comparing Looped vs vectorized implementations for multiplying array with a scalar

In the absence of loops, written code more closely resembles the natural language. This makes the code easier to comprehend. For eg. compare matrix multiplication operation in vectorized v/s non-vectorized implementation below and decide for yourself. x and y are matrices of dimensions 1000 * 10 and 10 * 1000 respectively.

Vectorized Implementation
Vectorized implementations — Pythonic and Intuitive

Installing numpy

You should have python and pip installed for installing numpy to your machine. pip is a package manager which is used for installing packages in python. It stands for either Pip Installs Packages or preferred installer program.

You can visit this page on the official python website for installing python for any OS — Windows, Linux (Ubuntu, LinuxMint etc.), MacOS and others.

This YouTube Video is a good resource to install pip on your system.

Once you have them installed, you can open a command prompt in windows or a terminal in mac/ubuntu and type

pip install numpy

This will automatically install the latest version of numpy to your local system. To verify if numpy has installed properly, in your shell/command prompt, type the following commands. If they execute successfully, that means you’re good to go!

import numpy
print(numpy.__version__)

In the next post, we will get started with numpy arrays.You can find the next post below


References