This is the 22nd day of my participation in the August More Text Challenge

Numpy is a scientific computing package for multi-dimensional arrays (NDARray) that encapsulates multiple methods that can be used for inter-array computation.

An array is a combination of elements of the same type in a certain order. The elements in an array must have the same data type

1 Generates a generic array

1.1 Generation of one-dimensional arrays

1.1.1 Generate a one-dimensional array as a list

import numpy as np
​
arr = np.array([2, 4, 6, 8])
print(arr)
Copy the code

result:

[2 4 6 8]
Copy the code

1.1.2 Generate a one-dimensional array as a tuple

import numpy as np
​
arr = np.array((2, 4, 6, 8))
print(arr)
Copy the code

result:

[2 4 6 8]
Copy the code

1.2 Generation of multidimensional arrays

1.2.1 Generate a one-dimensional array as a list

import numpy as np
​
arr = np.array([[1, 2, 3, 4], [2, 4, 6, 8]])
print(arr)
Copy the code

result:

[1 2 3 4] [2 4 6 8]]Copy the code

1.2.2 Generating one-dimensional arrays as tuples

import numpy as np
​
arr = np.array(((1, 2, 3, 4), (2, 4, 6, 8)))
print(arr)
Copy the code

result:

[1 2 3 4] [2 4 6 8]]Copy the code

2 Generate an array of a special type

2.1 Generate a fixed range of random numbers

The method used to generate a fixed range of random numbers is arange()

range(start,stop,step)
Copy the code

The above code generates a random sequence starting with start (including start) and ending with stop (excluding stop), with step as the step size.

Parameter Description:

Start: The start position. The generated sequence contains start, which defaults to 0

Stop: The end position, the generated sequence does not contain stop

Step: indicates the step. The default value is 1

import numpy as np
​
arr = np.arange(1, 15, 3)
print(arr)
Copy the code

result:

[1 4 7 10 13]Copy the code

The above represents generating a sequence of 3 intervals from 1 to 15

2.2 Generate an array of all zeros

The method used to generate an array of all zeros is zeros()

2.2.1 Generate a one-dimensional array with all zeros

import numpy as np
​
arr = np.zeros(3)
print(arr)
Copy the code

result:

[0. 0 0.]Copy the code

2.2.2 Generating a multidimensional array with all zeros

import numpy as np
​
arr = np.zeros((2, 3))
print(arr)
Copy the code

result:

[0. 0.] [0. 0.]Copy the code

2.3 Generate an array of all 1 shapes

The method used to generate an array of all ones is ones()

2.3.1 Generating a one-dimensional array of all 1s

import numpy as np
​
arr = np.ones(3)
print(arr)
Copy the code

result:

[1. 1. 1.]
Copy the code

2.3.2 Generating a multidimensional array of all 1s

import numpy as np
​
arr = np.ones((2, 3))
print(arr)
Copy the code

result:

[1. 1.] [1. 1.]Copy the code

2.4 Generating the identity matrix

import numpy as np
​
arr = np.eye(3)
print(arr)
Copy the code

result:

[0. 0.] [0. 0.] [0.Copy the code

3 Generate a random number group

3.1 Generate a random number group between 0 and 1

3.1.1 Generate a random one-dimensional array between 0 and 1

import numpy as np
​
print(np.random.rand(3))
Copy the code

result:

[0.75829412, 0.84077953, 0.84004982]Copy the code

3.1.2 Generate random multidimensional arrays between 0 and 1

import numpy as np
​
print(np.random.rand(2, 3))
Copy the code

result:

[[0.18621174 0.88523494 0.23696019]
 [0.48601592 0.96503436 0.17714081]]
Copy the code

Note the difference between creating a multi-dimensional array and passing multiple arguments without using tuples or lists

3.2 Generate arrays that satisfy normal distribution

3.2.1 Generate a one-dimensional array satisfying normal distribution

import numpy as np
​
print(np.random.randn(2))
Copy the code

result:

[0.54451739 0.70519053]Copy the code

3.2.2 Generate multidimensional arrays that meet normal distribution

import numpy as np
​
print(np.random.randn(2, 3))
Copy the code

result:

[[-1.09094899 0.29661067-0.9772994] [-0.66585228 0.91998882 0.06880275]]Copy the code

3.3 Generate random number groups within a certain range

The method used to generate a random set of numbers within a fixed range is randint()

randint(low,high=None,size=None)
Copy the code

The above code shows that the array is generated in [low, high], whose size is size, and the integer values in the array are evenly distributed

If high is null, the interval becomes [0, low].

3.3.1 Generate a random one-dimensional array within a certain range

Generates a one-dimensional array of length 10 between 0 and 5

import numpy as np
​
print(np.random.randint(5, size=10))
Copy the code

result:

[4 4 2 1 2 1 2 4 1 2]
Copy the code

3.3.2 Generate a random multidimensional array within a certain range

import numpy as np
​
print(np.random.randint(5, size=(2, 3)))
Copy the code

result:

[3 3 2] [3 1 0]]Copy the code

3.4 Randomly select an array of the appropriate size from the known array

The method used to generate a fixed range of random numbers is choice()

np.random.choice(a, size=None, replace=None, p=None)
Copy the code

The above code selects the size array from array A as a new array.

A can be an array or an integer.

A is an array that means sampling randomly from that array.

When a is an integer, it samples from range(a)

3.4.1 Randomly select a one-dimensional array of the corresponding size from the known array

import numpy as np

print(np.random.choice(5, 3))
Copy the code

result:

[0 2]Copy the code

3.4.2 Randomly select multidimensional arrays of corresponding sizes from known arrays

import numpy as np

print(np.random.choice(5, (2, 3)))
Copy the code

result:

[1 4 0] [0 4 4]]Copy the code

3.5 Array Scrambling

import numpy as np

arr = np.arange(10)
print(arr)
Copy the code

result:

[0 1 2 3 4 5 6 7 8 9]
Copy the code
np.random.shuffle(arr)
print(arr)
Copy the code

result:

[2 4 5 7 0 8 6 1 3 9]
Copy the code

Note: The results of each run are different and are only for reference