-
Notifications
You must be signed in to change notification settings - Fork 17
Implementation of DBSCAN model #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kelang-Tian
wants to merge
9
commits into
sql-machine-learning:develop
Choose a base branch
from
Kelang-Tian:model-tkl
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+243
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b4908be
dbscan model without test
827238b
dbscan model without test
db6b2d8
dbscan model with test
8b0ef5f
dbscan model without test
cca7b66
dbscan model without test
0e84ac0
add test with iris
3888acc
add func sqlflow_train_loop
99ec7fc
add input iris tf.dataset
143c856
rename dbscan test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
#!usr/bin/env python | ||
# -*- coding:utf-8 _*- | ||
|
||
""" | ||
__author__ : tiankelang | ||
__email__ : [email protected] | ||
__file_name__ : dbscan.py | ||
__create_time__ : 2020/07/01 | ||
|
||
demo iris: | ||
%%sqlflow | ||
SELECT * FROM iris.train | ||
TO TRAIN sqlflow_models.DBSCAN | ||
WITH | ||
model.min_samples=10, | ||
model.eps=0.3 | ||
INTO sqlflow_models.my_dbscan_model; | ||
""" | ||
import tensorflow as tf | ||
from scipy.spatial.distance import pdist, squareform | ||
from sklearn.base import BaseEstimator, ClusterMixin | ||
import pandas as pd | ||
from sklearn import datasets, metrics | ||
import numpy as np | ||
from scipy.spatial import KDTree | ||
from sklearn.datasets.samples_generator import make_blobs | ||
from sklearn.preprocessing import StandardScaler | ||
import six | ||
|
||
def optimizer(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that it's not a NN model, should we remove this function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there is no need to set optimizer. |
||
# SGD is just a placeholder to avoid panic on SQLFLow traning | ||
return tf.keras.optimizers.SGD(lr=0.1, momentum=0.9) | ||
|
||
|
||
def loss(): | ||
return None | ||
|
||
|
||
def prepare_prediction_column(prediction): | ||
"""Return the class label of highest probability.""" | ||
return prediction.argmax(axis=-1) | ||
|
||
|
||
def purity_score(y_true, y_pred): | ||
# compute contingency matrix | ||
contingency_matrix = metrics.cluster.contingency_matrix(y_true, y_pred) | ||
# return purity | ||
return np.sum(np.amax(contingency_matrix, axis=0)) / np.sum(contingency_matrix) | ||
|
||
|
||
class DBSCAN(tf.keras.Model): | ||
def __init__(self, | ||
eps: float = 0.5, | ||
min_samples: int = 5, | ||
has_label=False, | ||
feature_columns=None): | ||
''' | ||
:param eps: Neighborhood distance | ||
:param min_samples: | ||
The minimum number of samples required to form a class cluster | ||
''' | ||
super(DBSCAN, self).__init__(name='DBSCAN') | ||
self.eps = eps | ||
self.min_samples = min_samples | ||
self.core_sample_indices_ = list() | ||
self.components_ = None | ||
self.labels_ = None | ||
self.has_label = has_label | ||
|
||
def fit_predict(self, X): | ||
n_samples = len(X) | ||
|
||
kd_tree = KDTree(X) # build KDTree | ||
|
||
density_arr = np.array([len(kd_tree.query_ball_point(x, self.eps)) for x in X]) # 密度数组 | ||
|
||
visited_arr = [False for _ in range(n_samples)] # Access tag array | ||
|
||
k = -1 # init class | ||
self.labels_ = np.array([-1 for _ in range(n_samples)]) | ||
|
||
for sample_idx in range(n_samples): | ||
if visited_arr[sample_idx]: # Skip visited samples | ||
continue | ||
|
||
visited_arr[sample_idx] = True | ||
|
||
# Skip noise samples and boundary samples | ||
if density_arr[sample_idx] == 1 or density_arr[sample_idx] < self.min_samples: | ||
continue | ||
|
||
# core object | ||
else: | ||
# Find all the core objects in the neighborhood, including themselves | ||
cores = [idx for idx in kd_tree.query_ball_point(X[sample_idx], self.eps) if | ||
density_arr[idx] >= self.min_samples] | ||
k += 1 | ||
self.labels_[sample_idx] = k | ||
self.core_sample_indices_.append(sample_idx) | ||
|
||
while cores: | ||
cur_core = cores.pop(0) | ||
if not visited_arr[cur_core]: | ||
self.core_sample_indices_.append(cur_core) | ||
visited_arr[cur_core] = True | ||
self.labels_[cur_core] = k | ||
|
||
neighbors = kd_tree.query_ball_point(X[cur_core], self.eps) | ||
neighbor_cores = [idx for idx in neighbors if | ||
idx not in cores and density_arr[idx] >= self.min_samples] | ||
neighbor_boards = [idx for idx in neighbors if density_arr[idx] < self.min_samples] | ||
|
||
cores.extend(neighbor_cores) | ||
|
||
for idx in neighbor_boards: | ||
if self.labels_[idx] == -1: | ||
self.labels_[idx] = k | ||
|
||
# Update class properties | ||
self.core_sample_indices_ = np.sort(np.array(self.core_sample_indices_)) | ||
self.components_ = X[self.core_sample_indices_.astype('int64')] | ||
return self.labels_ | ||
|
||
|
||
def _read_Dataset_data(self, dataset): | ||
data = None | ||
label = None | ||
flag = True | ||
print("dataset:", dataset) | ||
for item in dataset: | ||
# print("item:", item) | ||
if flag: | ||
flag = False | ||
item_data = item[0] # dict | ||
len1 = len(item_data) | ||
index=0 | ||
|
||
feature_data = [] | ||
feature_column_names = [] | ||
|
||
for k, v in item_data.items(): | ||
if index == (len1-1): | ||
item_label = v.numpy().reshape(1, ) | ||
else: | ||
feature_column_names.append(k) | ||
feature_data.append(v.numpy()) | ||
index = index + 1 | ||
feature_data = np.asarray(feature_data).reshape(1, -1) | ||
|
||
data = np.asarray(feature_data).reshape(1, -1) | ||
label = item_label | ||
else: | ||
item_data = item[0] | ||
len1 = len(item_data) | ||
index = 0 | ||
|
||
feature_data = [] | ||
feature_column_names = [] | ||
|
||
for k, v in item_data.items(): | ||
if index == (len1 - 1): | ||
item_label = v.numpy().reshape(1, ) | ||
else: | ||
feature_column_names.append(k) | ||
feature_data.append(v.numpy()) | ||
index = index + 1 | ||
feature_data = np.asarray(feature_data).reshape(1, -1) | ||
|
||
data = np.concatenate((data, feature_data), axis=0) | ||
label = np.concatenate((label, item_label), axis=0) | ||
print("data:", type(data), data.shape) | ||
print("label:", type(label), label.shape) | ||
return data, label | ||
# do custom training here, parameter "dataset" is a tf.dataset type representing the input data. | ||
def sqlflow_train_loop(self, dataset, epochs=1, verbose=0): | ||
''' | ||
Parameter `epochs` and `verbose` will not be used in this function. :param dataset: demo iris, | ||
:param dataset: | ||
demo iris <class 'tensorflow.python.data.ops.dataset_ops.DatasetV1Adapter'> | ||
<DatasetV1Adapter shapes: ({sepal_length: (1,), sepal_width: (1,), petal_length: (1,), petal_width: (1,)}, | ||
(1, None)), types: ({sepal_length: tf.float32, sepal_width: tf.float32, petal_length: tf.float32, | ||
petal_width: tf.float32}, tf.int64)> | ||
:param epochs: | ||
:param verbose: | ||
:return: | ||
''' | ||
data, label = self._read_Dataset_data(dataset) | ||
|
||
self.fit_predict(data) | ||
print("DBSCAN(eps= %.2f, minpts= %d), the purity score: %f" % | ||
(self.eps, | ||
self.min_samples, | ||
purity_score(label, self.labels_))) | ||
# print("Predict labels:", self.labels_) | ||
# print("True labels:", label) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import sqlflow_models | ||
from tests.base import BaseTestCases | ||
import tensorflow as tf | ||
import unittest | ||
import numpy as np | ||
from sklearn.datasets import load_iris | ||
from sklearn.datasets.samples_generator import make_blobs | ||
from sklearn.preprocessing import StandardScaler | ||
from sklearn import datasets, metrics | ||
import logging | ||
from pathlib import Path | ||
from numpy import ndarray, testing | ||
|
||
iris = datasets.load_iris() | ||
iris_data = np.array(iris.data) | ||
iris_target = iris.target | ||
|
||
|
||
def purity_score(y_true, y_pred): | ||
# compute contingency matrix | ||
contingency_matrix = metrics.cluster.contingency_matrix(y_true, y_pred) | ||
# return purity | ||
return np.sum(np.amax(contingency_matrix, axis=0)) / np.sum(contingency_matrix) | ||
|
||
|
||
def print_in_test(string): | ||
logging.warning(string) | ||
|
||
|
||
class TestDBSCAN(unittest.TestCase): | ||
"""DBSCAN test cases.""" | ||
|
||
@classmethod | ||
def setUpClass(self): | ||
self.dbscan = sqlflow_models.DBSCAN( | ||
min_samples=10, eps=.4) | ||
self.dbscan.sqlflow_train_loop(iris_data) | ||
|
||
def test_dbscan_return_labels_with_type_numpy_array(self): | ||
self.assertIsInstance(self.dbscan.labels_, ndarray) | ||
print("Test DBSCAN (minpts=10, eps=0.4), the purity score: %f" % | ||
purity_score(iris_target, self.dbscan.labels_)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why increasing the timeout threshold ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Timeout occurred while installing the packages because of network instability.