Meshgrid usage and matrix vector merging in numpy of Python Scientific Computing Library __python

Source: Internet
Author: User

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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.