def testNearest2(self): with self.test_session(): [indices, distances] = clustering_ops.nearest_neighbors(self._points, self._centers, 2) self.assertAllClose(indices.eval(), [[0, 1], [0, 1], [1, 0], [4, 3]]) self.assertAllClose(distances.eval(), [[0., 2.], [5., 5.], [1., 5.], [0., 2.]])
def predict(self, X, with_distances=False): """ predict. Predict the closest cluster. Arguments: X: `1-D Array` or `2-D Array` of shape (n_samples, n_features). The sample(s) to predict. Return: cluster_indices or (cluster_indices, distances). """ X, orig_ndim = prepare_X(X, 2, max_dim=2, min_dim=1, debug_msg="X") with self.graph.as_default(): # Build Tree Graph self._build_estimator() if not self._pred.is_ready: input = tf.placeholder(tf.float32, name='pred_input', shape=[None, self.num_features]) output = c_ops.nearest_neighbors( input, self._cluster_centers_vars, k=1) self._build_pred(input, output) indices, distances = self.session.run(self._pred.output_tensor, feed_dict={self._pred.input_tensor: X}) indices = indices[0] distances = distances[0] if orig_ndim == 1: indices = indices[0] distances = distances[0] if with_distances: return indices, distances return indices
def testNearest1(self): with self.cached_session(): [indices, distances ] = clustering_ops.nearest_neighbors(self._points, self._centers, 1) self.assertAllClose(indices.eval(), [[0], [0], [1], [4]]) self.assertAllClose(distances.eval(), [[0.], [5.], [1.], [0.]])
def testNearest5(self): with self.test_session(): [indices, distances] = clustering_ops.nearest_neighbors(self._points, self._centers, 5) self.assertAllClose(indices.eval(), self._expected_nearest_neighbor_indices[:, 0:5]) self.assertAllClose( distances.eval(), self._expected_nearest_neighbor_squared_distances[:, 0:5])
def predict(self, X, with_distances=False): """ predict. Predict the closest cluster. Arguments: X: `1-D Array` or `2-D Array` of shape (n_samples, n_features). The sample(s) to predict. Return: cluster_indices or (cluster_indices, distances). """ X, orig_ndim = prepare_X(X, 2, max_dim=2, min_dim=1, debug_msg="X") with self.graph.as_default(): # Build Tree Graph self._build_estimator() if not self._pred.is_ready: input = tf.placeholder(tf.float32, name='pred_input', shape=[None, self.num_features]) output = c_ops.nearest_neighbors(input, self._cluster_centers_vars, k=1) self._build_pred(input, output) indices, distances = self.session.run( self._pred.output_tensor, feed_dict={self._pred.input_tensor: X}) indices = indices[0] distances = distances[0] if orig_ndim == 1: indices = indices[0] distances = distances[0] if with_distances: return indices, distances return indices
def testNearest1(self): with self.cached_session(): [indices, distances] = clustering_ops.nearest_neighbors(self._points, self._centers, 1) self.assertAllClose(indices.eval(), [[0], [0], [1], [4]]) self.assertAllClose(distances.eval(), [[0.], [5.], [1.], [0.]])