Friday 21 July 2017

Data Science & Machine Learning - 3.5 NumPy Array Methods

Hi friends,

Welcome to another NumPy tutorial under Data Science & Machine Learning. In the previous post, we discussed several ways to index NumPy Arrays and Conditional Selection in NumPy Arrays. In this post, we will learn several operations that we can perform on NumPy Arrays. We will also see various methods supported by the NumPy library to deal with NumPy Arrays.

Note: All the commands discussed below are run in the Jupyter Notebook environment. See this post on Jupyter Notebook to know about it in detail.


NumPy Array Methods

So, let's first declare a NumPy Array using the NumPy's arange() function:


  1. We can add/subtract a scalar to the NumPy Array:


  2. We can multiply/divide a scalar to the NumPy Array:


We can perform the same operations among the NumPy Arrays as well.
  1. Addition/Subtraction between two NumPy Arrays:


  2. Multiplication/Division between two NumPy arrays:



Notice the warning generated due to division by zero operation. However, the division was successful and the output was inf (for 1/0).

We can also perform exponentiation operation using the ** operator in Python. Here is the operation for raising each element of the NumPy Array to the power of 3:


Now, we see some useful methods supported by NumPy library for NumPy arrays:
  1. sqrt() - This method finds the square root of each element of a NumPy array


  2. max() - This returns the maximum element in a NumPy Array
  3. min() - Returns the minimum element of a NumPy Array


  4. Trigonometric functions - NumPy also supports trigonometric methods like sin(), cos() and tan()


      1. mean() - Returns the mean of a NumPy Array
      2. log() - Performs the natural logarithm of each element of a NumPy Array


        Notice, there is a warning since logarithm of zero does not exist.
      You can visit this link to know the list of all the functions supported over NumPy arrays by the NumPy library. 

      From the next post, we'll start with Pandas, another very important library for Data Science under Data Science & Machine Learning.
      Share:

      0 comments:

      Post a Comment

      Contact Me

      Name

      Email *

      Message *

      Popular Posts

      Blog Archive