Search code examples
pythonnumpygradient-descent

Update a numpy array except i-th entry


I am trying to implement SGD algorithm, where there is a update formula

This could be easily done by using

temp = beta_old[i]
beta = beta_old
beta[i] = temp

But I find this ugly and I am wondering if there is any more elegant way to do this (maybe by using some indexing tricks).


Solution

  • You may want to use a mask:

    mask = np.ones(size, dtype=np.bool)
    mask[i] = false
    

    Then use the mask later:

    beta[mask] = beta_old[mask]
    

    But it may be slower than your current method.