簡體   English   中英

python中的方括號函數

[英]Square bracket function in python

我正在學習蟒蛇。 我不明白 core_samples_mask[db.core_sample_indices_] = True 這段代碼。 core_sample_mask 是數字 0 到 1499 的數組,我不明白此代碼中的方括號是什么意思,為什么后面跟着 = True。

 #data prep, dbscn clustering
 X, y = createDataPoints([[4,3], [2,-1], [-1,4]] , 1500, 0.5)
 epsilon = 0.3
 minimumSamples = 7
 db = DBSCAN(eps=epsilon, min_samples=minimumSamples).fit(X)
 labels = db.labels_
 labels

 #create an array of booleans using the labels from db (I dont       understand what this means..)
 core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
 core_samples_mask[db.core_sample_indices_] = True
 core_samples_mask

這只是索引分配。 注意arr哪些元素發生了變化:

In [374]: mask = np.zeros(6, dtype=bool)                                                  
In [375]: mask                                                                            
Out[375]: array([False, False, False, False, False, False])
In [376]: mask[[1,3,4]] = True                                                            
In [377]: mask                                                                            
Out[377]: array([False,  True, False,  True,  True, False])

相同的操作,但使用整數 dtype 數組:

In [378]: arr = np.zeros(6, dtype=int)                                                    
In [379]: arr                                                                             
Out[379]: array([0, 0, 0, 0, 0, 0])
In [380]: arr[[1,3,4]] = 1                                                                
In [381]: arr                                                                             
Out[381]: array([0, 1, 0, 1, 1, 0])

類似的列表分配:

In [382]: alist = [1,2,3,4]                                                               
In [383]: alist[2] = 200                                                                  
In [384]: alist                                                                           
Out[384]: [1, 2, 200, 4]

暫無
暫無

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

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