# Basics of NumPy Arrays Part – 1

Hello Enthusiastic Learners! Its time to learn Basics of NumPy Arrays. Arrays are the base of everything, since everything in our computer are numbers, all of them are being processed via an Array. When it comes to effective data processing and data manipulation, NumPy is one of the key libraries to get results in most efficient way. Even library “Pandas” is built around NumPy. So, it is good to have good knowledge of basics of NumPy arrays. For more basics of NumPy arrays & good introduction to it, visit our article Introduction to NumPy

Some of the things that we will cover in this article are:

- Array Attributes – Shape, size, data types & more.
- Slicing of Arrays – Accessing sub-arrays from larger arrays
- Array Indexing – Accessing array elements

## NumPy Array Attributes

Attributes of an array corresponds to – its sieze, dimensions, size of each dimension & data type of values stroed in array.

We will learn all this with help of few examples.

Before we jump on to creating arrays with random number, a little about random number genrator “seed”.

*SEED* – while genrating random numbers if we want eveytime same sequence of random numbers must be generated then we provide a value called “SEED” which when kept same for every run will make sure that same sequence is generated.

Now, we will create few Arrays.

### Importing libraries

```
import numpy as np
# seed for reproducing same random number sequence
np.random.seed(10)
# 1 dimensional Array
a1 = np.random.randint(5, size=10)
# it will create a 1 dimensional array having 10 values and all below 5
a1
```

```
# @ dimensional Array
a2 = np.random.randint(5, size=(2,10))
# it will create a 2 dimensional array having 10 values in each row and all below 5
a2
```

```
# @ dimensional Array
a3 = np.random.randint(5, size=(3, 2, 10))
# it will create a 3 dimensional array having 10 values in each row all below 5
# In each dimension there will be 2 rows
a3
```

### Fetching attributes of arrays

Let’s fetch attributes for above arrays:

– number of dimensions in each array*ndim*– dimensions*shape*– total number of elements in array*size*– data type of array elemets (in current case it will be int64 only, as we used*dtype**randint*which genrated int type values only)

```
print("a1.ndim \t= '" + str(a1.ndim) + "' dimension(s) in array")
print("a1.shape \t= '" + str(a1.shape) + "' -- single row with 10 elements each")
print("a1.size \t= '" + str(a1.size) + "' -- total elements in array")
print("a1.dtype \t= '" + str(a1.dtype) + "' type data in array")
```

```
print("a2.ndim \t= '" + str(a2.ndim) + "' dimension(s) in array")
print("a2.shape \t= '" + str(a2.shape) + "' 2 rows with 10 elements each")
print("a2.size \t= '" + str(a2.size) + "' -- total elements in array")
print("a2.dtype \t= '" + str(a2.dtype) + "' type data in array")
```

```
print("a3.ndim \t= '" + str(a3.ndim) + "' dimension(s) in array")
print("a3.shape \t= '" + str(a3.shape) + "' 3 dimensions, 2 rows in each dimension with 10 elements each")
print("a3.size \t= '" + str(a3.size) + "' -- total elements in array")
print("a3.dtype \t= '" + str(a3.dtype) + "' type data in array")
```

### Fetching size of array

If you want to get size of array & its element in bytes you can use following functions:

- itemsize – size of each element of array in bytes
- nbytes – size of complete array in bytes

```
print("Size of each item in bytes \t= " + str(a3.itemsize))
print("Size of array in bytes \t = " + str(a3.nbytes))
```

### You can also check-out same tutorial in Video Format below

## Accessing array elements (Array Indexing)

Here is our 1 dimensional array:

```
a1
```

Fetching first element of array use following command:

```
a1[0]
```

Get 5th element by using the code below:

```
a1[4]
```

Due to what we read so far, we can say to access nth element we use *array[n-1]*

One can use negative values for indexing (or traversing) array from end.

In order to access last element of array we can use following:

```
a1[-1]
```

For fetching 5th element from behind:

```
a1[-5]
```

Certainly, for multi-dimension array, we can access values by adding a comma (,).

Here is our 2 dimensional array:

```
a2
```

Below code fetches 1st element:

```
a2[0,0]
```

Accessing 3rd element from 2nd row of array:

```
a2[1,2]
```

Similarly, you can traverse 2 dimensional array as well from behind.

For getting 2nd last element from 2nd row of array:

```
a2[-1, -2]
```

To access 2nd last element from 1st row of array:

```
a2[-2, -2]
```

## Slicing of Arrays

We will be accessing sub arrays from larger arrays. Hence, it is very important to learn that how we can access sub elements from an array because in many situations like while sampling data we need to fetch smaller data or sample from given data.

Above all, the syntax for slicing arrays is as follows:

*x[ start : stop : step ]*

– Starting position, from which we will start accessing values from larger array. Default value*start**start = 0*.– Last position in array till which you want to get values from larger array. Default value*stop**stop = size of dimension*.– as discussed in our article “Introduction to NumPy“, step is the value by which we are skipping values. Default value*step**step = 1*.

### Slicing on 1-Dimensional Array

Let’s proceed with creating an array.

```
x = np.arange(11)
x
```

Access first 4 elements

```
x[ : 4]
```

Note: Element at “stop” position is not included

Further, One can access elements after 4th element

```
x[ 4 : ]
```

Fetch elements 4th to 8th element

```
x[ 4 : 8 ]
```

All elements skipping 2 values, like 0, 3, 6, 9. For this we will *step = 3*

```
x[ : : 3 ]
```

All elements skipping 1 value and values starting from 4th element.

```
x[ 3 : : 2 ]
```

#### Using negative steps

Yes, negative STEP values are also allowed. In this case default values of ** start** &

**value are swapped. Therefore, we will receive an array in reverse order.**

*stop*All elements skipping 1 value and values starting from 8th element from end.

```
x[ 8 : : -2 ]
```

You can also use this technique to reverse an array.

```
x[ : : -1]
```

### Slicing on Multi-Dimensional Array

Let’s proceed with creating an array.

```
x = np.arange(20).reshape(4,5)
x
```

*reshape(rows, elements)*

*reshape(rows, elements)*

reshape is used to reshape our larger array as per *rows* & *elements* provided in *reshape()* function.

First of all, let’s fetch first 2 rows and 4 columns

```
x[ : 2, : 4 ]
```

Also, we can fetch first 2 rows and 4 columns with STEP=2 on column values.

```
x[ : 2, : 4 : 2]
```

Reversing multi-dimensional array

```
x[: : -1, : : -1]
```

So far, we have covered many powerful functionalities of NumPy Arrays. Practice them and also play around with steps and multi-dimensional array. Further, we will be providing more in-depth knowledge in our next article “Basics of Numpy Array Part-2”

Stay tuned. Keep Learning! Also, for video tutorials check our YouTube channel ML for Analytics

## 5 thoughts on “Basics of NumPy Arrays Part – 1”