Python Forum
Why A[0] and A[0:1] have different sizes?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why A[0] and A[0:1] have different sizes?
#1
So I have this:

candidates = 
array([[1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0,
        0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0],
       [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
        0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1],
       [1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
        0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0]])
And I do not understand why this one:

candidates[0] = 
array([1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0,
       0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0]

candidates[0].shape = (34,)
is different from this one:

candidates[0:1] = 
array([[1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0,
        0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0]])

candidates[0:1].shape = (1, 34)
So basically, why candidates[0] is not exactly similar to candidates[0:1]? Because the later is supposed to represent the first element only!
Reply
#2
I imagine array is numpy.array. The recommended way to use numpy is:
import numpy as np
candidates = np.array(
    [[1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0,
      0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0],
     [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
      0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1],
     [1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
      0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0]])
About why is different, candidates is a (3, 34) matrix, so candidates[0] selects the 1st row and numpy returns it as a single vector matrix (1 dimension matrix)
But candidates[0:1] selects many rows (in this case just 1) and return a sub-matrix of your original matrix, so that is the reason the dimensions are (1, 34).
To see it better, think on what you expect as result of candidates[0:2]. It shall be a matrix of (2, 34) with the first 2 rows no?.
Reply
#3
(May-06-2018, 01:22 PM)killerrex Wrote: I imagine array is numpy.array. The recommended way to use numpy is:
import numpy as np
candidates = np.array(
    [[1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0,
      0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0],
     [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
      0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1],
     [1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
      0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0]])
About why is different, candidates is a (3, 34) matrix, so candidates[0] selects the 1st row and numpy returns it as a single vector matrix (1 dimension matrix)
But candidates[0:1] selects many rows (in this case just 1) and return a sub-matrix of your original matrix, so that is the reason the dimensions are (1, 34).
To see it better, think on what you expect as result of candidates[0:2]. It shall be a matrix of (2, 34) with the first 2 rows no?.

Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Data cardinality is ambiguous: x sizes: 51 y sizes: 26 sidra 0 2,295 Oct-03-2020, 11:43 AM
Last Post: sidra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020