Можно ли сделать следующий цикл for быстрее, переместив его в numpy?
ri = numpy.zeros((R.shape[0],R.shape[2]))
for i in range(R.shape[0]):
ri[i, :] = R[i, indices[i], :]
Это относится к моему предыдущему вопросу: если столбец полностью nan , что должно было ускорить этот бит:
bestepsilons = numpy.zeros((R.shape[0]))
for i in range(R.shape[0]):
bestindex = numpy.nanargmin(R[i,:])
if(numpy.isnan(bestindex)):
bestepsilons[i]=numpy.nan
else:
bestepsilons[i]=epsilon[bestindex]
и решил (сам) как:
bestepsilons1 = numpy.zeros(R.shape[0])+numpy.nan
d0 = numpy.nanmin(R, axis=1) # places where the best index is not a nan
bestepsilons1[~numpy.isnan(d0)] = epsilon[numpy.nanargmin(R[~numpy.isnan(d0),:], axis=1)]
Но теперь более сложный случай:
bestepsilons = numpy.zeros((R.shape[0]))
for i in range(R.shape[0]):
bestindex = numpy.nanargmin(R[i,indices[i],:])
if(numpy.isnan(bestindex)):
bestepsilons[i]=numpy.nan
else:
bestepsilons[i]=epsilon[bestindex]
И теперь этот трюк, чтобы показать места, где лучший индекс не является наном, больше не работает с этим аргументом оси.