Introduction

6

At the heart of a Numpy library is the array object or the ndarray object (n-dimensional array). You will use Numpy arrays to perform logical, statistical, and Fourier transforms. As part of working with Numpy, one of the first things you will do is create Numpy arrays. The main objective of this guide is to inform a data professional, you, about the different tools available to create Numpy arrays.

There are three different ways to create Numpy arrays:

- Using Numpy functions
- Conversion from other Python structures like lists
- Using special library functions

Numpy has built-in functions for creating arrays. We will cover some of them in this guide.

First, let’s create a one-dimensional array or an array with a rank 1. `arange`

is a widely used function to quickly create an array. Passing a value 20 to the `arange`

function creates an array with values ranging from 0 to 19.

`1 2 3`

`import Numpy as np array = np.arange(20) array`

python

Output:

`1 2 3 4`

`array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])`

To verify the dimensionality of this array, use the *shape* property.

`1`

`array.shape`

python

Output:

`1`

`(20,)`

Since there is no value after the comma, this is a one-dimensional array. To access a value in this array, specify a non-negative index. As in other programming languages, the index starts from zero. So to access the fourth element in the array, use the index 3.

`1`

`array[3]`

python

Output:

`1`

`3`

Numpy Arrays are mutable, which means that you can change the value of an element in the array after an array has been initialized. Use the `print`

function to view the contents of the array.

`1 2`

`array[3] = 100 print(array)`

python

Output:

`1 2 3 4 5`

`[ 0 1 2 100 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]`

Unlike Python lists, the contents of a Numpy array are homogenous. So if you try to assign a string value to an element in an array, whose data type is *int*, you will get an error.

`1`

`array[3] ='Numpy'`

python

Output:

`1`

`ValueError: invalid literal for int() with base 10: 'Numpy'`

Let's talk about creating a two-dimensional array. If you only use the `arange`

function, it will output a one-dimensional array. To make it a two-dimensional array, chain its output with the `reshape`

function.

`1 2`

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

python

Output:

`1 2 3 4`

`array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]])`

First, 20 integers will be created and then it will convert the array into a two-dimensional array with 4 rows and 5 columns. Let's check the dimensionality of this array.

`1`

`array.shape`

python

Output:

`1`

`(4, 5)`

Since we get two values, this is a two-dimensional array. To access an element in a two-dimensional array, you need to specify an index for both the row and the column.

`1`

`array[3][4]`

python

Output:

`1`

`19`

To create a three-dimensional array, specify 3 parameters to the reshape function.

`1 2`

`array = np.arange(27).reshape(3,3,3) array`

python

Output:

`1 2 3 4 5 6 7 8 9 10 11`

`array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]])`

Just a word of caution: The number of elements in the array (27) must be the product of its dimensions (3*3*3). To cross-check if it is a three-dimensional array, you can use the shape property.

`1`

`array.shape`

python

Output:

`1`

`(3, 3, 3)`

Also, using the `arange`

function, you can create an array with a particular sequence between a defined start and end values

`1`

`np.arange(10, 35, 3)`

python

Output:

`1`

`array([10, 13, 16, 19, 22, 25, 28, 31, 34])`

Other than `arange`

function, you can also use other helpful functions like `zeros`

and `ones`

to quickly create and populate an array.

Use the `zeros`

function to create an array filled with zeros. The parameters to the function represent the number of rows and columns (or its dimensions).

`1`

`np.zeros((2,4))`

python

Output:

`1 2`

`array([[0., 0., 0., 0.], [0., 0., 0., 0.]])`

Use the `ones`

function to create an array filled with ones.

`1`

`np.ones((3,4))`

python

Output:

`1 2 3`

`array([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]])`

The `empty`

function creates an array. Its initial content is random and depends on the state of the memory.

`1`

`np.empty((2,3))`

python

Output:

`1 2`

`array([[0.65670626, 0.52097334, 0.99831087], [0.07280136, 0.4416958 , 0.06185705]])`

The `full`

function creates a n * n array filled with the given value.

`1`

`np.full((2,2), 3)`

python

Output:

`1 2`

`array([[3, 3], [3, 3]])`

The `eye`

function lets you create a n * n matrix with the diagonal 1s and the others 0.

`1`

`np.eye(3,3)`

python

Output:

`1 2 3`

`array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])`

The function `linspace`

returns evenly spaced numbers over a specified interval. For example, the below function returns four equally spaced numbers between the interval 0 and 10.

`1`

`np.linspace(0, 10, num=4)`

python

Output:

`1`

`array([ 0., 3.33333333, 6.66666667, 10.])`

Other than using Numpy functions, you can also create an array directly from a Python list.
Pass a Python list to the `array`

function to create a Numpy array:

`1 2`

`array = np.array([4,5,6]) array`

python

Output:

`1`

`array([4, 5, 6])`

You can also create a Python list and pass its variable name to create a Numpy array.

`1 2`

`list = [4,5,6] list`

python

Output:

`1`

`[4, 5, 6]`

`1 2`

`array = np.array(list) array`

python

Output:

`1`

`array([4, 5, 6])`

You can confirm that both the variables, `array`

and `list`

, are a of type *Python list* and *Numpy array* respectively.

`1`

`type(list)`

python

list

`1`

`type(array)`

python

Numpy.ndarray

To create a two-dimensional array, pass a sequence of lists to the array function.

`1 2`

`array = np.array([(1,2,3), (4,5,6)]) array`

python

Output:

`1 2`

`array([[1, 2, 3], [4, 5, 6]])`

`1`

`array.shape`

python

Output:

`1`

`(2, 3)`

You can also use special library functions to create arrays. For example, to create an array filled with random values between 0 and 1, use `random`

function. This is particularly useful for problems where you need a random state to get started.

`1`

`np.random.random((2,2))`

python

Output:

`1 2`

`array([[0.1632794 , 0.34567049], [0.03463241, 0.70687903]])`

Creating and populating a Numpy array is the first step to using Numpy to perform fast numeric array computations. Armed with different tools for creating arrays, you are now well set to perform basic array operations.

6

Test your skills. Learn something new. Get help. Repeat.

Start a FREE 10-day trial