Python NumPy Package


Machine Learning / Sunday, March 4th, 2018

In this blog, we will be discussing some basic operations and techniques of NumPy package in Python. This concept is totally for beginners though some basic syntax knowledge of Python syntax would be great.


Introduction

NumPy package in Python stands for Numeric (or numerical) Python. NumPy enhances the properties of Python by providing a large library of high-level mathematical and numerical functions to operate with, furthermore its an open source module extension thus making it widely used and preferable.

NumPy introduces array-based computing concept by featuring the use of arrays and matrices which could be of any dimension (1-dimensional, 2-dimensional, 3-dimensional, etc.) and also provides efficient functions to compute over multi-dimensional arrays and matrices. Also, SciPy is often used over the NumPy module to incorporate further useful operations.

It may not be already installed in your PC so, you could follow the instructions from its official site to download and install it on various platforms.


Creation of a NumPy Array

We could create a NumPy array by sending a list of arguments, by loading an already existing file, or by using NumPy’s functions as shown under :

Above snippet shows how to import a file using NumPy package, however we could also create arrays using NumPy’s built-in functions like :


NumPy Array Indexing

To get a single element out of an array we just call its position with the name of the array. Also, you must know that it is “zero-indexed” module; that is, the first element is stored at index 0, so you must place the call accordingly. For one-dimensional array, a basic syntax for calling an element is as under:

>>>   array_name[ index/location ]

However when dealing with two-dimensional arrays, the syntax changes a bit like:

>>>   array_name[ row, column ]

where, row and column depicts the index of rows and columns of the matrices, starting from zero as already mentioned.

Also, we could get a range of values from the array or matrix, this process is known as Slicing and is done as under:

>>>   array_name [ starting index : ending index]

You could use the above code for a single-dimensional array as well as multi-dimensional array , though with each dimension having a starting and an ending point explicitly.

NOTE : the slice made will be having an element less than the ending index mentioned in the call.


NumPy Random Submodule

To generate random numbers, NumPy provides some routines under the random submodule. Using it, we could also generate arrays with random values for experimental purposes as under:

 


Operations over NumPy Arrays

We could perform operations like addition, subtraction, multiplication, etc. on NumPy arrays using another array or even a scalar value. For instance, try the following code:

We could operate on arrays whether they have same dimension or different, and this leads to another concept of Python NumPy module known as Broadcasting.

Note : You may get some warnings and values such as ‘nan’ and/or ‘inf’ when performing division operation; where NaN stands for ‘Not A Number’ and Inf stands for ‘Infinity’. However, you should not worry about the warning as the code will run anyway.


NumPy Array Broadcasting

The term ‘broadcasting’ refers to the property of NumPy module which comes into play when doing arithmetic operations on the arrays and matrices.

While performing arithmetic operations on arrays having different dimension, NumPy just stretch the shape virtually to perform the required op instead of creating additional useless copies of data,. This process is termed as ‘smaller array is broadcasted over the larger array’. There are certain rules to be satisfied during broadcasting :

  • The size along each dimension must be same i.e. if a is 7x2x3 then to operate it with b, b’s dimension can be 2×3.
  • or if not same, then one of them should be 1.

The operations used above with scalars are the part of simplest broadcasting examples.


Other Basic NumPy Routines

Above are various operations you may go through and also could visit the Official NumPy documentation for any further details.


Thus, NumPy provides mathematical and logical operations and routines to support scientific computing using array-based programming.

If you’ve liked the efforts, then like and share the article. You could also post your doubts in comments or contact forum of this site, and don’t forget to subscribe to my feeds. Thank You!

5 Replies to “Python NumPy Package”

  1. Hello ,

    I saw your tweets and thought I will check your website. Have to say it looks very good!
    I’m also interested in this topic and have recently started my journey as young entrepreneur.

    I’m also looking for the ways on how to promote my website. I have tried AdSense and Facebok Ads, however it is getting very expensive.
    Can you recommend something what works best for you?

    I also want to improve SEO of my website. Would appreciate, if you can have a quick look at my website and give me an advice what I should improve: http://janzac.com/
    (Recently I have added a new page about FutureNet and the way how users can make money on this social networking portal.)

    I have subscribed to your newsletter. 🙂

    Hope to hear from you soon.

    P.S.
    Maybe I will add link to your website on my website and you will add link to my website on your website? It will improve SEO of our websites, right? What do you think?

    Regards
    Jan Zac

    1. It was nice to hear on your thoughts of my website. I just visited your website and it looks great. And I haven’t used any sort of marketing for my site, i just share it on social media platforms.

Leave a Reply

Your email address will not be published. Required fields are marked *