tsam 2.3.8__py3-none-any.whl → 3.0.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.
- tsam/__init__.py +79 -0
- tsam/api.py +602 -0
- tsam/config.py +852 -0
- tsam/exceptions.py +17 -0
- tsam/hyperparametertuning.py +289 -245
- tsam/periodAggregation.py +140 -141
- tsam/plot.py +513 -0
- tsam/py.typed +0 -0
- tsam/representations.py +177 -167
- tsam/result.py +397 -0
- tsam/timeseriesaggregation.py +1446 -1361
- tsam/tuning.py +1038 -0
- tsam/utils/durationRepresentation.py +229 -231
- tsam/utils/k_maxoids.py +138 -145
- tsam/utils/k_medoids_contiguity.py +139 -140
- tsam/utils/k_medoids_exact.py +232 -239
- tsam/utils/segmentation.py +232 -118
- {tsam-2.3.8.dist-info → tsam-3.0.0.dist-info}/METADATA +124 -81
- tsam-3.0.0.dist-info/RECORD +23 -0
- {tsam-2.3.8.dist-info → tsam-3.0.0.dist-info}/WHEEL +1 -1
- {tsam-2.3.8.dist-info → tsam-3.0.0.dist-info}/licenses/LICENSE.txt +21 -21
- tsam-2.3.8.dist-info/RECORD +0 -16
- {tsam-2.3.8.dist-info → tsam-3.0.0.dist-info}/top_level.txt +0 -0
tsam/periodAggregation.py
CHANGED
|
@@ -1,141 +1,140 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
:
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
:
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
n_sets
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return clusterCenters, clusterCenterIndices, clusterOrder
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from tsam.representations import representations
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def aggregatePeriods(
|
|
7
|
+
candidates,
|
|
8
|
+
n_clusters=8,
|
|
9
|
+
n_iter=100,
|
|
10
|
+
clusterMethod="k_means",
|
|
11
|
+
solver="highs",
|
|
12
|
+
representationMethod=None,
|
|
13
|
+
representationDict=None,
|
|
14
|
+
distributionPeriodWise=True,
|
|
15
|
+
timeStepsPerPeriod=None,
|
|
16
|
+
):
|
|
17
|
+
"""
|
|
18
|
+
Clusters the data based on one of the cluster methods:
|
|
19
|
+
'averaging', 'k_means', 'exact k_medoid' or 'hierarchical'
|
|
20
|
+
|
|
21
|
+
:param candidates: Dissimilarity matrix where each row represents a candidate. required
|
|
22
|
+
:type candidates: np.ndarray
|
|
23
|
+
|
|
24
|
+
:param n_clusters: Number of aggregated cluster. optional (default: 8)
|
|
25
|
+
:type n_clusters: integer
|
|
26
|
+
|
|
27
|
+
:param n_iter: Only required for the number of starts of the k-mean algorithm. optional (default: 10)
|
|
28
|
+
:type n_iter: integer
|
|
29
|
+
|
|
30
|
+
:param clusterMethod: Chosen clustering algorithm. Possible values are
|
|
31
|
+
'averaging','k_means','exact k_medoid' or 'hierarchical'. optional (default: 'k_means')
|
|
32
|
+
:type clusterMethod: string
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
# cluster the data
|
|
36
|
+
if clusterMethod == "averaging":
|
|
37
|
+
n_sets = len(candidates)
|
|
38
|
+
if n_sets % n_clusters == 0:
|
|
39
|
+
cluster_size = int(n_sets / n_clusters)
|
|
40
|
+
clusterOrder = [
|
|
41
|
+
[n_cluster] * cluster_size for n_cluster in range(n_clusters)
|
|
42
|
+
]
|
|
43
|
+
else:
|
|
44
|
+
cluster_size = int(n_sets / n_clusters)
|
|
45
|
+
clusterOrder = [
|
|
46
|
+
[n_cluster] * cluster_size for n_cluster in range(n_clusters)
|
|
47
|
+
]
|
|
48
|
+
clusterOrder.append(
|
|
49
|
+
[n_clusters - 1] * int(n_sets - cluster_size * n_clusters)
|
|
50
|
+
)
|
|
51
|
+
clusterOrder = np.hstack(np.array(clusterOrder, dtype=object))
|
|
52
|
+
clusterCenters, clusterCenterIndices = representations(
|
|
53
|
+
candidates,
|
|
54
|
+
clusterOrder,
|
|
55
|
+
default="meanRepresentation",
|
|
56
|
+
representationMethod=representationMethod,
|
|
57
|
+
representationDict=representationDict,
|
|
58
|
+
distributionPeriodWise=distributionPeriodWise,
|
|
59
|
+
timeStepsPerPeriod=timeStepsPerPeriod,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
if clusterMethod == "k_means":
|
|
63
|
+
from sklearn.cluster import KMeans
|
|
64
|
+
|
|
65
|
+
k_means = KMeans(n_clusters=n_clusters, max_iter=1000, n_init=n_iter, tol=1e-4)
|
|
66
|
+
|
|
67
|
+
clusterOrder = k_means.fit_predict(candidates)
|
|
68
|
+
# get with own mean representation to avoid numerical trouble caused by sklearn
|
|
69
|
+
clusterCenters, clusterCenterIndices = representations(
|
|
70
|
+
candidates,
|
|
71
|
+
clusterOrder,
|
|
72
|
+
default="meanRepresentation",
|
|
73
|
+
representationMethod=representationMethod,
|
|
74
|
+
representationDict=representationDict,
|
|
75
|
+
distributionPeriodWise=distributionPeriodWise,
|
|
76
|
+
timeStepsPerPeriod=timeStepsPerPeriod,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
if clusterMethod == "k_medoids":
|
|
80
|
+
from tsam.utils.k_medoids_exact import KMedoids
|
|
81
|
+
|
|
82
|
+
k_medoid = KMedoids(n_clusters=n_clusters, solver=solver)
|
|
83
|
+
|
|
84
|
+
clusterOrder = k_medoid.fit_predict(candidates)
|
|
85
|
+
clusterCenters, clusterCenterIndices = representations(
|
|
86
|
+
candidates,
|
|
87
|
+
clusterOrder,
|
|
88
|
+
default="medoidRepresentation",
|
|
89
|
+
representationMethod=representationMethod,
|
|
90
|
+
representationDict=representationDict,
|
|
91
|
+
distributionPeriodWise=distributionPeriodWise,
|
|
92
|
+
timeStepsPerPeriod=timeStepsPerPeriod,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
if clusterMethod == "k_maxoids":
|
|
96
|
+
from tsam.utils.k_maxoids import KMaxoids
|
|
97
|
+
|
|
98
|
+
k_maxoid = KMaxoids(n_clusters=n_clusters)
|
|
99
|
+
|
|
100
|
+
clusterOrder = k_maxoid.fit_predict(candidates)
|
|
101
|
+
clusterCenters, clusterCenterIndices = representations(
|
|
102
|
+
candidates,
|
|
103
|
+
clusterOrder,
|
|
104
|
+
default="maxoidRepresentation",
|
|
105
|
+
representationMethod=representationMethod,
|
|
106
|
+
representationDict=representationDict,
|
|
107
|
+
distributionPeriodWise=distributionPeriodWise,
|
|
108
|
+
timeStepsPerPeriod=timeStepsPerPeriod,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
if clusterMethod == "hierarchical" or clusterMethod == "adjacent_periods":
|
|
112
|
+
if n_clusters == 1:
|
|
113
|
+
clusterOrder = np.asarray([0] * len(candidates))
|
|
114
|
+
else:
|
|
115
|
+
from sklearn.cluster import AgglomerativeClustering
|
|
116
|
+
|
|
117
|
+
if clusterMethod == "hierarchical":
|
|
118
|
+
clustering = AgglomerativeClustering(
|
|
119
|
+
n_clusters=n_clusters, linkage="ward"
|
|
120
|
+
)
|
|
121
|
+
elif clusterMethod == "adjacent_periods":
|
|
122
|
+
adjacencyMatrix = np.eye(len(candidates), k=1) + np.eye(
|
|
123
|
+
len(candidates), k=-1
|
|
124
|
+
)
|
|
125
|
+
clustering = AgglomerativeClustering(
|
|
126
|
+
n_clusters=n_clusters, linkage="ward", connectivity=adjacencyMatrix
|
|
127
|
+
)
|
|
128
|
+
clusterOrder = clustering.fit_predict(candidates)
|
|
129
|
+
# represent hierarchical aggregation with medoid
|
|
130
|
+
clusterCenters, clusterCenterIndices = representations(
|
|
131
|
+
candidates,
|
|
132
|
+
clusterOrder,
|
|
133
|
+
default="medoidRepresentation",
|
|
134
|
+
representationMethod=representationMethod,
|
|
135
|
+
representationDict=representationDict,
|
|
136
|
+
distributionPeriodWise=distributionPeriodWise,
|
|
137
|
+
timeStepsPerPeriod=timeStepsPerPeriod,
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
return clusterCenters, clusterCenterIndices, clusterOrder
|