On Thu, May 3, 2012 at 3:12 PM, Moroney, Catherine M (388D) <[email protected]> wrote:
> Here is the python code: > > def single(element, targets): > > if (isinstance(element, tuple)): > xelement = element[0] > elif (isinstance(element, numpy.ndarray)): > xelement = element > else: > return FILL > > nlen = xelement.shape[0] > nvec = targets.data.shape[0] > x = xelement.reshape(1, nlen).repeat(nvec, axis=0) repeat is slow. I don't think you need it since broadcasting should take care of things. (But maybe I misunderstand the data.) > diffs = ((x - targets.data)**2).sum(axis=1) You could try np.dot instead of sum: one = np.ones(7); diff = np.dot(diff, one). You could even pass one in. > diffs = numpy.sqrt(diffs) Since you don't return the distance, no need for the sqrt. If you do need the sqrt only take the sqrt of one element instead of all elements. > return int(numpy.argmin(diffs, axis=0)) _______________________________________________ NumPy-Discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
