def z_score(x, axis=None): if not isinstance(x, R.Tensor): x = R.Tensor(x) if axis is not None: mean = R.mean(x, axis=axis) std = R.std(x, axis=axis) else: mean = R.mean(x) std = R.std(x) return R.div(R.sub(x, mean), std)
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 start_info(self, X): """ Calculate mean and standard deviation """ for feature in zip(*X): yield {'std': R.std(feature), 'mean': R.mean(features)}
def mean_squared_log_error(y_true, y_pred): """ Mean Squared Log Error """ if not isinstance(y_true, R.Tensor): y_true = R.Tensor(y_true) if not isinstance(y_pred, R.Tensor): y_pred = R.Tensor(y_pred) return R.mean(R.pow(R.sub(R.natlog(R.add(y_true, R.one())), R.natlog(R.add(y_pred, R.one()))), R.Scalar(2)))
def mean_absolute_error(y_true, y_pred): """ Mean Absolute Error """ if not isinstance(y_true, R.Tensor): y_true = R.Tensor(y_true) if not isinstance(y_pred, R.Tensor): y_pred = R.Tensor(y_pred) return R.mean(R.abs(R.sub(y_pred, y_true)))
def mean_squared_error(y_true, y_pred): """ Mean Squared Error """ if not isinstance(y_true, R.Tensor): y_true = R.Tensor(y_true) if not isinstance(y_pred, R.Tensor): y_pred = R.Tensor(y_pred) return R.mean(R.pow(R.sub(y_true, y_pred), R.Scalar(2)))
def standardize(x): """ Standardize an array """ if not isinstance(x, R.Tensor): x = R.Tensor(x) mean = R.mean(x) std = R.std(x) return R.div(R.sub(x, mean), std)
def log_loss(y_true, y_pred, with_logit=True): if with_logit: y_pred = sigmoid(y_pred) else: pass y_pred = R.clip(y_pred, R.epsilon(), R.sub(R.Scalar(1), R.epsilon())) loss = R.elemul(R.Scalar(-1), R.mean(R.elemul(y_true, R.natlog(y_pred)), R.elemul((R.sub(R.Scalar(1), y_true)), R.natlog(R.sub(R.Scalar(1), y_pred))))) return loss
def r2_score(y_true, y_pred): if not isinstance(y_true, R.Tensor): y_true = R.Tensor(y_true) if not isinstance(y_pred, R.Tensor): y_pred = R.Tensor(y_pred) scalar1 = R.Scalar(1) SS_res = R.sum(R.square(R.sub(y_true, y_pred))) SS_tot = R.sum(R.square(R.sub(y_true, R.mean(y_true)))) return R.sub(scalar1, R.div(SS_res, R.add(SS_tot, R.epsilon())))
def stat_info(self, X): """ Calculate mean and standard deviation """ for feature in zip(*X): feature = R.Tensor(list(feature), name = 'feature') std = R.std(feature) mean = R.mean(feature) yield { 'std': std, 'mean': mean }
def r2_score(y_true, y_pred): if isinstance(y_true, R.Tensor) or isinstance(y_true, R.Op): pass else: y_true = R.Tensor(y_true, name="y_true") if isinstance(y_pred, R.Tensor) or isinstance(y_pred, R.Op): pass else: y_pred = R.Tensor(y_pred, name="y_pred") print(type(y_true), type(y_pred)) scalar1 = R.Scalar(1) SS_res = R.sum(R.square(R.sub(y_pred, y_true)), name="ss_res") SS_tot = R.sum(R.square(R.sub(y_true, R.mean(y_true))), name="ss_tot") return R.sub(scalar1, R.div(SS_res, SS_tot), name="r2_score")
if average=='macro': for i in confusion: TP ,TN ,FP ,FN = i[0] ,i[1] ,i[2] ,i[3] Recall = R.div(TP, R.add(TP, FN)) Precision = R.div(TP, R.add(TP, FP)) if Precision == 0 or Recall == 0 or Recall == np.nan or Precision == np.nan: final.append(0) else: F1 = R.div(R.elemul(R.Scalar(2), R.elemul(Recall, Precision)),R.sum(Recall ,Precision)) final.append(F1) return R.mean(final) if average=='micro': confusion = R.Tensor(confusion) TP = R.sum(confusion ,axis=0)[0] TN = R.sum(confusion ,axis=0)[1] FP = R.sum(confusion ,axis=0)[2] FN = R.sum(confusion ,axis=0)[3] Recall = R.div(TP, R.add(TP, FN)) Precision = R.div(TP, R.add(TP, FP)) F1 = R.div(R.elemul(R.Scalar(2), R.elemul(Recall, Precision)),R.sum(Recall ,Precision)) return F1
import time import ravop.core as R from ravcom import inform_server a = R.Tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) b = R.Scalar(10) c = R.add(a, b) d = R.sub(a, b) e = R.multiply(a, b) f = R.mean(a) g = R.median(a) inform_server() # Wait for 10 seconds time.sleep(10) print(c()) print(d()) print(e()) print(f()) print(g())