簡體   English   中英

Python numpy數組索引可以為-1嗎?

[英]Python numpy array index can be -1?

我正在嘗試從https://github.com/Itseez/opencv/blob/master/samples/python2/letter_recog.py獲取 opencv示例,我需要幫助來解密此代碼。

new_samples = np.zeros((sample_n * self.class_n, var_n+1), np.float32)
new_samples[:,:-1] = np.repeat(samples, self.class_n, axis=0)
new_samples[:,-1] = np.tile(np.arange(self.class_n), sample_n)

我知道np.repeatnp.tile是什么,但是我不確定使用-1索引應該做什么new_samples[:,:-1]new_samples[:,-1] 我知道numpy數組索引的工作原理,但是沒有看到這種情況。 我無法通過搜索找到解決方案。

Python切片和numpy切片略有不同。 但通常數組或列表中的-1表示倒數(從最后一項開始)。 信息介紹中提到的字符串為:

>>> word = 'Python'
>>> word[-1]  #last character 
'n'

對於列表為:

>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[-1]
25

就像您的示例一樣,它也可以擴展為numpy數組索引

new_samples[:,:-1]表示除最后一列外的所有行

new_samples[:,-1]表示僅所有行和最后一列

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM