Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
2
Indexable
Never
from typing import Union, Optional

class Dataset:
    def __init__(self, rows: list, columns: list):
        self.rows = rows
        self.columns = columns

    @property
    def number_of_rows(self) -> int:
        return len(self.rows)

    @property
    def number_of_columns(self) -> int:
        return len(self.columns)

    def split(self, ratio: float) -> ('Dataset', 'Dataset'): pass

class Datasets:
    def load_iris(self) -> Dataset: pass
    def load_digits(self) -> Dataset: pass
    def load_from_file(self, filename: str, path: Optional[str] = None) -> Dataset: pass

class MLModel:
    def fit(
            self,
            training_data: Union[Dataset, list],
            target_values: list,
            sample_weight: Optional[list] = None
    ) -> 'MLModel': pass

    def predict(self, sample: list) -> list: pass



def test_split_01():
    ds_iris = Datasets().load_iris()
    assert ds_iris.split(0.1)[0].number_of_rows == ds_iris.number_of_rows * 0.1

    # invalid
    assert ds_iris.split(-7.0)[0] == None
    assert ds_iris.split(10.0)[0] == None
    assert ds_iris.split(0.0001)[0] == None

def test_split_02():
    ds_custom_dataset = Datasets().load_from_file(filename="credit_risk.csv")
    assert ds_custom_dataset != None
    assert ds_custom_dataset.number_of_columns == 15
    assert ds_custom_dataset.split(0.5)[0].number_of_rows == ds_custom_dataset.number_of_rows * 0.5

def test_split_03():
    ds_iris = Datasets().load_digits()
    assert ds_iris.number_of_rows == 30

def test_model_prediction():
    ds_iris = Datasets().load_iris()
    assert MLModel().fit(ds_iris, [1, 0, 1]).predict([2, 0, 3]) == [0.1, 0.3, 0.12]

def test_model_prediction_negative():
    try:
        MLModel().predict([2, 0, 3])
    except Exception:
        print("Predict method called without fit")