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