Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. class Criterion:
  2.     def initialize(self, X, y):
  3.         self._check_Xy_pair(X, y)
  4.         self.X = X; self.y = y
  5.         n_samples, n_features = X.shape
  6.         self.weights = find_weights(X, y)
  7.         self.sigma2 = np.sum((y - np.dot(X, self.weights))**2) / (n_samples - n_features)
  8.         self.sigma = np.sqrt(self.sigma2)
  9.         return self
  10.     def _check_Xy_pair(self, X, y):
  11.         assert isinstance(X, np.ndarray)
  12.         assert isinstance(y, np.ndarray)
  13.         assert X.ndim == 2
  14.         assert y.ndim == 1
  15.         assert X.shape[0] == y.shape[0]
  16.     def __call__(self, indices):
  17.         assert False, "Not implemented"
  18.     def __repr__(self):
  19.         return 'Criterion'
  20.  
  21. class CriterionAIC(Criterion):
  22.     def __call__(self, indices):
  23.         X = self.X[:, indices]
  24.         n_samples, n_features = X.shape
  25.         weights = find_weights(X, self.y)
  26.         return (2 * n_features * self.sigma2 + np.sum((np.dot(X, weights) - self.y) ** 2)) / n_samples / self.sigma2
  27.     def __repr__(self):
  28.         return 'AIC'
  29.  
  30. class CriterionBIC(Criterion):
  31.     def __call__(self, indices):
  32.         X = self.X[:, indices]
  33.         n_samples, n_features = X.shape
  34.         weights = find_weights(X, self.y)
  35.         return (np.log(n_samples) * n_features * self.sigma2 + np.sum((np.dot(X, weights) - self.y) ** 2)) / n_samples / self.sigma2
  36.     def __repr__(self):
  37.         return 'BIC'
  38.  
  39. class CriterionLOO(Criterion):
  40.     def __call__(self, indices):
  41.         X = self.X[:, indices]
  42.         weights = find_weights(X, self.y)
  43.         n_samples, n_features = X.shape
  44.         U = np.dot(np.dot(X, np.linalg.inv(np.dot(X.T, X))), X.T)
  45.         U = U[np.arange(n_samples), np.arange(n_samples)]
  46.         return np.sum(((self.y - np.dot(X, weights)) / (1 - U)) ** 2) / n_samples
  47.     def __repr__(self):
  48.         return 'LOO'
  49.  
  50. class CriterionCV(Criterion):
  51.     def __init__(self, n_folds=5, random_state=1):
  52.         self.n_folds = n_folds
  53.         self.random_state = random_state
  54.     def __call__(self, indices):
  55.         X = self.X[:, indices]
  56.         n_samples, n_features = X.shape
  57.         kfold = KFold(n_splits=self.n_folds, shuffle=True, random_state=self.random_state)
  58.         R_CV = 0
  59.         for tr_indices, ts_indices in kfold.split(X):
  60.             X_tr = X[tr_indices]; y_tr = self.y[tr_indices]
  61.             X_ts = X[ts_indices]; y_ts = self.y[ts_indices]
  62.             weights = find_weights(X_tr, y_tr)
  63.             R_CV += np.sum((y_ts - np.dot(X_ts, weights)) ** 2)
  64.         return R_CV / n_samples
  65.     def __repr__(self):
  66.         return 'CV' + str(self.n_folds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement