def update_centroids(self): gather = self._points.gather(R.find_indices(self._label, Tensor([0]))).mean(axis=1) for i in range(1, self.k): ind = R.find_indices(self._label, Tensor([i])) gat = R.gather(self._points, ind).mean(axis=1) gather = R.concat(gather, gat) self.centroids = gather.reshape( shape=[self.k, len(self._points.output[0])])
def update_centroids(self): gather = R.gather(self.points, R.find_indices(self.label, values=[0])).mean(axis=1) for i in range(1, self.k): ind = R.find_indices(self.label, values=[i]) gat = R.gather(self.points, ind).mean(axis=1) gather = R.concat(gather, gat) self.centroids = gather.reshape( shape=[self.k, len(self.points.output[0])]) inform_server()
def predict(self, X): n_q = len(X) self._X = R.Tensor(X) d_list = self.__eucledian_distance(self._X) # print(d_list) fe = d_list.foreach(operation='sort') sl = fe.foreach(operation='slice', begin=0, size=self.k) while sl.status != "computed": pass pred = R.Tensor([], name="prediction") for i in range(n_q): row = R.gather(d_list, R.Tensor([i])).reshape(shape=[self.n]) values = sl.gather(R.Tensor([i])).reshape(shape=[self.k]) while values.status != 'computed': pass ind = R.find_indices(row, values) while ind.status != 'computed': pass ind = ind.foreach(operation='slice', begin=0, size=1) y_neighbours = R.gather(self.Y, ind).reshape(shape=[self.k]) while y_neighbours.status != 'computed': pass pred = pred.concat(R.mean(y_neighbours).expand_dims(axis=0)) while pred.status != 'computed': pass print(pred) while pred.status != 'computed': pass self._label = pred return pred
def fit(self, X): self.n_q = len(X) self.X = Tensor(X) d_list = self.eucledian_distance(self.X) while d_list.status != "computed": pass #print(d_list) fe = d_list.foreach(operation='sort') sl = fe.foreach(operation='slice', begin=0, size=self.k) while sl.status != "computed": pass #print(sl) li = sl.output.tolist() for i in range(self.n_q): row = R.gather(d_list, Tensor([i])).reshape(shape=[self.n]) while row.status != 'computed': pass #print(row) ind = R.find_indices(row, values=li[i]) while ind.status != 'computed': pass #ind.foreach() #print(ind) ind = ind.foreach(operation='slice', begin=0, size=1) y_neighbours = R.gather(self.Y, ind) while y_neighbours.status != 'computed': pass print(y_neighbours) pass
def grow_tree(self, X, y, depth=0): pop_per_class = R.Tensor([]) for c in range(self.num_classes): pop_per_class = pop_per_class.concat( R.sum(R.equal(y, R.Scalar(c))).expand_dims()) predicted_class = R.argmax(pop_per_class) while predicted_class.status != "computed": pass node = Node(predicted_class=predicted_class.output, depth=depth) node.samples = R.shape(y).gather(R.Scalar(0)) if depth < self.max_depth: col, threshold = self.find_split(X, y) while threshold.status != "computed": pass z = X.shape_() z1 = y.shape_() while z1.status != "computed": pass if col is not None and threshold.output is not [None]: indices_left = X.transpose().gather( R.Scalar(col)).less(threshold) X_left = X.gather( R.find_indices(indices_left, R.Tensor( [1])).reshape(shape=R.sum(indices_left).expand_dims())) y_left = y.gather( R.find_indices(indices_left, R.Tensor( [1])).reshape(shape=R.sum(indices_left).expand_dims())) indices_right = X.transpose().gather( R.Scalar(col)).greater_equal(threshold) X_right = X.gather( R.find_indices(indices_right, R.Tensor([ 1 ])).reshape(shape=R.sum(indices_right).expand_dims())) y_right = y.gather( R.find_indices(indices_right, R.Tensor([ 1 ])).reshape(shape=R.sum(indices_right).expand_dims())) node.feature_index = col node.threshold = threshold node.left = self.grow_tree(X_left, y_left, depth + 1) node.left.leftbranch = True node.right = self.grow_tree(X_right, y_right, depth + 1) node.right.rightbranch = True return node
def update_centroids(self, points, label): while label.status != 'computed': pass if 0 in label.output: gather = R.gather(points, R.find_indices(label, values=[0])).mean(axis=1) else: gather = R.gather(self.centroids, Tensor([0])).expand_dims(axis=0) for i in range(1, self.k): if i in label.output: ind = R.find_indices(label, values=[i]) gat = R.gather(points, ind).mean(axis=1) else: gat = R.gather(self.centroids, Tensor([i])).expand_dims(axis=0) gather = R.concat(gather, gat) while gat.status != 'computed': pass return gather.reshape(shape=[self.k, len(self.points.output[0])])
def predict(self, X): n_q = len(X) X = Tensor(X) d_list = self.__euclidean_distance(X) fe = d_list.foreach(operation='sort') sl = fe.foreach(operation='slice', begin=0, size=self._k) label = R.Tensor([], name="label") for i in range(n_q): row = R.gather(d_list, Tensor([i])).reshape(shape=[self._n]) values = sl.gather(Tensor([i])).reshape(shape=[self._k]) print(values, row) ind = R.find_indices(row, values) ind = ind.foreach(operation='slice', begin=0, size=1) y_neighbours = R.gather(self._y, ind).reshape(shape=[self._k]) label = label.concat(R.mode(y_neighbours)) # Store labels locally self._labels = label return label