def check_valid_pipeline(steps):
    """
    Helper function to check whether a pipeline is constructed properly. Taken
    from original sklearn pipeline source code with minor modifications,
    which are commented below.
    """
    names, estimators = zip(*steps)
    transforms = estimators[:-1]
    estimator = estimators[-1]
    for t in transforms:
        if not hasattr(t, 'fit') and hasattr(t, 'transform'):
            raise TypeError(
                "All intermediate steps of the chain should be transforms and implement fit and transform '%s' (type %s) doesn't)"
                 % (t, type(t)))
    if not hasattr(estimator, 'fit'):
        raise TypeError(
            "Last step of chain should implement fit '%s' (type %s) doesn't)" %
            (estimator, type(estimator)))
