NumPy Fast Food Tutorial (1) – How to generate a multidimensional array

Python is now the hottest AI language, and support for various tools, such as Google’s Tensorflow, is preferred. However, unlike R, Python was not designed with specific support for matrix operations, statistical calculations and other functions in mind. So we need NumPy libraries to make up for this lack of capability. NumPy is a well-known extension library for Python, the equivalent of MATLAB in Python.

How do I generate a multidimensional array

Introduction to nDARray multidimensional arrays

The most common thing we use in algorithms is matrices, so let’s start with matrices. In NumPy, a two-dimensional multidimensional array called NDARray is used to store matrices.

Ex. :

A3 = np. Array ([[1, 0], [0, 1]])Copy the code

Generates a multidimensional array object like this

array([[1, 0],
       [0, 1]])Copy the code

Generating array sequences

Generates the array sequence -arange from the start value, end value, and step value

You can use the arange function to generate a one-dimensional array specifying the start, end, and step values. Note that the end value is not included in the sequence, which means that the end value is the open interval.

In [25] : a4 = np arange,10,1 (1) In [26] : a4 Out [26] : array ([1, 2, 3, 4, 5, 6, 7, 8, 9])Copy the code

Linear sequence – Linspace

Like Arange, Linspace generates sequences by giving initial values, final values, and the number of elements. Whether or not the final value is included can be set through the endpoint property.

Ex. :

. [37] : In a8 = np linspace (1,10,10, endpoint = True) In [38] : a8 Out [38] : array ([1, 2, 3., 4, 5, 6, 7, 8, 9, 10.])Copy the code

Geometric sequence – logspace

In addition to linear arithmetic sequences, we can also generate one-dimensional arrays by arithmetic sequences. The default is 10 to the NTH power. For example, logspace(0,4,3) means that the initial value is 10 to the NTH power, which is 1, and the final value is 10 to the fourth power, which is 100, generating three values.

Example, generate [1,100,10000]

In [47]: a9 = np.logspace(0,4,3) In [48]: a9 Out[48]: array([1.0000e +00, 1.0000e +02, 1.00000000e+04])Copy the code

We can also change the cardinality, for example, to 3:

[53] : In a10 = np. Logspace (1,5,3, base = 3) [54] : In a10 Out [54] : array ([3., 27, 243.])Copy the code

Change the shape of a multidimensional array

If you want to convert a one-dimensional array to a multi-dimensional array, you can do so by modifying the Shape property.

We can store the data in a one-dimensional array first. We can use lists or tuples to generate one-dimensional arrays, which are equivalent:

In [2] : a1 = np array ([1, 2, 3, 4]] In [3] : a1 Out [3] : array ([1, 2, 3, 4]] In [4] : a2 = np, array (,0,0,1) (1) In [5]. a2 Out[5]: array([1, 0, 0, 1])Copy the code

We use the shape property to view the shape of an array:

In [14]: a1.shape
Out[14]: (4,)

In [15]: a2.shape
Out[15]: (4,)Copy the code

The shape property can be changed directly. For example, if we want to change a1 to a 2 x 2 matrix, we can change the shape value directly:

In [16] : a1. Shape = 2, 2 In [17] : a1 Out [17] : array ([[1, 2], [3, 4]])Copy the code

If we can determine one axis, we can assign -1 to the other axis and let the system do it. Ex. :

In [18]: a2.shape= 2,-1

In [19]: a2
Out[19]: 
array([[1, 0],
       [0, 1]])Copy the code

If you want to keep this array the same and generate a new array that changes shape, you can call the 0 method. Example: We take an array of 25 elements and make a new array of 5×5 elements

In [59]: a11 = np. Linspace (1,100,25) In [60]: a11 Out[60]: Array ([1., 5.125, 9.25, 13.375, 17.5, 21.625, 25.75, 29.875, 34., 38.125, 42.25, 46.375, 50.5, 54.625, 58.75, 62.875, 67., 71.125, 75.25, 79.375, 83.5, 87.625, 91.75, 95.875, 100.]) In [61]: 00 a12 Out[62]: Array ([1., 5.125, 9.25, 13.375, 17.5], [21.625, 25.75, 29.875, 34., 38.125], [42.25, 46.375, 50.5, 54.625, 58.75], [62.875, 67, 71.125, 75.25, 79.375], [83.5, 87.625, 91.75, 95.875, 100]])Copy the code

Generate multidimensional arrays directly

Generates an array of all zeros

Zeros generates an array of zeros with shape as the first argument

Ex. :

In [65] : np. Zeros ((10, 10)) Out [65] : array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., .. 0, 0, 0., 0.], [... 0, 0, 0, 0), and 0., 0., 0., 0., 0., 0.], [... 0, 0, 0, 0), and 0., 0., 0., 0., 0., 0.], [... 0, 0, 0, .. 0, 0, 0.,. 0, 0. 0. 0.], [... 0, 0, 0, 0), and 0., 0., 0., 0., 0., 0.], [... 0, 0, 0, 0), and 0., 0., 0., 0., 0., 0.]. .. [0, 0, 0, 0), and 0., 0., 0., 0., 0., 0.], [... 0, 0, 0, 0), and 0., 0., 0., 0., 0., 0.]])Copy the code

Generates an array of all ones

Ex. :

In [66] : np. Catalog. ((5, 5)) Out [66] : array([[ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]])Copy the code

Only empty arrays are generated

Empty does not assign an initial value and is the fastest method

Ex. :

In [67]: np.empty((3,3))
Out[67]: 
array([[  1.   ,   2.125,   3.25 ],
       [  4.375,   5.5  ,   6.625],
       [  7.75 ,   8.875,  10.   ]])Copy the code

Function to generate an array

The fromFunction function lets you generate the desired array from a function.

For example, to generate the multiplication table:

In [125]: def mul2(x,y): ... : return (x+1)*(y+1) ... : In [126]: np. Fromfunction (mul2,(9,9)) Out[126]: array([[ 1., 2., 3., 4., 5., 6., 7., 8., 9.], [ 2., 4., 6., 8., 10., 12., 14., 16., 18.], [ 3., 6., 9., 12., 15., 18., . 21., 24, 27], [4, 8, 12, 16, and 20, and 24., 28, 32., 36], [5. 10., 15, 20., 25, and 30., 35, the 40, 45.], [6, 12. . 18., 24, 30, 36, 42., 48., 54.], [7, 14, 21, and 28, 35, 42), 49, and 56., 63.], [. 8, 16, 24, 32, and the 40, 48., 56., 64, 72], [9., 18., 27, 36, 45, and 54., 63, 72, 81]])Copy the code