robpy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of robpy might be problematic. Click here for more details.
- robpy/__init__.py +0 -0
- robpy/covariance/__init__.py +6 -0
- robpy/covariance/base.py +82 -0
- robpy/covariance/cellmcd.py +621 -0
- robpy/covariance/initial_ddcw.py +133 -0
- robpy/covariance/kendall.py +21 -0
- robpy/covariance/mcd.py +470 -0
- robpy/covariance/ogk.py +95 -0
- robpy/covariance/utils/alter_covariance.py +31 -0
- robpy/covariance/utils/cellmcd_utils.py +38 -0
- robpy/covariance/utils/cellmcd_visualization_utils.py +96 -0
- robpy/covariance/wrapping.py +72 -0
- robpy/datasets/__init__.py +4 -0
- robpy/datasets/base.py +389 -0
- robpy/datasets/data/animals.csv +66 -0
- robpy/datasets/data/glass.csv +181 -0
- robpy/datasets/data/stars.csv +48 -0
- robpy/datasets/data/telephone.csv +25 -0
- robpy/datasets/data/topgear.csv +298 -0
- robpy/datasets/descr/animals.rst +26 -0
- robpy/datasets/descr/glass.rst +20 -0
- robpy/datasets/descr/stars.rst +28 -0
- robpy/datasets/descr/telephone.rst +23 -0
- robpy/datasets/descr/topgear.rst +51 -0
- robpy/outliers/__init__.py +4 -0
- robpy/outliers/ddc.py +296 -0
- robpy/pca/__init__.py +3 -0
- robpy/pca/base.py +97 -0
- robpy/pca/robpca.py +89 -0
- robpy/pca/spca.py +48 -0
- robpy/preprocessing/__init__.py +3 -0
- robpy/preprocessing/data_cleaner.py +214 -0
- robpy/preprocessing/scaling.py +57 -0
- robpy/preprocessing/transfo.py +591 -0
- robpy/preprocessing/utils.py +63 -0
- robpy/regression/__init__.py +3 -0
- robpy/regression/base.py +116 -0
- robpy/regression/lts.py +408 -0
- robpy/regression/mm_estimator.py +57 -0
- robpy/regression/s_estimator.py +188 -0
- robpy/univariate/__init__.py +9 -0
- robpy/univariate/base.py +53 -0
- robpy/univariate/mcd.py +84 -0
- robpy/univariate/onestep_m.py +143 -0
- robpy/univariate/qn.py +112 -0
- robpy/univariate/tau.py +63 -0
- robpy/utils/distance.py +24 -0
- robpy/utils/general.py +31 -0
- robpy/utils/logging.py +22 -0
- robpy/utils/median.py +62 -0
- robpy/utils/outlyingness.py +61 -0
- robpy/utils/rho.py +35 -0
- robpy-0.1.0.dist-info/METADATA +31 -0
- robpy-0.1.0.dist-info/RECORD +55 -0
- robpy-0.1.0.dist-info/WHEEL +4 -0
robpy/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
from robpy.covariance.base import RobustCovarianceEstimator
|
|
2
|
+
from robpy.covariance.mcd import FastMCDEstimator
|
|
3
|
+
from robpy.covariance.wrapping import WrappingCovarianceEstimator
|
|
4
|
+
from robpy.covariance.ogk import OGKEstimator
|
|
5
|
+
from robpy.covariance.kendall import KendallTauEstimator
|
|
6
|
+
from robpy.covariance.cellmcd import CellMCDEstimator
|
robpy/covariance/base.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import matplotlib.pyplot as plt
|
|
3
|
+
import numpy as np
|
|
4
|
+
import pandas as pd
|
|
5
|
+
|
|
6
|
+
from scipy.stats import chi2
|
|
7
|
+
from scipy.linalg import pinvh
|
|
8
|
+
from sklearn.covariance import EmpiricalCovariance
|
|
9
|
+
from sklearn.exceptions import NotFittedError
|
|
10
|
+
from robpy.utils.distance import mahalanobis_distance
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class RobustCovarianceEstimator(EmpiricalCovariance):
|
|
14
|
+
def __init__(self, *, store_precision=True, assume_centered=False, nans_allowed=False):
|
|
15
|
+
super().__init__(
|
|
16
|
+
store_precision=store_precision,
|
|
17
|
+
assume_centered=assume_centered,
|
|
18
|
+
)
|
|
19
|
+
self.nans_allowed = nans_allowed
|
|
20
|
+
|
|
21
|
+
def fit(self, X: np.ndarray | pd.DataFrame) -> RobustCovarianceEstimator:
|
|
22
|
+
"""Fit the covariance estimator."""
|
|
23
|
+
if isinstance(X, pd.DataFrame):
|
|
24
|
+
X = X.values
|
|
25
|
+
if self.nans_allowed:
|
|
26
|
+
self.n_features_in_ = X.shape[1]
|
|
27
|
+
else:
|
|
28
|
+
X = self._validate_data(X) # this sets n_features_in_ also
|
|
29
|
+
|
|
30
|
+
if self.assume_centered:
|
|
31
|
+
self.location_ = np.zeros(X.shape[1])
|
|
32
|
+
else:
|
|
33
|
+
self.location_ = X.mean(0)
|
|
34
|
+
covariance = self.calculate_covariance(X)
|
|
35
|
+
self._set_covariance(covariance)
|
|
36
|
+
if self.store_precision:
|
|
37
|
+
self.precision_ = pinvh(covariance)
|
|
38
|
+
self.default_covariance_ = np.cov(X, rowvar=False)
|
|
39
|
+
self._robust_distances = mahalanobis_distance(
|
|
40
|
+
X, location=self.location_, covariance=self.covariance_
|
|
41
|
+
)
|
|
42
|
+
self._mahalanobis_distances = mahalanobis_distance(
|
|
43
|
+
X, location=self.location_, covariance=self.default_covariance_
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
return self
|
|
47
|
+
|
|
48
|
+
def calculate_covariance(self, X) -> np.ndarray:
|
|
49
|
+
raise NotImplementedError
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def covariance(self) -> np.ndarray:
|
|
53
|
+
if not hasattr(self, "covariance_") or self.covariance_ is None:
|
|
54
|
+
raise NotFittedError()
|
|
55
|
+
return self.covariance_
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def correlation(self) -> np.ndarray:
|
|
59
|
+
if not hasattr(self, "correlation_"):
|
|
60
|
+
raise AttributeError("This estimator does not calculate correlation.")
|
|
61
|
+
return self.correlation_
|
|
62
|
+
|
|
63
|
+
def distance_distance_plot(
|
|
64
|
+
self, chi2_percentile: float = 0.975, figsize: tuple[int, int] = (10, 4)
|
|
65
|
+
):
|
|
66
|
+
if not hasattr(self, "covariance_"):
|
|
67
|
+
raise NotFittedError()
|
|
68
|
+
|
|
69
|
+
fig, ax = plt.subplots(1, 1, figsize=figsize)
|
|
70
|
+
|
|
71
|
+
threshold = np.sqrt(chi2.ppf(chi2_percentile, self.n_features_in_))
|
|
72
|
+
|
|
73
|
+
ax.scatter(x=self._mahalanobis_distances, y=self._robust_distances)
|
|
74
|
+
|
|
75
|
+
ax.axhline(threshold, color="grey", linestyle="--")
|
|
76
|
+
ax.axvline(threshold, color="grey", linestyle="--")
|
|
77
|
+
ax.axline((0, 0), slope=1, color="grey", linestyle="-.")
|
|
78
|
+
|
|
79
|
+
ax.set_xlabel("Mahalanobis distance (default covariance)")
|
|
80
|
+
ax.set_ylabel("Robust distance")
|
|
81
|
+
|
|
82
|
+
return fig
|