NumPy Fast Food Tutorial (1) - How to Generate Multidimensional Arrays

NumPy Fast Food Tutorial (1) - How to Generate Multidimensional Arrays

Python is now the most popular artificial intelligence language, and the support of various tools, such as Google's Tensorflow, is the first choice to support Python.
However, unlike the R language, the Python language was designed without special support for matrix operations, statistical calculations and other functions. So we need the NumPy library to make up for this lack of capability.
NumPy is a well-known extension library for Python, equivalent to MATLAB in Python.

How to generate a multidimensional array

First acquaintance with ndarray multidimensional array

In the algorithm we most often use the matrix, let's start with the matrix.
In NumPy, a two-dimensional multidimensional array ndarray is used to store matrices.

example:

a3 = np.array([[1,0],[0,1]])

will generate such a multidimensional array object

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

generate array sequence

Generate array sequence from start value, end value and step value - arange

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

In [25]: a4 = np.arange(1,10,1)

In [26]: a4
Out[26]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])

Linear sequence - linspace

Similar to arange, linspace generates a sequence by giving an initial value, a final value, and the number of elements. Whether to include the final value can be set through the endpoint property.

example:

In [37]: a8 = np.linspace(1,10,10,endpoint=True)

In [38]: a8
Out[38]: array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])

Equal Sequence - logspace

In addition to linear arithmetic progressions, we can also generate one-dimensional arrays by means of arithmetic progressions.
The default is to use the nth power of 10 as the parameter. For example, logspace(0,4,3) means that the initial value is 10 to the 0th power, which is 1, and the final value is 10 to the 4th power, which is 100. A total of 3 values.

For example, generate [1,100,10000]

In [47]: a9 = np.logspace(0,4,3)

In [48]: a9
Out[48]: array([  1.00000000e+00,   1.00000000e+02,   1.00000000e+04])

Of course, we can also modify the base, such as changing to 3:

In [53]: a10 = np.logspace(1,5,3,base=3)

In [54]: a10
Out[54]: array([   3.,   27.,  243.])

Change the shape of a multidimensional array

If there is a one-dimensional array to be converted into a multi-dimensional array, it can be achieved by modifying the shape attribute.

We can store the data in a one-dimensional array first, and can use a list or a tuple to generate a one-dimensional array, they are equivalent:
for example:

In [2]: a1 = np.array([1,2,3,4])

In [3]: a1
Out[3]: array([1, 2, 3, 4])

In [4]: a2 = np.array((1,0,0,1))

In [5]: a2
Out[5]: array([1, 0, 0, 1])

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,)

The shape attribute can be modified directly. For example, if we want to change the above a1 to a 2 x 2 matrix, just change the shape value directly:

In [16]: a1.shape = 2,2

In [17]: a1
Out[17]: 
array([[1, 2],
       [3, 4]])

If one axis can be determined, the other can be assigned -1 for the system to calculate by itself.
example:

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

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

If you want to keep the array unchanged and generate a new array with a changed shape, you can call the reshape method.
Example: We will generate a new array of 5x5 from an array of 25 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]: a12 = a11.reshape(5,-1)

In [62]: 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.   ]])

Generate multidimensional arrays directly

Generate an array of all 0s

zeros generates an array of all 0s, and the first parameter is shape

example:

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.,  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.,  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.]])

generate an array of all 1s

example:

In [66]: np.ones((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.]])

only generates empty arrays

empty does not assign an initial value, it is the fastest method

example:

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.   ]])

Generate an array with a function

Through the fromfunction function, you can pass a function to generate the desired array.

For example, to generate a nine-nine 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.,  20.,  24.,  28.,  32.,  36.],
       [  5.,  10.,  15.,  20.,  25.,  30.,  35.,  40.,  45.],
       [  6.,  12.,  18.,  24.,  30.,  36.,  42.,  48.,  54.],
       [  7.,  14.,  21.,  28.,  35.,  42.,  49.,  56.,  63.],
       [  8.,  16.,  24.,  32.,  40.,  48.,  56.,  64.,  72.],
       [  9.,  18.,  27.,  36.,  45.,  54.,  63.,  72.,  81.]])

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325696844&siteId=291194637