我正在学习python。我不明白 core_samples_mask [db.core_sample_indices_] = True 此代码。 core_sample_mask是从0到1499的数字数组,我不明白squrebracket在此代码中的含义,以及为什么= 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
答案 0 :(得分:0)
这只是索引分配。请注意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]