{"id":13423789,"url":"https://github.com/mhahsler/dbscan","last_synced_at":"2025-05-14T20:09:34.714Z","repository":{"id":37734829,"uuid":"43447011","full_name":"mhahsler/dbscan","owner":"mhahsler","description":"Density Based Clustering of Applications with Noise (DBSCAN) and Related Algorithms - R package","archived":false,"fork":false,"pushed_at":"2025-04-03T21:23:54.000Z","size":9844,"stargazers_count":325,"open_issues_count":4,"forks_count":64,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-13T15:59:53.271Z","etag":null,"topics":["clustering","cran","dbscan","density-based-clustering","hdbscan","lof","optics","r"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mhahsler.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-30T17:14:37.000Z","updated_at":"2025-04-12T19:36:22.000Z","dependencies_parsed_at":"2024-01-16T12:46:23.386Z","dependency_job_id":"7b58074d-dd92-4ccc-9bd1-f25e4f46c1a5","html_url":"https://github.com/mhahsler/dbscan","commit_stats":{"total_commits":258,"total_committers":8,"mean_commits":32.25,"dds":0.1705426356589147,"last_synced_commit":"f95fe5a7926199cc320f30b678ee1da7bf77128f"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhahsler%2Fdbscan","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhahsler%2Fdbscan/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhahsler%2Fdbscan/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mhahsler%2Fdbscan/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mhahsler","download_url":"https://codeload.github.com/mhahsler/dbscan/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248741200,"owners_count":21154252,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clustering","cran","dbscan","density-based-clustering","hdbscan","lof","optics","r"],"created_at":"2024-07-31T00:00:42.469Z","updated_at":"2025-04-13T16:00:01.521Z","avatar_url":"https://github.com/mhahsler.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"---\noutput: github_document\nbibliography: vignettes/dbscan.bib\nlink-citations: yes\n---\n\n```{r echo=FALSE, results = 'asis'}\npkg \u003c- 'dbscan'\n\nsource(\"https://raw.githubusercontent.com/mhahsler/pkg_helpers/main/pkg_helpers.R\")\npkg_title(pkg, anaconda = \"r-dbscan\", stackoverflow = \"dbscan%2br\")\n```\n\n## Introduction\n\nThis R package [@hahsler2019dbscan] provides a fast C++ (re)implementation of several density-based algorithms with a focus on the DBSCAN family for clustering spatial data.\nThe package includes: \n \n__Clustering__\n\n- __DBSCAN:__ Density-based spatial clustering of applications with noise [@ester1996density].\n- __Jarvis-Patrick Clustering__: Clustering using a similarity measure based\non shared near neighbors [@jarvis1973].\n- __SNN Clustering__: Shared nearest neighbor clustering [@erdoz2003].\n- __HDBSCAN:__  Hierarchical DBSCAN with simplified hierarchy extraction [@campello2015hierarchical].\n- __FOSC:__ Framework for optimal selection of clusters for unsupervised and semisupervised clustering of hierarchical cluster tree [@campello2013density].\n- __OPTICS/OPTICSXi:__ Ordering points to identify the clustering structure and cluster extraction methods\n  [@ankerst1999optics].\n\n__Outlier Detection__\n\n- __LOF:__ Local outlier factor algorithm [@breunig2000lof]. \n- __GLOSH:__ Global-Local Outlier Score from Hierarchies algorithm [@campello2015hierarchical]. \n\n__Cluster Evaluation__\n\n- __DBCV:__ Density-based clustering validation [@moulavi2014].\n\n__Fast Nearest-Neighbor Search (using kd-trees)__\n\n- __kNN search__\n- __Fixed-radius NN search__\n\n\nThe implementations use the kd-tree data structure (from library ANN) for faster k-nearest neighbor search, and are\nfor Euclidean distance typically faster than the native R implementations (e.g., dbscan in package `fpc`), or the \nimplementations in [WEKA](https://ml.cms.waikato.ac.nz/weka/), [ELKI](https://elki-project.github.io/) and [Python's scikit-learn](https://scikit-learn.org/).\n\n```{r echo=FALSE, results = 'asis'}\npkg_usage(pkg)\npkg_citation(pkg, 2)\npkg_install(pkg)\n```\n\n## Usage\n\nLoad the package and use the numeric variables in the iris dataset\n```{r}\nlibrary(\"dbscan\")\n\ndata(\"iris\")\nx \u003c- as.matrix(iris[, 1:4])\n```\n\nDBSCAN\n```{r}\ndb \u003c- dbscan(x, eps = .42, minPts = 5)\ndb\n```\n\nVisualize the resulting clustering (noise points are shown in black).\n```{r dbscan}\npairs(x, col = db$cluster + 1L)\n```\n\n\nOPTICS\n```{r}\nopt \u003c- optics(x, eps = 1, minPts = 4)\nopt\n```\n\nExtract DBSCAN-like clustering from OPTICS \nand create a reachability plot (extracted DBSCAN clusters at eps_cl=.4 are colored)\n```{r OPTICS_extractDBSCAN, fig.height=3}\nopt \u003c- extractDBSCAN(opt, eps_cl = .4)\nplot(opt)\n```\n\nHDBSCAN\n\n```{r}\nhdb \u003c- hdbscan(x, minPts = 4)\nhdb\n```\n\nVisualize the hierarchical clustering as a simplified tree. HDBSCAN finds 2 stable clusters.\n\n```{r hdbscan, fig.height=4}\nplot(hdb, show_flat = TRUE)\n```\n\n## Using dbscan with tidyverse\n\n`dbscan` provides for all clustering algorithms `tidy()`, `augment()`, and `glance()` so they can\nbe easily used with tidyverse, ggplot2 and [tidymodels](https://www.tidymodels.org/learn/statistics/k-means/).\n\n```{r tidyverse, message=FALSE, warning=FALSE}\nlibrary(tidyverse)\ndb \u003c- x %\u003e% dbscan(eps = .42, minPts = 5)\n```\n\nGet cluster statistics as a tibble\n\n```{r tidyverse2}\ntidy(db)\n```\n\nVisualize the clustering with ggplot2 (use an x for noise points)\n```{r tidyverse3}\naugment(db, x) %\u003e% \n  ggplot(aes(x = Petal.Length, y = Petal.Width)) +\n    geom_point(aes(color = .cluster, shape = noise)) +\n    scale_shape_manual(values=c(19, 4))\n\n```\n\n\n\n\n## Using dbscan from Python\nR, the R package `dbscan`, and the Python package `rpy2` need to be installed.\n\n```{python, eval = FALSE}\nimport pandas as pd\nimport numpy as np\n\n### prepare data\niris = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', \n                   header = None, \n                   names = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species'])\niris_numeric = iris[['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth']]\n\n# get R dbscan package\nfrom rpy2.robjects import packages\ndbscan = packages.importr('dbscan')\n\n# enable automatic conversion of pandas dataframes to R dataframes\nfrom rpy2.robjects import pandas2ri\npandas2ri.activate()\n\ndb = dbscan.dbscan(iris_numeric, eps = 0.5, MinPts = 5)\nprint(db)\n```\n\n```\n## DBSCAN clustering for 150 objects.\n## Parameters: eps = 0.5, minPts = 5\n## Using euclidean distances and borderpoints = TRUE\n## The clustering contains 2 cluster(s) and 17 noise points.\n## \n##  0  1  2 \n## 17 49 84 \n## \n## Available fields: cluster, eps, minPts, dist, borderPoints\n```\n\n```{python, eval = FALSE}\n# get the cluster assignment vector\nlabels = np.array(db.rx('cluster'))\nlabels\n```\n\n```\n## array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n##         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1,\n##         1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 0, 2, 2, 2, 2, 2,\n##         2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,\n##         2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 0, 0, 2, 0, 0,\n##         2, 2, 2, 2, 2, 2, 2, 0, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0,\n##         2, 2, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]],\n##       dtype=int32)\n```\n\n## License \nThe dbscan package is licensed under the [GNU General Public License (GPL) Version 3](https://www.gnu.org/licenses/gpl-3.0.en.html). The __OPTICSXi__ R implementation was directly ported from the ELKI framework's Java implementation (GNU AGPLv3), with permission by the original author, Erich Schubert.  \n\n## Changes\n* List of changes from [NEWS.md](https://github.com/mhahsler/dbscan/blob/master/NEWS.md)\n\n## References\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhahsler%2Fdbscan","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmhahsler%2Fdbscan","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmhahsler%2Fdbscan/lists"}