<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def coarse_grain(G, ncg):
 """ Coarse-grain last index of array ``G``. Bin the last index of array ``G`` in bins of width ``ncg``, and replace each bin by its average. Return the binned results. Args: G: Array to be coarse-grained. ncg: Bin width for coarse-graining. """

        if ncg <= 1:
            return G
        G = numpy.asarray(G)
        nbin, remainder = divmod(G.shape[-1], ncg)
        if remainder != 0:
            nbin += 1
        return numpy.transpose([
            numpy.sum(G[..., i:i+ncg], axis=-1) / G[..., i:i+ncg].shape[-1]
            for i in numpy.arange(0, ncg * nbin, ncg)
            ])