Linux Format

Create multi-dimensiona­l arrays

-

If a two-dimensiona­l array can be thought of as a spreadshee­t, then a 3D array is akin to a cube. Numpy is perfect for creating these types of N-Dimensiona­l arrays through using ndarray. True to its name there’s no theoretica­l limit on the number of dimensions your array can have but for the time being, imagine that you want to create a virtual blackjack game which uses five decks of cards. If you followed the steps above you may have already created an array with a shape of 4,13. In other words, each card can be located based on suit and number. To get started with creating a 3D array to hold data on whether a deck card has been played, you can

use some of the NumPy Array creation routines. For instance, run: import numpy as np decks = np.zeros((5,4,13), dtype=np.int)

This will create a new array with all elements set to zero. The dtype determines the kind of data to be stored in the array. By default, this is float64. If you prefer to add 1 instead, substitute zeroes with ones. You can also create an array filled with values you’ve specified. See https:// docs.scipy.org/doc/numpy/user/basics.

creation.html for a full list of array creation routines. Once your multidimen­sional array has been created, you can manipulate it in the same way to the previous Numpy arrays you’ve created for example, decks[1,3,10] = 99 .

 ??  ?? Use numpy.zeros and you, too, can have your very own multidimen­sional array.
Use numpy.zeros and you, too, can have your very own multidimen­sional array.

Newspapers in English

Newspapers from Australia