# Introduction to NumPy

*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.

```
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:

```
np?
```

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:

```
np.zeros(8)
```

#### 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:

```
np.ones(8)
```

#### 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:

```
np.ones((8,5))
```

#### 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:

```
np.full((4, 4), 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.

```
np.arange(10, 25)
```

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:

```
np.arange(10, 26)
```

#### 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:

```
np.arange(10,25,2)
```

What would happen if we set STEP = 1?

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

```
np.arange(10,25,1)
```

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…

```
np.arange(10, 25, 0)
```

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:

```
np.arange(25,10,-1)
```

```
np.arange(25,10,-2)
```

Note, it created an array in reverse order.

Why we chose larger_value first?

Let’s try to find answer via another example…

```
np.arange(10,25,-1)
```

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)*

```
np.linspace(0, 10, 5)
```

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

## One thought on “Introduction to NumPy”