过滤 numpy 数组时 0:: 和 0: 之间有什么区别?
What is the difference between 0:: and 0: when filtering a numpy array?
我正在尝试使用 Python 和 NumPy 来解决问题的 Kaggle Titanic tutorial。我很难理解 data[0::, ] 和 data[0:, ] 之间的区别。我复制粘贴下面的相关代码片段:
for i in xrange(number_of_classes): #loop through each class
for j in xrange(number_of_price_brackets): #loop through each price bin
women_only_stats = data[ # Which element
(data[0::, 4] == "female") & # is a female and
(data[0::, 2].astype(np.float) # was ith class
== i+1)
& # and
(data[0:, 9].astype(np.float) # was greater
>= j * fare_bracket_size) # than this bin
& # and
(data[0:, 9].astype(np.float) # less than
< (j+1)*fare_bracket_size) # the next bin
, 1] # in the 2nd col
没有区别,两种方法都会以相同的方式挂接到 __getitem__
。
>>> class Thing(object):
... def __getitem__(self, item):
... print(repr(item))
...
>>> t = Thing()
>>> t[0:, 4]
(slice(0, None, None), 4)
>>> t[0::, 4]
(slice(0, None, None), 4)
我正在尝试使用 Python 和 NumPy 来解决问题的 Kaggle Titanic tutorial。我很难理解 data[0::, ] 和 data[0:, ] 之间的区别。我复制粘贴下面的相关代码片段:
for i in xrange(number_of_classes): #loop through each class
for j in xrange(number_of_price_brackets): #loop through each price bin
women_only_stats = data[ # Which element
(data[0::, 4] == "female") & # is a female and
(data[0::, 2].astype(np.float) # was ith class
== i+1)
& # and
(data[0:, 9].astype(np.float) # was greater
>= j * fare_bracket_size) # than this bin
& # and
(data[0:, 9].astype(np.float) # less than
< (j+1)*fare_bracket_size) # the next bin
, 1] # in the 2nd col
没有区别,两种方法都会以相同的方式挂接到 __getitem__
。
>>> class Thing(object):
... def __getitem__(self, item):
... print(repr(item))
...
>>> t = Thing()
>>> t[0:, 4]
(slice(0, None, None), 4)
>>> t[0::, 4]
(slice(0, None, None), 4)