CREATE AN ARRAY IN NUMPY

In this tutorial, we are going to learn about how to create an array in numpy using different methods and techniques followed by handy examples.





Now that we have learned about ndarray object in numpy, it’s time to learn about how to create an array in numpy using different methods. We will use arrays to perform different functions such as logical, arithmetic, statistical etc. So, let’s get started with creating arrays. There are numerous ways to create arrays in numpy such as:

Creating Lists with Different Dimensions

Numpy has a built-in function which is known as arange, it is used to generate numbers within a range if the shape of an array is predefined.

Creating a Single Dimensional Array

Let’s create a single dimension array having no columns but just one row. This type of array is usually called a rank 1 array because it has only one axis (one dimensional array). Similarly, an array with rank 2 will be a 2D array because it will have 2 axis (rows x columns). Let’s pass on a value of 10 in the arange function which will generate values from 0 to 9 (index wise).

import numpy as np
#Single dimensional array
one_d = np.arange(10)
one_d

Output:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

To verify the dimensions of the shape of an array, you can use the shape function:

one_d.shape

Output:

(10,)

You can see that there is no value after 10. This means that it is a single dimensional array with having 10 indices. You can update different values in your array by assigning new ones. For example, if you want to change the  value at index 5, then you can simply do as follows:

one_d[5] = 30
one_d

Output:

array([ 0,  1, 2, 3, 4, 30,  6, 7, 8, 9])

But you have to be careful when updating values in your numpy, for example you cannot update strings in your numpy array which has a datatype int. Since, arrays should of the same type at a time so if you update a string value to an integer datatype, then there is going to be an error.

one_d[6] = "Numbers"
one_d

Output:

ValueError           Traceback (most recent call last)
in 
----> 1 one_d[6] = "Numbers"

2 one_d
ValueError: invalid literal for int() with base 10: 'Numbers'





Creating a 2D Array

Let’s create a 2D array now. We have learnt about using the arange function as it outputs a single dimensional array. To create a 2D array we will link the reshape function with the arange function.

import numpy as np
two_d = np.arange(30).reshape(5,6)
two_d

Output:

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, 27, 28, 29]])

You can always check the shape of the array by using the shape function:

two_d.shape

Output:

(5, 6)

You can access the values in a two dimensional array by specifying the index value of the row as the column:

two_d[3][1]

Output:

19

Make sure that the product of the reshape numbers should be equal to the number defined in arange function.

Creating a 3D Array

For creating a 3D array, we can specify 3 axises to the reshape function like we did in 2D array. To create a three-dimensional array, specify 3 parameters to the reshape function.

three_d = np.arange(8).reshape(2,2,2)
three_d 

Output:

array([[[0, 1],
        [2, 3]],

       [[4, 5],
        [6, 7]]])

Make sure that the product of the reshape numbers should be equal to the number defined in arange function.





Through Numpy Functions

We have used the arange function now we will use the zeroes, ones along with other built in functions that we can use for numpy array creation.

Using Zeros Function

We can use the zeros function for creating an array representing only zeroes. We can identify the number of rows and columns as parameters while declaring the zeros function:

zeros = np.zeros((3,4))
zeros

Output:

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

Using Ones Function

We can use the ones function for creating an array representing only ones. We can identify the number of rows and columns as parameters while declaring the ones function:

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

Output:

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

Note: The default datatype for zeros and ones is ‘float’

Using Empty Function

Empty function in numpy creates an array which is based on random numbers and depends on the state of the memory. The empty function creates an array. Its initial content is random and depends on the state of the memory.

empty = np.empty((2,4))
empty

Output:

array([[-1.72723371e-077, 2.00389442e+000, 9.76118064e-313,
2.20687562e-312],
[ 2.37663529e-312, 4.99006302e-322, 0.00000000e+000,
5.56268465e-309]])




Using Full Function

Returns a new array with a shape of rows and columns followed by the only value that you want to see in your array.

full = np.full((3,4),3)
full

Output:

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

Using the Eye Function

The Eye function creates an array in numpy that is positioned with a diagonal design of 1s and 0s.

eye = np.eye(4,4)
eye

Output:

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

Using Linspace Function

The linspace function creates an array in which numbers are represented at a defined interval. For example, if I want to represent the numbers between 1 and 10 for 20 times, I can write it as:

linspace = np.linspace(1, 10, 20)
linspace

Output:

array([ 1. , 1.47368421, 1.94736842, 2.42105263, 2.89473684,
3.36842105, 3.84210526, 4.31578947, 4.78947368, 5.26315789,
5.73684211, 6.21052632, 6.68421053, 7.15789474, 7.63157895,
8.10526316, 8.57894737, 9.05263158, 9.52631579, 10. ])

Extracting Lists as Numpy Arrays

We can also create an array in numpy by using lists, we can separately create lists and add them to an array

list = [1,2,3,4,5]
list

Output:

[1, 2, 3, 4, 5]

Now let’s create an array and insert the existing list in it.

list_array = np.array(list)
list_array

Output:

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

Using the Random Function

We can special functions in numpy as well to create arrays, one of them is the random function. Let’s learn about how to create an array holding random values:

rand = np.random.rand(3,5)
rand

Output:

array([[0.09361231, 0.79701563],
[0.9774606 , 0.87040235],
[0.79645207, 0.34890012]])

Creating arrays in numpy is the most crucial part of working in numpy. This phase trains us to create arrays before starting any sort of computations on them. Hence it is the most important one as well.