Create a list of arrays Python

Introduction

527

  • Introduction
  • Using Numpy functions
  • Conversion from Python Lists
  • Using Special Library Functions
  • Conclusion
  • Top

Introduction

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:

  1. Using Numpy functions
  2. Conversion from other Python structures like lists
  3. Using special library functions

Using Numpy functions

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

Creating a One-dimensional Array

First, lets 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.

1import Numpy as np 2array = np.arange[20] 3array
python

Output:

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

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

1array.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.

1array[3]
python

Output:

13

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.

1array[3] = 100 2print[array]
python

Output:

1[ 0 1 2 100 2 4 5 6 7 3 8 9 10 11 4 12 13 14 15 5 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.

1array[3] ='Numpy'
python

Output:

1ValueError: invalid literal for int[] with base 10: 'Numpy'

Creating a Two-dimensional Array

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.

1array = np.arange[20].reshape[4,5] 2array
python

Output:

1array[[[ 0, 1, 2, 3, 4], 2 [ 5, 6, 7, 8, 9], 3 [10, 11, 12, 13, 14], 4 [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.

1array.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.

1array[3][4]
python

Output:

119

Creating a Three-dimensional Array and Beyond

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

1array = np.arange[27].reshape[3,3,3] 2array
python

Output:

1array[[[[ 0, 1, 2], 2 [ 3, 4, 5], 3 [ 6, 7, 8]], 4 5 [[ 9, 10, 11], 6 [12, 13, 14], 7 [15, 16, 17]], 8 9 [[18, 19, 20], 10 [21, 22, 23], 11 [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.

1array.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

1np.arange[10, 35, 3]
python

Output:

1array[[10, 13, 16, 19, 22, 25, 28, 31, 34]]

Using Other Numpy Functions

Other than arange function, you can also use other helpful functions like zerosand 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].

1np.zeros[[2,4]]
python

Output:

1array[[[0., 0., 0., 0.], 2 [0., 0., 0., 0.]]]

Use the ones function to create an array filled with ones.

1np.ones[[3,4]]
python

Output:

1array[[[1., 1., 1., 1.], 2 [1., 1., 1., 1.], 3 [1., 1., 1., 1.]]]

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

1np.empty[[2,3]]
python

Output:

1array[[[0.65670626, 0.52097334, 0.99831087], 2 [0.07280136, 0.4416958 , 0.06185705]]]

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

1np.full[[2,2], 3]
python

Output:

1array[[[3, 3], 2 [3, 3]]]

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

1np.eye[3,3]
python

Output:

1array[[[1., 0., 0.], 2 [0., 1., 0.], 3 [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.

1np.linspace[0, 10, num=4]
python

Output:

1array[[ 0., 3.33333333, 6.66666667, 10.]]

Conversion from Python Lists

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:

1array = np.array[[4,5,6]] 2array
python

Output:

1array[[4, 5, 6]]

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

1list = [4,5,6] 2list
python

Output:

1[4, 5, 6]
1array = np.array[list] 2array
python

Output:

1array[[4, 5, 6]]

You can confirm that both the variables, array and list, are a of type Python list and Numpy array respectively.

1type[list]
python

list

1type[array]
python

Numpy.ndarray

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

1array = np.array[[[1,2,3], [4,5,6]]] 2array
python

Output:

1array[[[1, 2, 3], 2 [4, 5, 6]]]
1array.shape
python

Output:

1[2, 3]

Using Special Library Functions

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.

1np.random.random[[2,2]]
python

Output:

1array[[[0.1632794 , 0.34567049], 2 [0.03463241, 0.70687903]]]

Conclusion

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.

527

Video liên quan

Chủ Đề