New answers tagged

1

The second loop can be eliminated by creating a 2D array by tiling the self.x array with the np.tile method. The removal of elements in de 1D array with np.delete can than be replaced with a boolean mask (unfortunately this results in a 1D array which needs to be reshaped). The calculation of the 2D array can be done outside the first loop, as can be the ...


2

Okay, so there are a lot of problems here. I think the best way to address them is in stages. I want to show you how to go through your own code critically and address problems in it as you write. Before I begin, for the benefit of someone who will review your code, it is helpful to include all the imports you used so that the person doing the review can ...


0

For really large sparse matrices, convert numpy dense to scipy.sparse. These store only the non-zeros (well, 2 ints + 1 double, 24 bytes): import scipy.sparse S = scipy.sparse.csr_matrix( C ) # dense to sparse print( "S: %s %d non-0" % (S.shape, S.nnz) ) S *= 30 S.data += 1 # increment only the non-0 # Dense = S.toarray() # sparse to dense (...


3

This seems to so what you want, but is specific to the example and code you gave. There are two pairs of subarrays in arr and two different sets of indices and data to add to the subarrays. So there are four combinations. These get figured out by the values of i, j, and 'k'. Because the data to be added is sparse, I'm going to use scipy.sparse.coo_matrix() ...


0

A one liner: np.sum(r_array*va_array, axis=1, keepdims=True) To match r_array@va_array, use va_array.T in the 1liner.


Top 50 recent answers are included