How do you optimize this code for nn prediction?
<p>How do you optimize this code?
At the moment it is running to slow for the amount of data that goes through this loop. This code runs 1-nearest neighbor. It will predict the label of the training_element based off the p_data_set</p>

<pre><code>#               [x] ,           [[x1],[x2],[x3]],    [l1, l2, l3]
def prediction(training_element, p_data_set, p_label_set):
    temp = np.array([], dtype=float)
    for p in p_data_set:
        temp = np.append(temp, distance.euclidean(training_element, p))

    minIndex = np.argmin(temp)
    return p_label_set[minIndex]
</code></pre>

<p>Use a <a href="https://en.wikipedia.org/wiki/K-d_tree" rel="nofollow"><em>k</em>-D tree</a> for fast nearest-neighbour lookups, e.g. <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.cKDTree.html" rel="nofollow"><code>scipy.spatial.cKDTree</code></a>:</p>

<pre><code>from scipy.spatial import cKDTree

# I assume that p_data_set is (nsamples, ndims)
tree = cKDTree(p_data_set)

# training_elements is also assumed to be (nsamples, ndims)
dist, idx = tree.query(training_elements, k=1)

predicted_labels = p_label_set[idx]
</code></pre>

python performance numpy scipy nearest-neighbor