Python for machine learning: numPy – Part 1

In this post, we will look into the NumPy library in python. NumPy is a powerful python library which adds multidimensional array support and functions to manipulate the arrays to python. In this post, we will look at some basic things about numPy. You can learn more about numPy from here. Since we are dealing with some basic machine learning I won’t go deep into numPy.

NumPy library is well documented and very easy to understand with many examples. So it is better to look at the documentation.

Let us see how we can create a basic numPy array. This post is organized as follows

Part 1: (this post)

  1. Basic 1-D array creation
  2. 2D and 3D array creation

Part 2 : (next post)

  1. Functions for creating arrays
  2. Visualization (using matplotlib library)
  3. Indexing and slicing

If you install anaconda distribution then numPy is already preinstalled otherwise please look at numPy installation guide it is very easy to install using PIP.

Before we proceed to array creation we need to import numPy using

import numpy as np

Basic 1-D array creation

a 1-D array is a simple array with only one dimension. I’ve given the basic code to create a 1-D array

a = np.array([0, 1, 2, 3])

The output will be

[0 1 2 3]

From this post onward i will not be including the examples by writing it separately as code and output instead i will present the above example as follows

>>> import numpy as np
>>> a = np.array([0, 1, 2, 3])
>>> print(a)
[0 1 2 3]

if you see the above example you can see the lines that have “>>>” are the code that we write and the lines without “>>>” is the output that we get when we execute the code.

Now lets see another example of 1-D array with words

>>> import numpy as np
>>> a = np.array(['hello','world','how', 'are','you'])
>>> print(a)
['hello' 'world' 'how' 'are' 'you']

if you see above we can create a word array also with numPy but for this topic we will stick to numbers.

we will see some basic functions to view the size and dimension of the array.

>>> import numpy as np
>>> a = np.array([0,1,2,3,4])
>>> print(a)
[0 1 2 3 4]
>>> a.size
>>> a.ndim
>>> len(a)

The size function will give the size of the array. The ndim function will give the dimension of the array. The length for the 1-D array will return the size of the array.

2D and 3D array creation

The 2D and 3D array unleashes the power of numPy. Many datasets will often include many dimensions so it is best to learn how to create multi dimension array. To see how we can create 2D array below

>>> b = np.array([[0, 1, 2], [3, 4, 5]])
>>> b
array([[0, 1, 2],
       [3, 4, 5]])

Now we will see some basic function in numPy. it is similar to the 1D array but the output will be somewhat different.

>>> b.ndim
>>> b.shape
(2, 3)
>>> len(b)

As you can see above the functions are very similar to 1D array.

We will now create 3D array and see how it is different

>>> c = np.array([[[1], [2]], [[3], [4]]])
>>> c

>>> c.shape
(2, 2, 1)

Now we know how to create array and some basic function to explore the dimension of it. In the next post we will see some functions for creating numPy array, visualizing the data in the numPy and Indexing and slicing of it.

Happy Coding

Python for Machine learning

In this series we will see python programming language and how we can use it for machine learning and Data Science. In this series we cover

  1. Installing Anaconda Distribution for Data Science.
  2. Basic Python programming language
  3. numPy
  4. Pandas
  5. Sklearn
  6. Basic programs for Data exploration and Data cleaning.

In the next post i will discuss about installing anaconda distribution and how to get started with Python

Essentials for learning Machine learning

To get started with machine learning we need some things.

  1. Python
  2. Pandas
  3. numPy
  4. Scikit-learn
  5. Jupyter notebooks or Jupyter Lab

You can get all these and install it manually or download Anaconda Distribution so it can set everything for you. to download anaconda click the link below

If you have any difficulty with the installation comment on the forum post below. I have setup a forum so that we can discuss all the things that are needed. To go to the forum click the link below

Happy Learning