First import module requires the use of a renamed np
import numpy as np
1, use the list directly into an array
arr = np.array([1,2,3,4])
Use array () method of directly into an array of fissile
2, to create an array using arange
arr = np.arange(0, 5, 1)
Use aRange () method for creating an array, the parameter 1 is the initial position, parameter 2 as the end position parameter . 3 jump to step (refer to for loop Range () )
3, to generate a arithmetic array
arr = np.linspace(0,5,5)
Use linspqce () to generate a arithmetic array, the parameter 1 : the starting position, parameter 2 : end position, parameter 3 : the number of elements in the array, Endpoint : is True comprising the end position, is False not the ending position comprising
4, create a geometric array
arr = np.logspace(0, 2, 3)
Use LOGSPACE () generates a geometric array, the parameter 1 : the starting position, parameter 2 : end position, parameter 3 : the number of elements in the array, Endpoint : is True when the end position comprising, as False not contain an end position, Base : base-generated geometric series
5, create an element of 0, array
arr = np.zeros(shape=(2,3))
Use zeros () method creates all the elements of a 0 of the array, the parameter shape = (2,3) in the shape of the array, here two rows . 3 column
6, create a element in an array of 1
arr = np.ones(shape=(2,3))
Use ones () method creates all elements of a one of the array, the parameter shape = (2,3) in the shape of the array, here two rows . 3 column
7, to create a similar array diagonal matrix
arr = np.diag (v = [1, 2, 3, 4], k = 1)
Use diag () to create a similar diagonal matrix array, the parameter v is the diagonal elements of the array, K> 0 upward (right) the offset value, k <0 downward (left) of the offset value
8, create a similar array of matrix
arr = np.eye(N=2,M=2,k=0)
Use Eye () method of creating a matrix array of similar units, the parameter N is the number of rows, the array parameter M is the number of columns of the array, K> 0 upward (right) the offset value, K <0 downward (left) offset value
9, creates a random array
arr = np.random.random(size=10)
arr = np.random.random(size=(2, 3))
Use random.random () method to create in [0, 1) interval random array parameter size number of elements in the array and specified shapes
10, subject to generate an array of uniformly distributed random
arr =np.random.rand(10)
arr = np.random.rand(2, 3)
Use random.rand () method to create in [0, 1) random array section subject to uniform distribution, the shape parameter specifies the number of elements of the array, and
11, generates a random array obey a normal distribution
arr = np.random.randn(10)
arr = np.random.randn(2,3)
Use random.randn () method to create in [0, 1) random array section subject to uniform distribution, the shape parameter specifies the number of elements of the array, and
12, creates an array of random integers in the interval
arr = np.random.randint(low=1,high=5,size=10)
arr = np.random.randint(low=1,high=5,size=(2,3))
Using the random.randint () to create an array of integer random method, parameters low and high range of the specified array, size number of elements in the array and specified shapes
13, creates a random decimal array within a certain range
arr = np.random.uniform(size=10) arr = np.random.uniform(size=(2,3)) arr = np.random.uniform(low=2,high=3,size=(2,3))
Use random.uniform () method to create a random interval within a decimal array parameters low and high range of the specified array, size number of elements in the array and specified shapes