|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | +# @Filename: SemiSupervised.py |
| 4 | +# @Author: Daniel Puente Ramírez |
| 5 | +# @Time: 16/4/22 00:22 |
| 6 | + |
| 7 | +import random |
| 8 | + |
| 9 | +import pytest |
| 10 | +import numpy as np |
| 11 | +import pandas as pd |
| 12 | +from sklearn.datasets import load_iris as load_digits |
| 13 | +from sklearn.model_selection import train_test_split |
| 14 | + |
| 15 | +from semisupervised import STDPNF, CoTraining, TriTraining, \ |
| 16 | + DemocraticCoLearning |
| 17 | + |
| 18 | + |
| 19 | +def to_dataframe(y): |
| 20 | + if not isinstance(y, pd.DataFrame): |
| 21 | + return pd.DataFrame(y) |
| 22 | + return y |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def digits_dataset_ss(): |
| 27 | + x, y = load_digits(return_X_y=True, as_frame=True) |
| 28 | + x = x.to_numpy() |
| 29 | + y = y.to_numpy() |
| 30 | + opt_labels = np.unique(y) |
| 31 | + x_train, x_test, y_train, y_test = train_test_split( |
| 32 | + x, y, train_size=0.75, stratify=y, random_state=42 |
| 33 | + ) |
| 34 | + x_train = pd.DataFrame(x_train) |
| 35 | + x_test = pd.DataFrame(x_test) |
| 36 | + y_train = pd.DataFrame(y_train) |
| 37 | + y_test = pd.DataFrame(y_test) |
| 38 | + li = list(set(range(x_train.shape[0]))) |
| 39 | + unlabeled = random.sample(li, int(x_train.shape[0] * 0.3)) |
| 40 | + y_train.loc[unlabeled] = -1 |
| 41 | + |
| 42 | + return x_train, x_test, y_train, y_test, opt_labels |
| 43 | + |
| 44 | + |
| 45 | +def base(x_train, x_test, y_train, y_test, opt_labels, algorithm, params=None): |
| 46 | + assert isinstance(x_train, pd.DataFrame) and isinstance(y_train, |
| 47 | + pd.DataFrame) |
| 48 | + assert isinstance(x_test, pd.DataFrame) and isinstance(y_test, |
| 49 | + pd.DataFrame) |
| 50 | + model = algorithm(**params) if params is not None else algorithm() |
| 51 | + |
| 52 | + model.fit(x_train, y_train) |
| 53 | + y_pred = model.predict(x_test) |
| 54 | + |
| 55 | + assert set(y_pred).issubset(opt_labels) |
| 56 | + |
| 57 | + |
| 58 | +def test_co_training(digits_dataset_ss): |
| 59 | + x_train, x_test, y_train, y_test, opt_labels = digits_dataset_ss |
| 60 | + base(x_train, x_test, y_train, y_test, opt_labels, CoTraining) |
| 61 | + |
| 62 | + |
| 63 | +def test_tri_training(digits_dataset_ss): |
| 64 | + x_train, x_test, y_train, y_test, opt_labels = digits_dataset_ss |
| 65 | + base(x_train, x_test, y_train, y_test, opt_labels, TriTraining) |
| 66 | + |
| 67 | + |
| 68 | +def test_demo_co_learning(digits_dataset_ss): |
| 69 | + x_train, x_test, y_train, y_test, opt_labels = digits_dataset_ss |
| 70 | + base(x_train, x_test, y_train, y_test, opt_labels, DemocraticCoLearning) |
| 71 | + |
| 72 | + |
| 73 | +def test_density_peaks(digits_dataset_ss): |
| 74 | + x_train, x_test, y_train, y_test, opt_labels = digits_dataset_ss |
| 75 | + base(x_train, x_test, y_train, y_test, opt_labels, STDPNF) |
0 commit comments