| @@ 35-94 (lines=60) @@ | ||
| 32 | ||
| 33 | """ |
|
| 34 | ||
| 35 | def __init__(self, loss='logistic', l2=1.0, iwe='lr', smoothing=True, |
|
| 36 | clip=-1, kernel_type='rbf', bandwidth=1): |
|
| 37 | """ |
|
| 38 | Select a particular type of importance-weighted classifier. |
|
| 39 | ||
| 40 | Parameters |
|
| 41 | ---------- |
|
| 42 | loss : str |
|
| 43 | loss function for weighted classifier, options: 'logistic', |
|
| 44 | 'quadratic', 'hinge' (def: 'logistic') |
|
| 45 | l2 : float |
|
| 46 | l2-regularization parameter value (def:0.01) |
|
| 47 | iwe : str |
|
| 48 | importance weight estimator, options: 'lr', 'nn', 'rg', 'kmm', |
|
| 49 | 'kde' (def: 'lr') |
|
| 50 | smoothing : bool |
|
| 51 | whether to apply Laplace smoothing to the nearest-neighbour |
|
| 52 | importance-weight estimator (def: True) |
|
| 53 | clip : float |
|
| 54 | maximum allowable importance-weight value; if set to -1, then the |
|
| 55 | weights are not clipped (def:-1) |
|
| 56 | kernel_type : str |
|
| 57 | what type of kernel to use for kernel density estimation or kernel |
|
| 58 | mean matching, options: 'diste', 'rbf' (def: 'rbf') |
|
| 59 | bandwidth : float |
|
| 60 | kernel bandwidth parameter value for kernel-based weight |
|
| 61 | estimators (def: 1) |
|
| 62 | ||
| 63 | Returns |
|
| 64 | ------- |
|
| 65 | None |
|
| 66 | ||
| 67 | """ |
|
| 68 | self.loss = loss |
|
| 69 | self.l2 = l2 |
|
| 70 | self.iwe = iwe |
|
| 71 | self.smoothing = smoothing |
|
| 72 | self.clip = clip |
|
| 73 | self.kernel_type = kernel_type |
|
| 74 | self.bandwidth = bandwidth |
|
| 75 | ||
| 76 | # Initialize untrained classifiers based on choice of loss function |
|
| 77 | if self.loss == 'logistic': |
|
| 78 | # Logistic regression model |
|
| 79 | self.clf = LogisticRegression() |
|
| 80 | elif self.loss == 'quadratic': |
|
| 81 | # Least-squares model |
|
| 82 | self.clf = LinearRegression() |
|
| 83 | elif self.loss == 'hinge': |
|
| 84 | # Linear support vector machine |
|
| 85 | self.clf = LinearSVC() |
|
| 86 | else: |
|
| 87 | # Other loss functions are not implemented |
|
| 88 | raise NotImplementedError('Loss function not implemented.')
|
|
| 89 | ||
| 90 | # Whether model has been trained |
|
| 91 | self.is_trained = False |
|
| 92 | ||
| 93 | # Dimensionality of training data |
|
| 94 | self.train_data_dim = '' |
|
| 95 | ||
| 96 | def iwe_ratio_gaussians(self, X, Z): |
|
| 97 | """ |
|
| @@ 25-74 (lines=50) @@ | ||
| 22 | functions. |
|
| 23 | """ |
|
| 24 | ||
| 25 | def __init__(self, loss='logistic', l2=1.0, num_pivots=1, |
|
| 26 | num_components=1): |
|
| 27 | """ |
|
| 28 | Select a particular type of importance-weighted classifier. |
|
| 29 | ||
| 30 | Parameters |
|
| 31 | ---------- |
|
| 32 | loss : str |
|
| 33 | loss function for weighted classifier, options: 'logistic', |
|
| 34 | 'quadratic', 'hinge' (def: 'logistic') |
|
| 35 | l2 : float |
|
| 36 | l2-regularization parameter value (def:0.01) |
|
| 37 | num_pivots : int |
|
| 38 | number of pivot features to use (def: 1) |
|
| 39 | num_components : int |
|
| 40 | number of components to use after extracting pivot features |
|
| 41 | (def: 1) |
|
| 42 | ||
| 43 | Returns |
|
| 44 | ------- |
|
| 45 | None |
|
| 46 | ||
| 47 | """ |
|
| 48 | self.loss = loss |
|
| 49 | self.l2 = l2 |
|
| 50 | self.num_pivots = num_pivots |
|
| 51 | self.num_components = num_components |
|
| 52 | ||
| 53 | # Initialize untrained classifiers based on choice of loss function |
|
| 54 | if self.loss == 'logistic': |
|
| 55 | # Logistic regression model |
|
| 56 | self.clf = LogisticRegression() |
|
| 57 | elif self.loss == 'quadratic': |
|
| 58 | # Least-squares model |
|
| 59 | self.clf = LinearRegression() |
|
| 60 | elif self.loss == 'hinge': |
|
| 61 | # Linear support vector machine |
|
| 62 | self.clf = LinearSVC() |
|
| 63 | else: |
|
| 64 | # Other loss functions are not implemented |
|
| 65 | raise NotImplementedError('Loss not implemented yet.')
|
|
| 66 | ||
| 67 | # Whether model has been trained |
|
| 68 | self.is_trained = False |
|
| 69 | ||
| 70 | # Maintain pivot component matrix |
|
| 71 | self.C = 0 |
|
| 72 | ||
| 73 | # Dimensionality of training data |
|
| 74 | self.train_data_dim = '' |
|
| 75 | ||
| 76 | def augment_features(self, X, Z, l2=0.0): |
|
| 77 | """ |
|
| @@ 34-75 (lines=42) @@ | ||
| 31 | | >>>> preds = clf.predict(Z) |
|
| 32 | """ |
|
| 33 | ||
| 34 | def __init__(self, loss='logistic', l2=1.0, num_components=1): |
|
| 35 | """ |
|
| 36 | Select a particular type of subspace aligned classifier. |
|
| 37 | ||
| 38 | Parameters |
|
| 39 | ---------- |
|
| 40 | loss : str |
|
| 41 | loss function for weighted classifier, options: 'logistic', |
|
| 42 | 'quadratic', 'hinge' (def: 'logistic') |
|
| 43 | l2 : float |
|
| 44 | l2-regularization parameter value (def:0.01) |
|
| 45 | num_components : int |
|
| 46 | number of transfer components to maintain (def: 1) |
|
| 47 | ||
| 48 | Returns |
|
| 49 | ------- |
|
| 50 | None |
|
| 51 | ||
| 52 | """ |
|
| 53 | self.loss = loss |
|
| 54 | self.l2 = l2 |
|
| 55 | self.num_components = num_components |
|
| 56 | ||
| 57 | # Initialize untrained classifiers |
|
| 58 | if self.loss == 'logistic': |
|
| 59 | # Logistic regression model |
|
| 60 | self.clf = LogisticRegression() |
|
| 61 | elif self.loss == 'quadratic': |
|
| 62 | # Least-squares model |
|
| 63 | self.clf = LinearRegression() |
|
| 64 | elif self.loss == 'hinge': |
|
| 65 | # Linear support vector machine |
|
| 66 | self.clf = LinearSVC() |
|
| 67 | else: |
|
| 68 | # Other loss functions are not implemented |
|
| 69 | raise NotImplementedError('Loss function not implemented.')
|
|
| 70 | ||
| 71 | # Whether model has been trained |
|
| 72 | self.is_trained = False |
|
| 73 | ||
| 74 | # Dimensionality of training data |
|
| 75 | self.train_data_dim = '' |
|
| 76 | ||
| 77 | def subspace_alignment(self, X, Z, num_components=1): |
|
| 78 | """ |
|