Skip to content

Commit

Permalink
first prototype for detecting invalid features
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalau authored and manumerous committed Nov 21, 2021
1 parent 0dca4d3 commit 29775ea
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions Tools/parametric_model/src/optimizers/linear_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def estimate_parameters(self, X, y):
# Estimate parameters c such that X * c = y
self.X = X
self.y = y
self.check_features()
self.reg.fit(self.X, self.y)
self.estimation_completed = True

Expand Down
11 changes: 11 additions & 0 deletions Tools/parametric_model/src/optimizers/optimizer_base_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

from abc import ABC, abstractmethod
from typing import Dict, List
import warnings
import numpy as np


class ParametersNotEstimatedError(Exception):
Expand Down Expand Up @@ -67,6 +69,15 @@ def check_estimation_completed(self):
else:
raise self.parametersNotEstimatedError

def check_features(self):
for i in range(self.X.shape[1]):
if np.count_nonzero(self.X[:,i]) == 0:
warnings.warn("Feature detected that is only zero. " + \
"Parameter {} is probably wrong.".format(self.param_name_list[i]),
RuntimeWarning
)
return

@abstractmethod
def estimate_parameters(self) -> None:
pass
Expand Down
1 change: 1 addition & 0 deletions Tools/parametric_model/src/optimizers/qp_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def estimate_parameters(self, X, y):
# remove fixed coefficients from problem formulation
self.X = X
self.y = y
self.check_features()
self.X_reduced, self.y_reduced = self.remove_fixed_coef_features(X, y)
self.y = y
c = cvxpy.Variable(self.n_opt_coef)
Expand Down

0 comments on commit 29775ea

Please sign in to comment.