0

i have a numpy array p like this:

array([[ 0.92691702,  0.07308298],
   [ 0.65515095,  0.34484905],
   [ 0.32526151,  0.67473849],
   ..., 
   [ 0.34171992,  0.65828008],
   [ 0.77521514,  0.22478486],
   [ 0.96430103,  0.03569897]])

If i do x=p[:,1:2], i would get

array([[ 0.07308298], [ 0.34484905], [ 0.67473849], ..., [ 0.65828008], [ 0.22478486], [ 0.03569897]]) and x.shape is (5500,1)

However, if i do x=p[:,1], i would get

array([ 0.07308298,  0.34484905,  0.67473849, ...,  0.65828008,
    0.22478486,  0.03569897])

and x.shape is (5500, )

Why there is difference like this? It quite confuses me. Thanks all in advance for your help.

1
  • 1
    Same thing with lists: [1,2,3][1] is not the same as [1,2,3][1:2]. Try understanding this example first. Commented Jun 13, 2014 at 18:58

1 Answer 1

2

It's the difference between using a slice and a single integer in the ndarray.__getitem__ call. Slicing causes the ndarray to return "views" while integers cause the ndarray values.

I'm being a little loose in my terminology here -- Really, for your case they both return a numpy view -- It's easier to consider just the 1D case first:

>>> import numpy as np
>>> x = np.arange(10)
>>> x[1]
1
>>> x[1:2]
array([1])

This idea extends to multiple dimensions nicely -- If you pass a slice for a particular axis, you'll get "array-like" values along that axis. If you pass a scalar for a particular axis, you'll get scalars along that axis in the result.

Note that the 1D case really isn't any different from how a standard python list behaves:

>>> x = [1, 2, 3, 4]
>>> x[1]
2
>>> x[1:2]
[2]
Sign up to request clarification or add additional context in comments.

3 Comments

It's not the view/value distinction that's important here; it's the item/subsequence distinction. In fact, both operations return views for this case.
@user2357112 -- Of course you're right. I was trying to draw an analogy with the 1D case, but I suppose I wasn't explicit enough. Updated.
Thank you all, your answers really help, i understand that now. If i use slicing, i will get a sub sequence, if i use integer, i will only get the item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.