Np.newaxis adds an axis to Numpy.ndarray (multidimensional array)

Source: Internet
Author: User
Tags lua

type(np.newaxis)NoneType

Np.newaxis in use and functionally equivalent to none, in fact, is an alias of none.

1. Np.newaxis's Practical
>> x = np.arange(3)>> xarray([0, 1, 2])>> x.shape(3,)>> x[:, np.newaxis]array([[0], [1], [2]])>> x[:, None]array([[0], [1], [2]])>> x[:, np.newaxis].shape (3, 1)
2. A row vector is returned when a column of a multidimensional array is indexed
>>> X = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])>>> X[:, 1]array([2, 6, 10]) % 这里是一个行>>> X[:, 1].shape % X[:, 1] 的用法完全等同于一个行,而不是一个列,(3, )

So, a correct way to index is:

>>>X[:, 1][:, np.newaxis]array([[2],      [6],      [10]])

If you want to make the concatenation (cascade) of the second and fourth columns:

>>>X_sub = np.hstack([X[:, 1][:, np.newaxis], X[:, 3][:, np.newaxis]])            % hstack:horizontal stack,水平方向上的层叠>>>X_subarray([[2, 4] [6, 8] [10, 12]])

Np.newaxis adds an axis to Numpy.ndarray (multidimensional array)

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.