NumPy multidimensional array, several functions to create the array

To create an array object:

You can create an array of ndarrays through the array function of the NumPy library. Generally speaking, ndarray is a common data container, that is, all elements in it need the same type. NumPy library can convert data (list, tuple, array or other sequence types) into ndarray array

1. Create an array object using array

array function format:

`np.array(object,dtype,ndmin)`
 parameter explain object Receive array, indicating the array you want to create dtype Receive data type, indicating the data type required by the array. If it is a given time, it is None by default ndamin Receive int and specify the minimum dimension that the generated array should have. The default is None

Create an array of ndarray s:

```import numpy as np
data1 = [1,3,5,7]    #list
w1 = np.array(data)

data2 = (1,3,5,7)    #tuple
w2 = np.array(data2)

data3 = [[1,2,3,4],[5,6,7,8]]
w3 = np.array(data3)
```

When creating an array, NumPy will infer an appropriate data type for the newly created array and save it in dtype. When there are integers and floating-point numbers in the sequence, NumPy will define the dtype of the array as the floating-point data type

Specify dtype in array:

```import numpy as np
w3 = np.array([1,2,3,4],dtype='float64')
print(w3.dtype)

#Output results
#float64
```

2. Functions for creating arrays:

Using existing Python sequences to create arrays through the array function is inefficient. Therefore, NumPy provides many functions for creating arrays

1) Lagrange function

The range function is similar to Python's built-in function range, but range is mainly used to create arrays

```import numpy as np
warray = np.arange(10)
print(warray)```

Out:[0 1 2 3 4 5 6 7 8 9]

Specify the range of start value, end value and step size parameters

```import numpy as np
warray = np.arange(0,1,0.2)
print(warray)```

Out:[0,0.2,0.4,0.6,0.8]

2)linspace function

When the parameter of the range is a floating-point type, it is usually impossible to predict the number of elements due to the limited precision of the floating-point type. For this reason, a better function linspace is usually selected, which receives the number of elements as the parameter. The linspace function creates a one-dimensional array by specifying the start value, end value and number of elements, including the end value by default

```import numpy as np
warray = np.linspace(0,1,5)
print(warray)```

Out:[0. 0.25 0.5 0.75 1.]

3)logspace function:

The logspace function is similar to the linspace function. The difference is that logspace creates an isometric sequence

```import numpy as np
warray = np.logspace(0,1,5)#Generate an equal ratio sequence of 1 ~ 10 with 5 elements
print(warray)```

Out:[ 1.          1.77827941  3.16227766  5.62341325 10.        ]

In the parameter of logspace, the start bit and end bit represent the power of 10 (the default cardinality is 10), and the third element represents the number of elements

4) zeros function:

Creates an all 0 array of the specified length or shape

All zero matrix;

```import numpy as np
warray = np.zeros(4)
print(warray)
print(np.zeros([4,4]))```

Out:[0. 0. 0. 0.]

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

5)ones function:

Creates a full 1 array of the specified length or shape

```import numpy as np
warray = np.ones(4)
print(warray)
print(np.ones([4,4]))```

Out:[1. 1. 1. 1.]

[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]

6)dig function

Create a diagonal matrix, that is, the diagonal element is 0 or the specified value, and the other elements are 0

```import numpy as np

print(np.diag([1,2,3,4]))```

Out:[[1 0 0 0]
[0 2 0 0]
[0 0 3 0]
[0 0 0 4]]

7)eye function:

Diagonal position is 1, other positions are 0

```import numpy as np

print(np.eye(3))```

Out: [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

8) mat function

```import numpy as np

print(np.mat("1,2,3,4,5,6,7,8,9"))

print(np.mat("1,2,3;4,5,6;7,8,9"))#three × Matrix of 3```

Out: [[1 2 3 4 5 6 7 8 9]]
[[1 2 3]
[4 5 6]
[7 8 9]]

9)np.random.random

```import numpy as np

print(np.random.random([3,3]))

print(np.random.random([3,3,2]))#The first parameter is number 3, which outputs three arrays of three rows and two columns```

Keywords: Python sklearn

Added by Cetanu on Tue, 21 Dec 2021 20:26:56 +0200