A python package for a distance-based classifier which can use several different distance metrics.
To install DistClassiPy, run the following command:
pip install distclassipyHere's a quick example to get you started with DistClassiPy:
import distclassipy as dcpy
from sklearn.datasets import make_classification
X, y = make_classification(
n_samples=1000,
n_features=4,
n_informative=2,
n_redundant=0,
random_state=0,
shuffle=False,
)
# Example usage of DistanceMetricClassifier
clf = dcpy.DistanceMetricClassifier()
clf.fit(X, y)
print(clf.predict([[0, 0, 0, 0]], metric="canberra"))
# Example usage of EnsembleDistanceClassifier (combines all metrics: no need to choose one)
ensemble_clf = dcpy.EnsembleDistanceClassifier()
ensemble_clf.fit(X, y)
print(ensemble_clf.predict([[0, 0, 0, 0]]))All of DistClassiPy's distance metrics can also be used on their own. dcpy.cdist works just like scipy.spatial.distance.cdist, but also accepts every DistClassiPy metric, computed in compiled C code:
import numpy as np
XA = np.random.rand(5, 4)
XB = np.random.rand(3, 4)
dist = dcpy.cdist(XA, XB, metric="hellinger") # shape (5, 3)Since many scikit-learn estimators accept precomputed distances, this also lets you use any DistClassiPy metric with them, for example k-nearest neighbours:
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(metric="precomputed")
knn.fit(dcpy.cdist(X, X, metric="clark"), y)
print(knn.predict(dcpy.cdist([[0, 0, 0, 0]], X, metric="clark")))- Distance Metric-Based Classification: Utilizes a variety of distance metrics for classification.
- Customizable for Scientific Goals: Allows fine-tuning based on scientific objectives by selecting appropriate distance metrics and features, enhancing both computational efficiency and model performance.
- Interpretable Results: Offers improved interpretability of classification outcomes by directly using distance metrics and feature importance, making it ideal for scientific applications.
- Efficient and Scalable: Demonstrates lower computational requirements compared to traditional methods like Random Forests, making it suitable for large datasets.
- Open Source and Accessible: Available as an open-source Python package on PyPI, encouraging broad application in astronomy and beyond.
- (NEW) Ensemble Distance Classification: Combines all distance metrics, each using the features that suit it, into one vote weighted by each metric's cross-validated performance, so there is no need to choose a metric.
- (NEW) Expanded Distance Metrics: DistClassiPy now offers 43 built-in distance metrics, an increase from the previous 18. Additionally, users can still define and use custom distance metrics as needed.
- (NEW) Fast Distance Metrics: All of DistClassiPy's own metrics are now compiled (via Cython), running at speeds comparable to SciPy's built-in metrics, and are available on their own through
dcpy.cdist.
For more detailed information about the package and its functionalities, please refer to the official documentation.
Contributions are welcome! If you have suggestions for improvements or bug fixes, please feel free to open an issue or submit a pull request.
DistClassiPy is released under the GNU General Public License v3.0. See the LICENSE file for more details.
If you use DistClassiPy in your research or project, please consider citing the paper:
Chaini, S., Mahabal, A., Kembhavi, A., & Bianco, F. B. (2024). Light Curve Classification with DistClassiPy: a new distance-based classifier. Astronomy and Computing. https://doi.org/10.1016/j.ascom.2024.100850.
@ARTICLE{2024A&C....4800850C,
author = {{Chaini}, S. and {Mahabal}, A. and {Kembhavi}, A. and {Bianco}, F.~B.},
title = "{Light curve classification with DistClassiPy: A new distance-based classifier}",
journal = {Astronomy and Computing},
keywords = {Variable stars (1761), Astronomy data analysis (1858), Open source software (1866), Astrostatistics (1882), Classification (1907), Light curve classification (1954), Astrophysics - Instrumentation and Methods for Astrophysics, Astrophysics - Solar and Stellar Astrophysics, Computer Science - Machine Learning},
year = 2024,
month = jul,
volume = {48},
eid = {100850},
pages = {100850},
doi = {10.1016/j.ascom.2024.100850},
archivePrefix = {arXiv},
eprint = {2403.12120},
primaryClass = {astro-ph.IM},
adsurl = {https://ui.adsabs.harvard.edu/abs/2024A&C....4800850C},
adsnote = {Provided by the SAO/NASA Astrophysics Data System}
}
Siddharth Chaini, Ashish Mahabal, Ajit Kembhavi and Federica B. Bianco.