The following is to share a Python numpy library to convert the matrix into a list of functions such as the method, has a good reference value, I hope to be helpful to everyone. Come and see it together.
This article focuses on some of the functions in Python's numpy library and makes backups to find them.
(1) A function to convert a matrix to a list: Numpy.matrix.tolist ()
Back to List
Examples
>>>
>>> x = Np.matrix (Np.arange) reshape ((3,4))); Xmatrix ([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, ten, one]]) >>> x.tolist () [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
(2) function to convert an array to a list: Numpy.ndarray.tolist ()
Notes: (arrays can be reconstructed)
The array may be recreated, A=np.array (A.tolist ()).
Examples
>>>
>>> a = Np.array ([1, 2]) >>> a.tolist () [1, 2]>>> a = Np.array ([[1, 2], [3, 4]]) >>> list (a) [Array ([1, 2]), Array ([3, 4])]>>> a.tolist () [[1, 2], [3, 4]]
(3) Numpy.mean () computes the mean of the Matrix or array:
Examples
>>>
>>> a = Np.array ([[1, 2], [3, 4]]) #对所有元素求均值 >>> Np.mean (a) 2.5>>> Np.mean (A, axis=0) #对每一列求均值ar Ray ([2., 3.]) >>> Np.mean (A, Axis=1) #对每一行求均值array ([1.5, 3.5])
(4) NUMPY.STD () calculates the standard deviation of a matrix or array:
Examples
>>>
>>> a = Np.array ([[1, 2], [3, 4]]) #对所有元素求标准差 >>> np.std (a) 1.1180339887498949>>> np.std (A, Axi s=0) #对每一列求标准差array ([1., 1.]) >>> np.std (A, Axis=1) #对每一行求标准差array ([0.5, 0.5])
(5) Numpy.newaxis adds a dimension to an array:
Examples:
>>> A=np.array ([[1,2,3],[4,5,6],[7,8,9]]) #先输入3行2列的数组a >>> B=a[:,:2] >>> B.shape # When the rows and columns of the array are greater than 1 o'clock, do not add dimensions (3, 2) >>> c=a[:,2] >>> c.shape #可以看到, when the array has only one column, the dimension of the missing column (3,) >>> CArray ([3, 6, 9])
>>> D=a[:,2,np.newaxis] #np. Newaxis implements the dimension of adding columns >>> darray ([[3], [6], [9]]) >>> D.shape #d的维度成了3行1列 (3,1) (3, 1) >>> E=a[:,2,none] #None与np. Newaxis implements the same functionality >>> Earray ([[3], [6], [9]]) >>> E.shape (3, 1)
(6) Numpy.random.shuffle (index): disrupts the Order of Datasets (arrays):
Examples:
>>> index = [I for I in Range] >>> index [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> Np.random.shuff Le (index) >>> Index [7, 9, 3, 0, 4, 1, 5, 2, 8, 6]
(7) Calculates the maximum minimum value for a row or column of a two-dimensional array:
>>> import NumPy as np >>> a = np.arange (0) reshape (5,3) #构造一个5行3列的二维数组 >>> a array ], [3, 4, 5], [6, 7, 8], [ 9, ten, one], [, +]]) >>> B = a[:,0].min () # #取第0列的最小值, other columns ;>> b 0 >>> C = A[0,:].max () # #取第0行的最大值, other lines similarly >>> C 2
(8) Adding a column to the array: Np.hstack ()
n = Np.array (Np.random.randn (4,2)) n out[153]: Array ([[0.17234, -0.01480043], [-0.33356669,-1.33565616], [ -1.11680009, 0.64230761], [ -0.51233174, -0.10359941]]) L = Np.array ([1,2,3,4]) l out[155]: Array ([1, 2, 3, 4]) L.shape out[156]: (4,)
As you can see, N is a two-dimensional, l is one-dimensional, and if you call Np.hstack () directly, an error occurs: The dimensions are different.
n = Np.hstack ((n,l)) Valueerror:all the input arrays must has same number of dimensions
The workaround is to change L to two-dimensional, using the method in (5):
n = Np.hstack ((n,l[:,np.newaxis)) # #注意: You must enclose the variable in () when using Np.hstack () because it accepts only one variable n out[161]: Array ([[0.17234,- 0.01480043, 1. ], [ -0.33356669, -1.33565616, 2. ], [ -1.11680009, 0.64230761, 3. ], [- 0.51233174, -0.10359941, 4. ])
Here's how to add a value to an empty list by column:
n = Np.array ([[1,2,3,4,5,6],[11,22,33,44,55,66],[111,222,333,444,555,666]]) # #产生一个三行六列容易区分的数组 n out[166]: array ([[1, 2, 3, 4, 5, 6], [One, one, one,, and ], [111, 222, 333, 444, 555, 666]]) sample = [[]for I in range ( 3] # #产生三行一列的空列表 out[172]: [[], [], []] for I in range (0,6,2): # #每间隔一列便添加到sample中 sample = Np.hstack ((SAMPLE,N[:,I,NP. Newaxis])) sample out[170]: Array ([[1., 3., 5.], [one., [ 111., 333., 555.]]
Continuous update ...