def Accuracy(y_true, y_pred, multioutput=None): """ Calculate the accuracy score (Accuracy). .. math:: \\text{Accuracy} = \\frac{1}{n}\\sum_{t=1}^n 1(y_t=\\hat{y_t}) :param y_true: Array-like of shape = (n_samples, \*). Ground truth (correct) target values. :param y_pred: Array-like of shape = (n_samples, \*). Estimated target values. :return: Float or ndarray of floats. A non-negative floating point value (the best value is 1.0), or an array of floating point values, one for each individual target. """ from sklearn.metrics._classification import accuracy_score y_true = np.squeeze(y_true) y_pred = np.squeeze(y_pred) if np.any(y_pred != y_pred.astype(int)): # y_pred is probability if y_pred.ndim == 1: y_pred = np.where(y_pred > 0.5, 1, 0) else: y_pred = np.argmax(y_pred, axis=1) return accuracy_score(y_true, y_pred)
def Accuracy(y_true, y_pred, multioutput=None): from sklearn.metrics._classification import accuracy_score y_true = np.squeeze(y_true) y_pred = np.squeeze(y_pred) if np.any(y_pred != y_pred.astype(int)): # y_pred is probability if y_pred.ndim == 1: y_pred = np.where(y_pred > 0.5, 1, 0) else: y_pred = np.argmax(y_pred, axis=1) return accuracy_score(y_true, y_pred)
def experiment_transductive(name='E(1)', exp_index=0, preprocessing='none'): """ performs transductive experiment for frame/target pair parameters: name: image name exp_index: no. experiment (e.g. 0-9) preprocessing: preprocessing mode returns: True/False """ start = time.time() outfile = 'results/{}_{}_{}.npz'.format(name, exp_index, preprocessing) data, train_gt, test_gt, _ = load_ds(name, exp_index) #preprocessing X = data.reshape(-1, data.shape[2]) X = preprocess(X, preprocessing=preprocessing) data = X.reshape(data.shape) X_train = data[train_gt > 0] y_train = train_gt[train_gt > 0] time_prepare = time.time() - start start = time.time() svm = TwoStageSVM() svm.fit(X_train, y_train) time_train = time.time() - start X_test = data[test_gt > 0] y_test = test_gt[test_gt > 0] start = time.time() y_pred = svm.predict(X_test) time_test = time.time() - start y_pred_all = svm.predict(X) acc = accuracy_score(y_test, y_pred) np.savez_compressed(outfile, y_test=y_test, y_pred=y_pred, sh=data.shape, best_params=[svm.params_['C'], svm.params_['gamma']], acc=[acc], cls_score=[svm.score_], y_pred_all=y_pred_all, time_train=time_train, time_test=time_test, time_prepare=time_prepare) print(outfile, acc, time_train, time_test)
def Accuracy(y_true, y_pred, multioutput=None): from sklearn.metrics._classification import accuracy_score return accuracy_score(y_true, y_pred)
def experiment_inductive(name_train='hyperblood_frame_day_1_afternoon', name_test='hyperblood_comparison_day_1', exp_index=0, preprocessing='none'): """ performs inductive experiment for frame/target pair parameters: name_train: image name (for training) name_test: image name (for testing) exp_index: no. experiment (e.g. 0-9) preprocessing: preprocessing mode """ print(name_train, name_test) start = time.time() outfile = 'results_i/{}_{}_{}_{}.npz'.format(name_train, name_test, exp_index, preprocessing) data_train, train_gt, _, _ = load_ds(name_train, exp_index) data_test, _, test_gt, _ = load_ds(name_test, exp_index) _, anno_test, _, _, _ = blood_loader(PATH_DATA, name_test) test_gt = np.asarray(test_gt, dtype=np.int32) anno_test = np.asarray(anno_test, dtype=np.int32) assert np.sum(np.unique(anno_test) - np.unique(test_gt)) == 0 #preprocessing X = data_train.reshape(-1, data_train.shape[2]) X = preprocess(X, preprocessing=preprocessing) data_train = X.reshape(data_train.shape) X = data_test.reshape(-1, data_test.shape[2]) X = preprocess(X, preprocessing=preprocessing) data_test = X.reshape(data_test.shape) X_train = data_train[train_gt > 0] y_train = train_gt[train_gt > 0] time_prepare = time.time() - start start = time.time() svm = TwoStageSVM() svm.fit(X_train, y_train) time_train = time.time() - start #second version of results X_test_gt = data_test.copy()[anno_test > 0] y_test_gt = anno_test.copy()[anno_test > 0] X_test = data_test[test_gt > 0] y_test = test_gt[test_gt > 0] y_pred = svm.predict(X_test) y_pred_all = svm.predict(X) start = time.time() y_pred_gt = svm.predict(X_test_gt) time_test = time.time() - start acc = accuracy_score(y_test, y_pred) acc_gt = accuracy_score(y_test_gt, y_pred_gt) np.savez_compressed(outfile, y_test=y_test, y_pred=y_pred, sh_train=data_train.shape, sh_test=data_test.shape, best_params=[svm.params_['C'], svm.params_['gamma']], acc=[acc], cls_score=[svm.score_], y_pred_all=y_pred_all, y_test_gt=y_test_gt, y_pred_gt=y_pred_gt, acc_gt=[acc_gt], time_train=time_train, time_test=time_test, time_prepare=time_prepare) print(outfile, acc, acc_gt, time_train, time_test)