def test_logistic_regression_model_train_evaluate(): data, labels = load_iris(return_X_y=True) randomize = np.arange(len(labels)) np.random.shuffle(randomize) data = data[randomize, ] labels = labels[randomize] dim = 100 train_data = data[0:dim, ] train_labels = labels[0:dim] test_data = data[dim:, ] test_labels = labels[dim:] model = LogisticRegression(max_iter=150) lgr = LinearClassifierModel(n_features=np.shape(train_data)[1], classes=np.unique(train_labels), model=model) lgr.train(data=train_data, labels=train_labels) evaluation = np.array(lgr.evaluate(data=test_data, labels=test_labels)) performance = lgr.performance(data=test_data, labels=test_labels) prediction = lgr.predict(data=test_data) model_params = lgr.get_model_params() lgr_ref = LogisticRegression(max_iter=150).fit(train_data, train_labels) prediction_ref = lgr_ref.predict(test_data) assert np.array_equal(model_params[0], lgr_ref.intercept_) assert np.array_equal(model_params[1], lgr_ref.coef_) assert np.array_equal(prediction, prediction_ref) assert np.array_equal(evaluation, np.array((metrics.balanced_accuracy_score(test_labels, prediction_ref),\ metrics.cohen_kappa_score(test_labels, prediction_ref)))) assert performance == metrics.balanced_accuracy_score( test_labels, prediction_ref)
def test_linearSVC_model_train_evaluate(): data, labels = load_iris(return_X_y=True) randomize = np.arange(len(labels)) np.random.shuffle(randomize) data = data[randomize, ] labels = labels[randomize] dim = 100 train_data = data[0:dim, ] train_labels = labels[0:dim] test_data = data[dim:, ] test_labels = labels[dim:] model = LinearSVC(random_state=123) svc = LinearClassifierModel(n_features=np.shape(train_data)[1], classes=np.unique(train_labels), model=model) svc.train(data=train_data, labels=train_labels) evaluation = np.array(svc.evaluate(data=test_data, labels=test_labels)) performance = svc.performance(data=test_data, labels=test_labels) prediction = svc.predict(data=test_data) model_params = svc.get_model_params() svc_ref = LinearSVC(random_state=123).fit(train_data, train_labels) prediction_ref = svc_ref.predict(test_data) assert np.array_equal(model_params[0], svc_ref.intercept_) assert np.array_equal(model_params[1], svc_ref.coef_) assert np.array_equal(prediction, prediction_ref) assert np.array_equal( evaluation, np.array((metrics.balanced_accuracy_score(test_labels, prediction_ref), metrics.cohen_kappa_score(test_labels, prediction_ref)))) assert performance == metrics.balanced_accuracy_score( test_labels, prediction_ref)