Site icon Machine Learning For Analytics

Introduction to NumPy

Introduction to NumPy

Images, music, alphabets, all are saved in form of numbers only to be precise array of numbers. Images are stack of matrices having values ranging from (0 to 255) and similarly alphabets can be stored in machines in form of binary data, that is, numbers in an Array, though dimensionality may vary. While working on Data Science or Machine Learning or any analysis we always need a way which provides efficiency in storage, efficiency in manipulation of numerical arrays & that too quickest possible way. Thankfully! NumPy provides us all of this.

Numerical Python OR NumPy is extremely efficient in storing and operating on huge amount of data. It exhibits resemblance to Python’s built-in list type, yet NumPy arrays give substantially more effective storage and information activities as the arrays become bigger in size.

And NumPy is one the CORE elements of Data Science, so learning it is a must.

 

Let’s begin with importing Numpy library in Python.

In [1]:
import numpy as np

Note, everywhere you will see that NumPy is incorporated with “np” alias, so it will be good to add it to one of your good coding practices.

 

To get documentation of NumPy you can use following syntax:

In [2]:
np?
Type:        module
String form: <module 'numpy' from '\\anaconda3\\lib\\site-packages\\numpy\\__init__.py'>
File:        \anaconda3\lib\site-packages\numpy\__init__.py
Docstring:  
NumPy
=====

Provides
  1. An array object of arbitrary homogeneous items
  2. Fast mathematical operations over arrays
  3. Linear Algebra, Fourier Transforms, Random Number Generation

How to use the documentation
----------------------------
Documentation is available in two forms: docstrings provided
with the code, and a loose standing reference guide, available from
`the NumPy homepage <https://www.scipy.org>`_.

We recommend exploring the docstrings using
`IPython <https://ipython.org>`_, an advanced Python shell with
TAB-completion and introspection capabilities.  See below for further
instructions.

The docstring examples assume that `numpy` has been imported as `np`::

  >>> import numpy as np

Code snippets are indicated by three greater-than signs::

  >>> x = 42
  >>> x = x + 1

Use the built-in ``help`` function to view a function's docstring::

  >>> help(np.sort)
  ... # doctest: +SKIP

For some objects, ``np.info(obj)`` may provide additional help.  This is
particularly true if you see the line "Help on ufunc object:" at the top
of the help() page.  Ufuncs are implemented in C, not Python, for speed.
The native Python help() does not know how to view their help, but our
np.info() function does.

To search for documents containing a keyword, do::

  >>> np.lookfor('keyword')
  ... # doctest: +SKIP

General-purpose documents like a glossary and help on the basic concepts
of numpy are available under the ``doc`` sub-module::

  >>> from numpy import doc
  >>> help(doc)
  ... # doctest: +SKIP

Available subpackages
---------------------
doc
    Topical documentation on broadcasting, indexing, etc.
lib
    Basic functions used by several sub-packages.
random
    Core Random Tools
linalg
    Core Linear Algebra Tools
fft
    Core FFT routines
polynomial
    Polynomial tools
testing
    NumPy testing tools
f2py
    Fortran to Python Interface Generator.
distutils
    Enhancements to distutils with support for
    Fortran compilers support and more.

Utilities
---------
test
    Run numpy unittests
show_config
    Show numpy build configuration
dual
    Overwrite certain functions with high-performance Scipy tools
matlib
    Make everything matrices.
__version__
    NumPy version string

Viewing documentation using IPython
-----------------------------------
Start IPython with the NumPy profile (``ipython -p numpy``), which will
import `numpy` under the alias `np`.  Then, use the ``cpaste`` command to
paste examples into the shell.  To see which functions are available in
`numpy`, type ``np.<TAB>`` (where ``<TAB>`` refers to the TAB key), or use
``np.*cos*?<ENTER>`` (where ``<ENTER>`` refers to the ENTER key) to narrow
down the list.  To view the docstring for a function, use
``np.cos?<ENTER>`` (to view the docstring) and ``np.cos??<ENTER>`` (to view
the source code).

Copies vs. in-place operation
-----------------------------
Most of the functions in `numpy` return a copy of the array argument
(e.g., `np.sort`).  In-place versions of these functions are often
available as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``.
Exceptions to this rule are documented.

After running above command you will be able to see complete documentation of Numpy. You can even apply same logic to any other library too.

Creating Arrays using NumPy

When creating very large arrays then it is good to use built-in routines of NumPy , it helps us in achieving highest possible efficiency.

Creating a ZEROs array

ZEROs array, as the name suggest, is an array consisting all of its value equal to ‘0’.

For example, a ZEROs array of size 8 can be created in following way:

In [3]:
np.zeros(8)
Out[3]:
array([0., 0., 0., 0., 0., 0., 0., 0.])

Creating a ONEs array

Similarly, a ONEs array is an array that consists only ‘1’ as its value

For example, a ONEs array of size 8 can be created in following way:

In [4]:
np.ones(8)
Out[4]:
array([1., 1., 1., 1., 1., 1., 1., 1.])

Creating a ONEs Matrix

By using same function used earlier we can also create a matrix or in our case a 2D Array.

For example, a ONEs matrix of size 8×5, that is, 8 rows & 5 columns can be created in following way:

In [5]:
np.ones((8,5))
Out[5]:
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])

Creating a Matrix filled with ANY value

We will be creating a matrix filled a value provided by us.

For example, a matrix of size 4×4, that is, 4 rows & 4 columns filled with value ‘2.345’ can be created in following way:

In [6]:
np.full((4, 4), 2.345)
Out[6]:
array([[2.345, 2.345, 2.345, 2.345],
       [2.345, 2.345, 2.345, 2.345],
       [2.345, 2.345, 2.345, 2.345],
       [2.345, 2.345, 2.345, 2.345]])

Create an array filled with a sequence of numbers

We will be creating an array having values starting from 10 & ending at 25 using np.arrange() function.

In [7]:
np.arange(10, 25)
Out[7]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])

Note, if you want to include last digit as well, then enter value ‘+1′ to it.

In our case if we want to include ’25’ (see last number is ’26’), then it will look like:

In [8]:
np.arange(10, 26)
Out[8]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25])

STEP in an Array

By ‘STEP’ we literally mean steps, that is, if we are creating an array ranging from 10 to 25 (where 25 is not included) and STEP is given as ‘2’, then it create an array of like 10, 12, 14, etc…

That, is moving 2 steps ahead and putting our value in array.

Note, as the value of STEP increases size of array will decrease because we are basically skipping values.

Let’s see an example described above:

In [9]:
np.arange(10,25,2)
Out[9]:
array([10, 12, 14, 16, 18, 20, 22, 24])

 

What would happen if we set STEP = 1?

You, guessed it right it will create a standard array.

In [10]:
np.arange(10,25,1)
Out[10]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])

 

Can we set STEP=0?

Actually NO, we can’t because np.arange() includes a point at which it divides total size of standard array by STEP value to calculate number of element in an array.

Let’s check via coding too…

In [11]:
np.arange(10, 25, 0)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-11-f91cea88a88f> in <module>
----> 1 np.arange(10, 25, 0)

ZeroDivisionError: division by zero

 

As said suggested earlier we have got following error “ZeroDivisionError: division by zero”

 

We said, STEP can be any number other than ‘0’, does that mean it can be a NEGATIVE number too?

Answer is YES!

That is, we can traverse backwards too, however we will have to swap values, np.arange(larger_value, smaller_value, negative_step)

Here is an example for same:

In [12]:
np.arange(25,10,-1)
Out[12]:
array([25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11])
In [13]:
np.arange(25,10,-2)
Out[13]:
array([25, 23, 21, 19, 17, 15, 13, 11])

Note, it created an array in reverse order.

Why we chose larger_value first?
Let’s try to find answer via another example…

In [14]:
np.arange(10,25,-1)
Out[14]:
array([], dtype=int32)

As you can see it returned us an Empty array.

If we think logically, we are asking function to create an Array where

  • Min Value = 10
  • Max Value = 24 (as 25 is not included)
  • STEP = ‘-1’,

that is, we want to go one step back from ’10’, which results in ‘9’, however, range provided by us is 10 to 25 and 9 can’t fall in that range.

Thus, we received an EMPTY Array.

Create an array having evenly distributed values between two given numbers

Let’s assume we have to create an array having its values ranging between 0 to 10 and total number of values in array must be 5 and each value should be evenly spaced.

For this we will be using np.linspace(range_min, range_max, total_values)

In [15]:
np.linspace(0, 10, 5)
Out[15]:
array([ 0. ,  2.5,  5. ,  7.5, 10. ])

We received an array having 5 values and each value has equal space of ‘2.5’

 

So far we have seen few basic arrays created via NumPy and it clearly shows us that how NumPy makes things easy for us and how much versatile it is. In our next tutorial we will deep dive in NumPy Arrays and will do a lot of calculations and modeling on them. To know more about NumPy visit our next article “Basics of NumPy Part-1

Keep Learning & Keep Practicing! Also, for video tutorials please check our YouTube channel ML for Analytics

 

 

Exit mobile version