# NumPy Arrays – Numerical Python Arrays

Our focus today is on numPy arrays with examples, in order to provide users with a more effective learning experience. NumPy’s main object is homogeneous multidimensional arrays.

It is basically a table with all elements having the same type and indexed by a positive integer tuple.

### Syntax

```numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
```

## NumPy ndarray Object

The NumPy library is utilized to deal with arrays. When NumPy makes arrays, the array object is known as ndarray.

By calling NumPy array() function, we can make a NumPy ndarray object.

#### Example:1

import numpy as npy mrx_arr = npy.array(["Arizona Cardinals", "Buffalo Bills", "Chicago Bears", "Houston Texans"]) print(mrx_arr) print(type(mrx_arr))

#### Example:2

import numpy as npy mrx_arr = npy.array([4.74, 8.16, 9.22, 6.76, 25.19]) print(mrx_arr) print(type(mrx_arr))

type(): We can determine the type of an object by calling this Python function. The above code indicates that the mrx_arr type is numpy.ndarray.

A list, tuple, or any array-like object can be provided to the array() method to generate an ndarray.

Make a NumPy array utilizing a tuple:

#### Example:1

import numpy as npy mrx_arr = npy.array((4.74, 8.16, 9.22, 6.76, 25.19)) print(mrx_arr)

#### Example:2

import numpy as npy mrx_arr = npy.array(("Arizona Cardinals", "Buffalo Bills", "Chicago Bears", "Houston Texans")) print(mrx_arr)

## Array Dimensions

When we discuss numpy arrays, a dimension is the size of the array (nested arrays).

nested array (array in an array): The components of arrays are arrays.

### Array 0-D

The components of an array are 0-D arrays, or scalars. In an array, a value is a 0-D array.

Make a 0-D array with the element “mrexamples”:

#### Example:

import numpy as npy mrx_arr = npy.array("mrexamples") print(mrx_arr)

Set 33 as the value of a 0-D array:

#### Example:

import numpy as npy mrx_arr = npy.array(33) print(mrx_arr)

### Array 1-D

When numpy generates arrays, a 1-D array that has 0-D arrays is known as a 1-D array.

The most frequently used and fundamental arrays are listed here.

Make a 1-D array that includes 1990,1995,2000,2005,2010,2015,2020,2025:

#### Example:1

import numpy as npy year_arr = npy.array([1990,1995,2000,2005,2010,2015,2020,2025]) print(year_arr)Build a 1-D array with the values "Washington","London","Berlin","Paris","Islamabad","Rome":

#### Example:2

import numpy as npy capitals_arr = npy.array(["Washington","London","Berlin","Paris","Islamabad","Rome"]) print(capitals_arr)

## Array 2-D

A 2-D array a collection of 1-D arrays as its components, as we are talking about numpy creating arrays.

In many cases, they are utilized to display matrices or tensors of second order.

A complete separate module of NumPy is dedicated to matrix operations known as numpy.mat

You will need to generate a 2-D array that includes two arrays with the elements “Washington”,”London”,”Berlin” and “Paris”,”Islamabad”,”Rome”:

#### Example:

import numpy as npy capitals_arr = npy.array([["Washington","London","Berlin"],["Paris","Islamabad","Rome"]]) print(capitals_arr)

Make a 2-D array with three arrays with the elements 1, 2, 3, 4, 5 and 2, 4, 6, 8, 10 and 1, 3, 5, 7, 9:

#### Example:

import numpy as npy mrx_arr = npy.array([[1, 2, 3, 4, 5],[2, 4, 6, 8, 10],[1, 3, 5, 7, 9]]) print(mrx_arr)

### Array 3-D

Arrays with components that are 2-D arrays (matrices) are known as 3-D arrays.

Third order tensors are most commonly displayed by these.

Make a 3-D array from two 2-D arrays, each having two arrays with the data “Washington”,”London”,”Berlin” and “Paris”,”Islamabad”,”Rome”:

#### Example:

Build a 3-D array using two 2-D arrays, two arrays including three arrays with the data 1, 2, 3, 4, 5 and 2, 4, 6, 8, 10 and 1, 3, 5, 7, 9:

#### Example:

import numpy as npy mrx_arr = npy.array([[[1, 2, 3, 4, 5],[2, 4, 6, 8, 10],[1, 3, 5, 7, 9]],[[1, 2, 3, 4, 5],[2, 4, 6, 8, 10],[1, 3, 5, 7, 9]]]) print(mrx_arr)

## Number of Dimensions

The ndim attribute of NumPy Arrays provides an integer indicating the number of dimensions the array has.

Find out the number of dimensions the array contains:

#### Example:1

import numpy as npy mrx1 = npy.array(33) mrx2 = npy.array([1990,1995,2000,2005,2010,2015,2020,2025]) mrx3 = npy.array([[1, 2, 3, 4, 5],[2, 4, 6, 8, 10],[1, 3, 5, 7, 9]]) mrx4 = npy.array([[[1, 2, 3, 4, 5],[2, 4, 6, 8, 10],[1, 3, 5, 7, 9]],[[1, 2, 3, 4, 5],[2, 4, 6, 8, 10],[1, 3, 5, 7, 9]]]) print(mrx1.ndim) print(mrx2.ndim) print(mrx3.ndim) print(mrx4.ndim)

## Higher Dimensional Arrays

It is important to note that when dealing with numpy arrays, every number of dimensions is possible.

By passing the ndmin argument, the user can set the number of dimensions for the array.

Check that an array has 4 dimensions by making an array with 4 dimensions:

#### Example:2

import numpy as npy year_arr = npy.array([1990,1995,2000,2005,2010,2015,2020,2025], ndmin=4) print(year_arr) print('Number of dimensions is:', year_arr.ndim)

There are eight components in the innermost dimension (4th dim) of this array, One component of the 3rd dimension is the matrix with the vector, There is one component in the 2nd dimension that is a 3D array,

And one component in the 1st dimension that is a 4D array.

## NumPy Array Indexing

Numpy array indexing equates to referencing an array element when we discuss array indexing.

By referring to an array element’s index number, you can locate it.

## Access Array Element

NumPy arrays contain indexes beginning with 0, so the first item has index 0, and the second has index 1, etc.

From the below array, retrieve the first item:

#### Example:1

import numpy as npy odd_arr = npy.array([1, 3, 5, 7, 9]) print(odd_arr[0])

#### Example:2

import numpy as npy even_arr = npy.array([2, 4, 6, 8, 10]) print(even_arr[0])

From the below array, retrieve the third item:

#### Example:1

import numpy as npy odd_arr = npy.array([1, 3, 5, 7, 9]) print(odd_arr[2])

#### Example:2

import numpy as npy even_arr = npy.array([2, 4, 6, 8, 10]) print(even_arr[2])

In the below array, find the fourth and fifth items and add both together:

#### Example:1

import numpy as npy odd_arr = npy.array([1, 3, 5, 7, 9]) print(odd_arr[3] + odd_arr[4])

#### Example:2

import numpy as npy even_arr = npy.array([2, 4, 6, 8, 10]) print(even_arr[3] + even_arr[4])

## Access 2-D Arrays

Numpy array indexing allows us to retrieve data from 2-D arrays by utilizing comma-separated numbers that indicate the dimension and the index.

The 3rd item on the 1st dimension can be located as follows:

#### Example:1

import numpy as npy mrx_arr = npy.array([[2,4,6,8,10], [1,3,5,7,9]]) print('In the first dim the third item is: ', mrx_arr[0, 2])

#### Example:2

import numpy as npy mrx_arr = npy.array([["Major League Soccer","Premier League","La Liga","Serie A","Süper Lig"], ["Bundesliga","Ligue 1","Primeira Liga","Eredivise:","Scottish Premiership"]]) print('In the first dim the third item is: ', mrx_arr[0, 2])

The 5th item on the 2nd dimension can be found as follows:

#### Example:1

import numpy as npy mrx_arr = npy.array([[2,4,6,8,10], [1,3,5,7,9]]) print('In the second dim the fifth item is: ', mrx_arr[1, 4])

#### Example:2

import numpy as npy mrx_arr = npy.array([["Major League Soccer","Premier League","La Liga","Serie A","Süper Lig"], ["Bundesliga","Ligue 1","Primeira Liga","Eredivise:","Scottish Premiership"]]) print('In the second dim the fifth item is: ', mrx_arr[1, 4])

## Access 3-D Arrays

Numpy array indexing allows us to locate items in 3-D arrays through comma-separated integers defining the dimensions and index.

The third item of the second array of the first array can be located as follows:

#### Example:

import numpy as npy mrx_arr = npy.array([[[0, 2, 4], [6, 8, 10]], [[1, 3, 5], [7, 9, 11]]]) print(mrx_arr[0, 1, 2])

Explanation of above example:

mrx_arr[0, 1, 2] displays the digit 10.

Here’s why:

This number indicates the first dimension, which has two arrays:
[[0, 2, 4], [6, 8, 10]]
and:
[[1, 3, 5], [7, 9, 11]]
We now have the first array since we chose 0:
[[0, 2, 4], [6, 8, 10]]

The second number indicates the second dimension, which is also built up of two arrays:
[0, 2, 4]
and:

[6, 8, 10]
Having selected 1, we now have the second array:
[6, 8, 10]

There are three items in the third dimension, which is indicated by the third number:
6
8
10
As an outcome of selecting 2, we end up with the third item:
10

#### Another Example:

import numpy as npy mrx_arr = npy.array([[["New York City", "Los Angeles", "Chicago"], ["Houston", "Phoenix ", "Philadelphia"]], [["San Antonio", "San Antonio", "Dallas "], ["San Jose", "Austin, TX", "Jacksonville, FLA"]]]) print(mrx_arr[0, 1, 2])

Explanation:

mrx_arr[0, 1, 2] displays the word Philadelphia.

Here’s why:

This number indicates the first dimension, which has two arrays:

[[“New York City”, “Los Angeles”, “Chicago”], [“Houston”, “Phoenix “, “Philadelphia”]]

and:

[[“San Antonio”, “San Antonio”, “Dallas “], [“San Jose”, “Austin, TX”, “Jacksonville, FLA”]]

We now have the first array since we chose 0:

[[“New York City”, “Los Angeles”, “Chicago”], [“Houston”, “Phoenix “, “Philadelphia”]]

The second number indicates the second dimension, which is also built up of two arrays:

[“New York City”, “Los Angeles”, “Chicago”]

and:

Having selected 1, we now have the second array:

There are three items in the third dimension, which is indicated by the third number:

“Houston”

“Phoenix “

As an outcome of selecting 2, we end up with the third item:

## Negative Indexing

To retrieve an array from the tail, utilize negative indexing.

From the second dimension, display the second last item:

#### Example:1

import numpy as npy mrx_arr = npy.array([[2,4,6,8,10], [1,3,5,7,9]]) print('From the 2nd dim the 2nd last item is: ', mrx_arr[1, -2])

#### Example:2

import numpy as npy mrx_arr = npy.array([["Major League Soccer","Premier League","La Liga","Serie A","Süper Lig"], ["Bundesliga","Ligue 1","Primeira Liga","Eredivise:","Scottish Premiership"]]) print('From the 2nd dim the 2nd last item is: ', mrx_arr[1, -2])

The next chapter will provide you with more information on numPy data types.

+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0