Skip to content

Understand it better #265

Answered by abhi-bhatra
pavittarx asked this question in Q&A
Aug 3, 2021 · 3 comments · 14 replies
Discussion options

You must be logged in to vote

Hi,
This line takes the second element from all the columns of array X and creates a new array for you with new dimensions.

  1. When we initialize X, its shape was (442, 10)

  1. But, when we use np.newaxis for array slicing, its shape will be (442, 1)

Let me explain you np.newaxis works with a simple example:

import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr)

Here the output will be:

[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]

But, when you do np.newaxis slicing:

import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[:, np.newaxis, 2])

Here, the output will be:

[[3]
 [8]]

I think the above shown example will help you better understand the us…

Replies: 3 comments 14 replies

Comment options

You must be logged in to vote
3 replies
@pavittarx
Comment options

@jlooper
Comment options

@pavittarx
Comment options

Answer selected by jlooper
Comment options

You must be logged in to vote
8 replies
@mubarak-mustopha
Comment options

@abhi-bhatra
Comment options

@mubarak-mustopha
Comment options

@jlooper
Comment options

@mubarak-mustopha
Comment options

Comment options

You must be logged in to vote
3 replies
@abhi-bhatra
Comment options

@mubarak-mustopha
Comment options

@mubarak-mustopha
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested needs review
4 participants