human
I am working on the file "Sessions/Session02/Day2/ModelSelection_ExerciseSolutions.ipynb".
The first task is:
Here are the important things about writing transformer objects for use in scikit-learn:
* The class must have the following methods:
    - fit: fit your training data
    - transform: transform your training data into the new representation
    - predict: predict new examples
    - score: score predictions
    - fit_transform is optional (I think)
* The __init__ method only sets up parameters. Don't put any relevant code in there (this is convention more than anything else, but it's a good one to follow!)
* The fit method is always called in a Pipeline object (either on its own or as part of fit_transform). It usually modifies the internal state of the object, so returning self (i.e. the object itself) is usually fine.
* For transformer objects, which don't need scoring and prediction methods, you can just return pass as above.
Exercise 8: Last time, you learned that the SDSS photometric classifier uses a single hard cut to separate stars and galaxies in imaging data:
$$\mathtt{psfMag} - \mathtt{cmodelMag} \gt 0.145,$$
sources that satisfy this criteria are considered galaxies.

Implement an object that takes $\mathtt{psfMag}$ and $\mathtt{cmodelMag}$ as inputs and has a free parameter s that sets the value above which a source is considered a galaxy. 
Implement a transform methods that returns a single binary feature that is one if $$\mathtt{psfMag} - \mathtt{cmodelMag} \gt p$$ and zero otherwise. 
Add this feature to your optimized set of features consisting of either the PCA or your alternative representation, and run a random forest classifier on both. Run a CV on all components involved.

Hint: $\mathtt{psfMag}$ and $\mathtt{cmodelMag}$ are the first and the last column in your feature vector, respectively.
Hint: You can use FeatureUnion to combine the outputs of two transformers in a single data set. (Note that using pipeline with all three will chain them, rather than compute the feature union, followed by a classifier). You can input your FeatureUnion object into Pipeline.
Can you write Python program for it?
 ... What is a Python program to compute the feature union, followed by a classifier)?
assistant
#Here is Python program to compute the feature union, followed by a classifier).

class PSFMagThreshold(BaseEstimator, TransformerMixin):

    def __init__(self, p=1.45,):

        """
        Initialize hyperparameters

        Parameters
        ----------
        p : float
            The threshold for the magnitude - model magnitude
        """

        self.p = p # store parameter in object
        
        return

    def fit(self,X):
        """
        I don't really need a fit method!
        """
        return self
        
        
    def transform(self, X):

        # extract relevant columns
        psfmag = X[:,0]
        c_psfmag = X[:,-1]
        
        # compute difference
        d_psfmag = psfmag - c_psfmag
        
        # make a 1D array of length N
        X_new = np.zeros(X.shape[0])
        
        X_new[d_psfmag > self.p] = 1.0
        
        # IMPORTANT: Your output vector must be a COLUMN vector
        # You can achieve this with the numpy function atleast_2D()
        # and the numpy function transpose()
        return np.atleast_2d(X_new).T

    def predict(self, X):
        pass
    
    def score(self, X):
        pass

    def fit_transform(self, X, y=None):

        self.fit(X)
        X_new = self.transform(X)

        return X_new


pt = PSFMagThreshold(p=1.45)
X_pt = pt.fit_transform(X)