Signup/Sign In

Creating high-performance Arrays with numpy.arange() method

Posted in Programming   LAST UPDATED: OCTOBER 14, 2017

    If you are here we hope you have already gone through the previous tutorials of this series - The Introduction to NumPy and How NumPy Arrays are better than the Python Lists.

    In this tutorial we will start learning How to use NumPy to generate arrays, and we will be using the arange() method provided by NumPy package for this.

    How numpy.arange() method works?

    This method returns an array with evenly spaced elements as per the interval secified.

    The interval mentioned is half opened i.e. [Start, Stop), which means that it will include the Start element but will not include the Stop element.

    Length of array being generated can be easily calculated - Ceil((Stop - Start) / Step)


    What are the Parameters - arange(start, stop, step, dtype):

    start: This is an optional parameter and defines the start of the interval range for which the array is created. By default its value is zero.

    stop: This parameter defines the end of the interval range.

    step: This is also an optional parameter. This parameter defines the step size of the interval. It's value by default is 1, which means that all the numbers starting from start till the stop value will be incuded into the array. If the step parameter has a value 2, it will mean that starting from the start value, every second number will be included into the array, until the stop value.

    dtype: This defines the type of the output array. It can be int, float etc.


    Time for an Example:

    # Example for the
    # numpy.arange method
     
    import numpy as dude
    
    # reshape is a numpy method to arrange array into 2D matri of size 2x2
    print("Array 1: \n", dude.arange(4).reshape(2, 2), "\n")
    
    # step value is 1 by default
    print("Array 2: \n", dudedude.arange(6, 12), "\n")
    
    # step value is defined as 3
    print("Array 3: \n", dude.arange(8, 24, 3), “\n”)

    Output:

    Array 1:

    [[0 1]
    [2 3]]

    Array 2:

    [6 7 8 9 10 11]

    Array 3:

    [8 11 14 17 20 23]


    Why choose numpy.arange() method over the built in range() method in Python?

    If you know the Python programming language then you must have an idea about the range() function which can be used to create Lists in python. So the first difference is that range() function will return a List while arange() returns a numpy array.

    The second and more important factor supporting the use of numpy.arange() method is speed. It is the faster solution. The below program showcases the time taken by range functin and arange function in python.

    import numpy as np
    import sys
    
    sys.version
    # out: '2.7.3rc2 (default, Mar 22 2012, 04:35:15) \n[GCC 4.6.3]'
    np.version.version
    # out: '1.6.2'
    
    size = int(1E6)
    
    %timeit for x in range(size): x ** 2
    # out: 10 loops, best of 3: 136 ms per loop
    
    %timeit for x in xrange(size): x ** 2
    # out: 10 loops, best of 3: 88.9 ms per loop
    
    # avoid this
    %timeit for x in np.arange(size): x ** 2
    #out: 1 loops, best of 3: 1.16 s per loop
    
    # use this
    %timeit np.arange(size) ** 2
    #out: 100 loops, best of 3: 19.5 ms per loop

    In the above program you can see that arange function is almost 4 times faster.

    About the author:
    Aspire to Inspire Before I Expire :)
    Tags:python-programpythonnumpy
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS