def sparsify_columns(arr, n_non_zero, keep_diagonal=False, random_state=None):
    """Set all but ``n_non_zero`` entries to zero for each column of ``arr``.

    This is a common technique to find better starting points for learning
    deep and/or recurrent networks.

    Parameters
    ----------

    arr : array_like, two dimensional
      Array to work upon in place.

    n_non_zero : integer
      Amount of non zero entries to keep.

    keep_diagonal : boolean, optional [default: False]
      If set to True and ``arr`` is square, do keep the diagonal.

    random_state : numpy.random.RandomState object, optional [default : None]
      If set, random number generator that will generate the indices
      corresponding to the zero-valued columns.

    Examples
    --------

    >>> import numpy as np
    >>> from climin.initialize import sparsify_columns
    >>> arr = np.arange(9).reshape((3, 3))
    >>> sparsify_columns(arr, 1)
    >>> arr                                         # doctest: +SKIP
    array([[0, 0, 0],
           [0, 4, 5],
           [6, 0, 0]])
    """
    colsize = arr.shape[0]

    # In case it's gnumpy, copy to numpy array first. The sparsifying loop will
    # run in numpy.
    arr_np = arr if isinstance(arr, np.ndarray) else arr.as_numpy_array()
    mask = np.ones_like(arr_np)
    for i in range(arr.shape[1]):
        idxs = range(colsize)
        if random_state is None:
            zeros = random.sample(idxs, colsize - n_non_zero)
        else:
            zeros = random_state.choice(idxs, n_non_zero - colsize,
                                        replace=False)
        mask[zeros, i] *= 0
    if keep_diagonal and arr.shape[0] == arr.shape[1]:
        mask += np.eye(arr.shape[0])
    arr *= mask
dataset/ETHPy150Open BRML/climin/climin/initialize.py/sparsify_columns/swapped operands of `-`