robust-mixed-dist 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.
File without changes
@@ -0,0 +1,110 @@
1
+ import polars as pl
2
+ import pandas as pd
3
+ from scipy.spatial import distance
4
+ from scipy.spatial.distance import pdist, squareform
5
+
6
+ ################################################################################
7
+
8
+ def sokal_dist_matrix(X):
9
+ """
10
+ Calculates the Sokal distance matrix for a data matrix `X` using SciPy.
11
+
12
+ Parameters (inputs)
13
+ ----------
14
+ X: a pandas/polars DataFrame or a NumPy array. It represents a data matrix.
15
+
16
+ Returns (outputs)
17
+ -------
18
+ M: the Sokal distance matrix between the rows of X.
19
+ """
20
+
21
+ if isinstance(X, pl.DataFrame):
22
+ X = X.to_numpy()
23
+ if isinstance(X, pd.DataFrame):
24
+ X = X.to_numpy()
25
+
26
+ # Compute the pairwise distances using pdist and convert to a square form.
27
+ M = squareform(pdist(X, metric='sokalmichener'))
28
+
29
+ return M
30
+
31
+ ################################################################################
32
+
33
+ def sokal_dist(xi, xr) :
34
+ """
35
+ Calculates the Sokal distance between a pair of vectors.
36
+
37
+ Parameters (inputs)
38
+ ----------
39
+ xi, xr: a pair of quantitative vectors. They represent a couple of statistical observations.
40
+
41
+ Returns (outputs)
42
+ -------
43
+ The Sokal distance between the observations `xi` and `xr`.
44
+ """
45
+
46
+ if isinstance(xi, (pl.DataFrame, pd.DataFrame)) :
47
+ xi = xi.to_numpy().flatten()
48
+ elif isinstance(xi, (pd.Series, pl.Series)) :
49
+ xi = xi.to_numpy()
50
+ if isinstance(xr, (pl.DataFrame, pd.DataFrame)) :
51
+ xr = xr.to_numpy().flatten()
52
+ elif isinstance(xr, (pd.Series, pl.Series)) :
53
+ xr = xr.to_numpy()
54
+
55
+ return distance.sokalmichener(xi, xr)
56
+
57
+ ################################################################################
58
+
59
+ def jaccard_dist_matrix(X):
60
+ """
61
+ Calculates the Jaccard distance matrix for a data matrix `X` using SciPy.
62
+
63
+ Parameters (inputs)
64
+ ----------
65
+ X: a pandas/polars DataFrame or a NumPy array. It represents a data matrix.
66
+
67
+ Returns (outputs)
68
+ -------
69
+ M: the Jaccard distance matrix between the rows of X.
70
+ """
71
+
72
+ if isinstance(X, pl.DataFrame):
73
+ X = X.to_numpy()
74
+ if isinstance(X, pd.DataFrame):
75
+ X = X.to_numpy()
76
+
77
+ # Compute the pairwise distances using pdist and convert to a square form.
78
+ M = squareform(pdist(X, metric='jaccard'))
79
+
80
+ return M
81
+
82
+ ################################################################################
83
+
84
+ def jaccard_dist(xi, xr) :
85
+ """
86
+ Calculates the Jaccard distance between a pair of vectors.
87
+
88
+ Parameters (inputs)
89
+ ----------
90
+ xi, xr: a pair of quantitative vectors. They represent a couple of statistical observations.
91
+
92
+ Returns (outputs)
93
+ -------
94
+ The Jaccard distance between the observations `xi` and `xr`.
95
+ """
96
+
97
+ if isinstance(xi, (pl.DataFrame, pd.DataFrame)) :
98
+ xi = xi.to_numpy().flatten()
99
+ elif isinstance(xi, (pd.Series, pl.Series)) :
100
+ xi = xi.to_numpy()
101
+ if isinstance(xr, (pl.DataFrame, pd.DataFrame)) :
102
+ xr = xr.to_numpy().flatten()
103
+ elif isinstance(xr, (pd.Series, pl.Series)) :
104
+ xr = xr.to_numpy()
105
+
106
+ return distance.jaccard(xi, xr)
107
+
108
+ ################################################################################
109
+
110
+