# 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])
print(a)```

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
5
>>> a.ndim
1
>>> len(a)
5```

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
2
>>> b.shape
(2, 3)
>>> len(b)
2```

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([[, ], [, ]])
>>> c
array([[,
],

[,
]])
>>> 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