1
|
|
|
import sklearn.cluster as skl_cluster |
|
|
|
|
2
|
|
|
from Orange.data import Table, DiscreteVariable, Domain, Instance |
3
|
|
|
from Orange.projection import SklProjector, Projection |
4
|
|
|
from numpy import atleast_2d, ndarray, where |
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
__all__ = ["DBSCAN"] |
8
|
|
|
|
9
|
|
|
class DBSCAN(SklProjector): |
10
|
|
|
__wraps__ = skl_cluster.DBSCAN |
11
|
|
|
|
12
|
|
|
def __init__(self, eps=0.5, min_samples=5, metric='euclidean', |
|
|
|
|
13
|
|
|
algorithm='auto', leaf_size=30, p=None, |
|
|
|
|
14
|
|
|
preprocessors=None): |
15
|
|
|
super().__init__(preprocessors=preprocessors) |
16
|
|
|
self.params = vars() |
17
|
|
|
|
18
|
|
|
def fit(self, X, Y=None): |
19
|
|
|
proj = skl_cluster.DBSCAN(**self.params) |
20
|
|
|
self.X = X |
|
|
|
|
21
|
|
|
if isinstance(X, Table): |
22
|
|
|
proj = proj.fit(X.X,) |
23
|
|
|
else: |
24
|
|
|
proj = proj.fit(X, ) |
25
|
|
|
return DBSCANModel(proj) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class DBSCANModel(Projection): |
29
|
|
|
def __init__(self, proj): |
30
|
|
|
super().__init__(proj=proj) |
31
|
|
|
|
32
|
|
|
def __call__(self, data): |
33
|
|
|
if isinstance(data, ndarray): |
34
|
|
|
return self.proj.fit_predict(data).reshape((len(data), 1)) |
35
|
|
|
|
36
|
|
|
if isinstance(data, Table): |
37
|
|
|
if data.domain is not self.pre_domain: |
38
|
|
|
data = Table(self.pre_domain, data) |
39
|
|
|
y = self.proj.fit_predict(data.X) |
40
|
|
|
vals = [-1] + list(self.proj.core_sample_indices_) |
41
|
|
|
c = DiscreteVariable(name='Core sample index', values=vals) |
42
|
|
|
domain = Domain([c]) |
43
|
|
|
return Table(domain, y.reshape(len(y), 1)) |
44
|
|
|
|
45
|
|
|
elif isinstance(data, Instance): |
46
|
|
|
if data.domain is not self.pre_domain: |
47
|
|
|
data = Instance(self.pre_domain, data) |
48
|
|
|
# Instances-by-Instance classification is not defined; |
49
|
|
|
raise Exception("Core sample assignment is not supported " |
50
|
|
|
"for single instances.") |
51
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.