1
- import Tree from '../util/tree.js'
2
-
3
1
/**
4
2
* Hierarchical Density-based spatial clustering of applications with noise
5
3
*/
@@ -82,7 +80,7 @@ export default class HDBSCAN {
82
80
83
81
const tree = [ ]
84
82
for ( let i = 0 ; i < n ; i ++ ) {
85
- tree [ i ] = new Tree ( { point : datas [ i ] , index : [ i ] , distance : 0 } )
83
+ tree [ i ] = { index : [ i ] , distance : 0 , children : [ ] }
86
84
}
87
85
while ( tree . length > 1 ) {
88
86
let min_i = - 1
@@ -107,13 +105,11 @@ export default class HDBSCAN {
107
105
}
108
106
dmreach [ min_i ] . splice ( min_j , 1 )
109
107
dmreach . splice ( min_j , 1 )
110
- tree [ min_i ] = new Tree (
111
- {
112
- distance : min_d ,
113
- index : [ ...tree [ min_i ] . value . index , ...tree [ min_j ] . value . index ] ,
114
- } ,
115
- [ tree [ min_i ] , tree [ min_j ] ]
116
- )
108
+ tree [ min_i ] = {
109
+ distance : min_d ,
110
+ index : [ ...tree [ min_i ] . index , ...tree [ min_j ] . index ] ,
111
+ children : [ tree [ min_i ] , tree [ min_j ] ] ,
112
+ }
117
113
tree . splice ( min_j , 1 )
118
114
}
119
115
@@ -122,29 +118,29 @@ export default class HDBSCAN {
122
118
const clusters = [ ]
123
119
while ( stack . length > 0 ) {
124
120
let [ tree , ctree ] = stack . pop ( )
125
- ctree . index = tree . value . index
126
- ctree . lbirth = 1 / tree . value . distance
121
+ ctree . index = tree . index
122
+ ctree . lbirth = 1 / tree . distance
127
123
ctree . stability = 0
128
124
ctree . children = [ ]
129
125
clusters . push ( ctree )
130
126
while ( true ) {
131
- for ( let i = tree . length - 1 ; i >= 0 ; i -- ) {
132
- const c = tree . at ( i )
133
- if ( c . value . index . length < this . _minClusterSize ) {
134
- tree . removeAt ( i )
135
- if ( c . value . distance > 0 ) {
136
- ctree . stability += c . value . index . length * ( 1 / c . value . distance - ctree . lbirth )
127
+ for ( let i = tree . children . length - 1 ; i >= 0 ; i -- ) {
128
+ const c = tree . children [ i ]
129
+ if ( c . index . length < this . _minClusterSize ) {
130
+ tree . children . splice ( i , 1 )
131
+ if ( c . distance > 0 ) {
132
+ ctree . stability += c . index . length * ( 1 / c . distance - ctree . lbirth )
137
133
}
138
134
}
139
135
}
140
- if ( tree . length === 0 ) {
136
+ if ( tree . children . length === 0 ) {
141
137
break
142
- } else if ( tree . length === 1 ) {
143
- tree = tree . at ( 0 )
138
+ } else if ( tree . children . length === 1 ) {
139
+ tree = tree . children [ 0 ]
144
140
} else {
145
- for ( let i = 0 ; i < tree . length ; i ++ ) {
141
+ for ( let i = 0 ; i < tree . children . length ; i ++ ) {
146
142
const ct = { }
147
- stack . push ( [ tree . at ( i ) , ( ctree . children [ i ] = ct ) ] )
143
+ stack . push ( [ tree . children [ i ] , ( ctree . children [ i ] = ct ) ] )
148
144
}
149
145
break
150
146
}
0 commit comments