Created
June 28, 2013 21:03
-
-
Save dashesy/5888095 to your computer and use it in GitHub Desktop.
Joint classifier for FeatureUnion in the pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Created on June 26, 2013 | |
@author: dashesy | |
Purpose: glue the classifier wire logic to have multiple | |
classifiers work jointly with different groups of X,y | |
Each group could accept different number os samples or features | |
''' | |
import numpy as np | |
import warnings | |
from scipy import linalg | |
from sklearn.base import BaseEstimator, TransformerMixin | |
from sklearn.utils import array2d | |
class JointClassifier(BaseEstimator, TransformerMixin): | |
''' | |
Glue logic for FeatureUnion to accept different X,y | |
If joint group numbers match acts as a connected wire, otherwise as a buffered wire | |
Parameters | |
---------- | |
group : int, | |
Transformation group number (0-based). | |
Default is 1 (i.e. the second group) | |
copy : bool | |
If False, data passed to fit are overwritten | |
self._n_samples = n_samples | |
self._n_features = n_features | |
Attributes | |
---------- | |
`_X` : array, shape=[n_samples, n_features] | |
Previous Training data. | |
`_y` : array-like, shape = [n_samples] | |
Previous Target vector relative to X | |
'_X_transformed' : array, shape=[n_samples, n_features] | |
Previous transformation result | |
''' | |
def __init__(self, joint_group = 1, copy=True): | |
self.copy = copy | |
self.joint_group = joint_group | |
def fit(self, X, y, **params): | |
"""Fit the model according to the given training data | |
Parameters | |
---------- | |
X: array-like, shape (n_samples, n_features) | |
Training data, where n_samples in the number of samples | |
and n_features is the number of features. | |
y : array-like, shape = [n_samples] | |
Target vector relative to X | |
joint_group: Transformation group number (0-based). | |
Groups must match in order to fit or transform | |
Default is 0 (i.e. the first group) | |
Returns | |
------- | |
self : object | |
Returns the instance itself. | |
""" | |
joint_group = 0 | |
if params.has_key('joint_group'): | |
joint_group = params['joint_group'] | |
if joint_group == self.joint_group: | |
self._fit(X, y) | |
return self | |
def fit_transform(self, X, y, **params): | |
"""Fit the model with X and apply the dimensionality reduction on X. | |
Parameters | |
---------- | |
X : array-like, shape (n_samples, n_features) | |
Training data, where n_samples in the number of samples | |
and n_features is the number of features. | |
y : array-like, shape = [n_samples] | |
Target vector relative to X | |
joint_group: Transformation group number (0-based). | |
Groups must match in order to fit or transform | |
Default is 0 (i.e. the first group) | |
Returns | |
------- | |
X_new : array-like, shape (n_samples, n_axis) | |
""" | |
joint_group = 0 | |
if params.has_key('joint_group'): | |
joint_group = params['joint_group'] | |
if joint_group == self.joint_group: | |
self._fit(X, y) | |
return self.transform(X, **params) | |
def _fit(self, X, y = None): | |
''' | |
Fit the model with X | |
''' | |
X = array2d(X) | |
# Buffer X,y | |
self._X = X | |
self._y = y | |
def transform(self, X, **params): | |
"""Apply the dimensionality reduction on X. | |
Parameters | |
---------- | |
X : array-like, shape (n_samples, n_features) | |
New data, where n_samples in the number of samples | |
and n_features is the number of features. | |
joint_group: Transformation group number (0-based). | |
If joint groups matche this transformation acts as a connected wire, otherwise as a buffered wire for previous transformation | |
Default is 0 (i.e. the first group) | |
Returns | |
------- | |
X_new : array-like, shape (n_samples, n_axis) | |
""" | |
joint_group = 0 | |
if params.has_key('joint_group'): | |
joint_group = params['joint_group'] | |
X = array2d(X) | |
n_samples, n_features = X.shape | |
if joint_group != self.joint_group: | |
# Model must be transformed once before with matching joint group | |
return self._X_transformed | |
self._X_transformed = X | |
return X | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment