I. The role of Meshgrid
First, the function of Meshgrid is to extend the two vectors horizontally and vertically.
>>> import NumPy as NP
>>> x=np.arange ( -1,3)
>>> x
Array ([-1, 0, 1, 2])
>>> Y=np.array ([7,8,9])
>>> y
Array ([7, 8, 9])
>>> Xe,ye=np.meshgrid (x,y)
>>> XE
Array ([[1, 0, 1, 2], [-1, 0, 1, 2],
[-1, 0, 1, 2]]
>>> ye
Array ([[7, 7, 7, 7],
[8, 8, 8, 8],
[9, 9, 9, 9]])
That is, the resulting xe and ye of the Meshgrid effect are arrays of matrices with the same dimensions
Then, then flatten the XE, and the Ye Show and the transpose, and then the row vectors merge into an array
>>> xe.ravel ()
Array ([-1, 0, 1, 2,-1, 0, 1, 2,-1, 0, 1, 2])
>>> Ye.ravel (). T
Array ([7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9])
>>> np.array ([Xe.ravel (), Ye.ravel (). T]
Array ([1, 0, 1, 2,-1, 0, 1, 2,-1, 0, 1, 2],
[7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9]]
The effect of the above result is to combine x with each element in Y, that is, Cartesian product correspondence; This technique is commonly used when constructing composite features.
On the dimensions of vectors in NumPy
Given the row vector A and array B, it needs to be noted that the dimension of A is 1*4, and it is no use to turn the column vector 4*1 directly to the transpose, which requires the specified dimension to be transformed
>>> A=np.array ([3,5,1,2])
>>> a
array ([3, 5, 1, 2])
>>> a.shape
(4,)
>>> a.t
Array ([3, 5, 1, 2])
>>> B=np.array ([[[9,8,7,6],[5,0,4,3]])
>>> b
Array ([[9, 8, 7, 6],
[5, 0, 4, 3]])
>>> b.t Array ([9, 5
], [8, 0], [7, 4], [6, 3]
])
>>> Np.dot (a,b.t)
array
>>> np.hstack ((a,a))
Array ([3, 5, 1, 2, 3, 5, 1, 2])
>>> a.reshape (4,1)
Array ([[3],
[5],
[1],
[2]])
The dimension problem of vector, when the training set of machine learning and the label category of validation set are merged, special attention is paid to
hstack action by row vector。