Skip to content

Commit

Permalink
Make compute_sse a prviate method & move outside of class
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusHolly committed Oct 17, 2024
1 parent bd3244c commit b8149b5
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions idaes/apps/grid_integration/multiperiod/price_taker_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@
_logger = idaeslog.getLogger(__name__)


def _compute_sse(data, centroids, idx):
"""
PRIVATE METHOD
Method used to compute the inertia (sum of square errors) for k clusters.
Args:
data: Columnar data for a given LMP signal
centroids: Array of k centroids
idx: Index for data
Returns:
inertia: Sum of square errors for k clusters
"""
inertia = 0
for i, centroid in enumerate(centroids):
cluster_points = data[idx == i]
inertia += np.sum((cluster_points - centroid) ** 2)
return inertia


class PriceTakerModel(ConcreteModel):
def __init__(self, seed=20, horizon_length=24):
super().__init__()
Expand Down Expand Up @@ -85,25 +106,6 @@ def horizon_length(self, value):
)
self._horizon_length = value

@staticmethod
def compute_sse(data, centroids, idx):
"""
Method used to compute the inertia (sum of square errors) for k clusters.
Args:
data: Columnar data for a given LMP signal
centroids: Array of k centroids
idx: Index for data
Returns:
inertia: Sum of square errors for k clusters
"""
inertia = 0
for i, centroid in enumerate(centroids):
cluster_points = data[idx == i]
inertia += np.sum((cluster_points - centroid) ** 2)
return inertia

def generate_daily_data(self, raw_data):
"""
Function used to generate the daily data in a usable format
Expand Down Expand Up @@ -186,7 +188,7 @@ def get_optimal_n_clusters(
idx, _ = vq(whitened_daily_data, centroids)

# Compute the inertia (SSE) for k clusters
inertia = self.compute_sse(whitened_daily_data, centroids, idx)
inertia = _compute_sse(whitened_daily_data, centroids, idx)
inertia_values.append(inertia)

# Calculate the second derivative
Expand Down

0 comments on commit b8149b5

Please sign in to comment.