-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
87 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import numpy as np | ||
|
||
from torch_frame.utils.split import SPLIT_TO_NUM, generate_random_split | ||
|
||
|
||
def test_generate_random_split(): | ||
num_data = 20 | ||
train_ratio = 0.8 | ||
val_ratio = 0.1 | ||
test_ratio = 0.1 | ||
|
||
split = generate_random_split(num_data, seed=42, train_ratio=train_ratio, | ||
val_ratio=val_ratio) | ||
assert (split == SPLIT_TO_NUM['train']).sum() == int(num_data * | ||
train_ratio) | ||
assert (split == SPLIT_TO_NUM['val']).sum() == int(num_data * val_ratio) | ||
assert (split == SPLIT_TO_NUM['test']).sum() == int(num_data * test_ratio) | ||
assert np.allclose( | ||
split, | ||
np.array([0, 1, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0]), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
from .io import save, load | ||
from .concat import cat | ||
from .split import generate_random_split | ||
|
||
__all__ = functions = [ | ||
'save', | ||
'load', | ||
'cat', | ||
'generate_random_split', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import numpy as np | ||
|
||
SPLIT_TO_NUM = {'train': 0, 'val': 1, 'test': 2} | ||
|
||
|
||
def generate_random_split(length: int, seed: int, train_ratio: float = 0.8, | ||
val_ratio: float = 0.1) -> np.ndarray: | ||
r"""Generate a list of random split assignments of the specified length. | ||
The elements are either :obj:`0`, :obj:`1`, or :obj:`2`, representing | ||
train, val, test, respectively. Note that this relies on the fact that | ||
numpy's shuffle is consistent across versions, which has been historically | ||
the case.""" | ||
assert train_ratio + val_ratio < 1 | ||
assert train_ratio > 0 | ||
assert train_ratio > 0 | ||
train_num = int(length * train_ratio) | ||
val_num = int(length * val_ratio) | ||
test_num = length - train_num - val_num | ||
|
||
arr = np.concatenate([ | ||
np.full(train_num, SPLIT_TO_NUM['train']), | ||
np.full(val_num, SPLIT_TO_NUM['val']), | ||
np.full(test_num, SPLIT_TO_NUM['test']) | ||
]) | ||
np.random.seed(seed) | ||
np.random.shuffle(arr) | ||
|
||
return arr |