forked from ibis-project/ibis-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
70 additions
and
0 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
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,37 @@ | ||
import random | ||
|
||
import ibis | ||
import ibis.expr.types as ir | ||
from ibis import _ | ||
|
||
|
||
def train_test_split( | ||
table: ir.Table, | ||
unique_key: str | list[str], | ||
test_size: float = 0.25, | ||
random_state=42, | ||
) -> tuple[ir.Table, ir.Table]: | ||
|
||
if not (0 < test_size < 1): | ||
raise ValueError("test size should be a float between 0 and 1.") | ||
|
||
# Set the random seed for reproducibility | ||
random.seed(random_state) | ||
# Generate a random 256-bit key | ||
random_key = str(random.getrandbits(256)) | ||
# set the number of buckets | ||
num_buckets = 100000 | ||
|
||
if isinstance(unique_key, str): | ||
unique_key = [unique_key] | ||
|
||
table = table.mutate( | ||
combined_key=ibis.literal("").join(table[col].cast("str") for col in unique_key) | ||
).mutate( | ||
train=(_.combined_key + random_key).hash().abs() % num_buckets | ||
< int((1 - test_size) * num_buckets) | ||
) | ||
|
||
return table[table.train].drop(["combined_key"]), table[~table.train].drop( | ||
["combined_key"] | ||
) |
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,32 @@ | ||
import ibis | ||
import pandas.testing as tm | ||
|
||
import ibis_ml as ml | ||
|
||
|
||
def test_train_test_split(): | ||
N = 100 | ||
test_size = 0.25 | ||
table = ibis.memtable({"key1": range(N)}) | ||
|
||
train_table, test_table = ml.train_test_split( | ||
table, unique_key="key1", test_size=test_size, random_state=42 | ||
) | ||
|
||
# Check counts and overlaps in train and test dataset | ||
assert train_table.count().execute() + test_table.count().execute() == N | ||
assert train_table.intersect(test_table).count().execute() == 0 | ||
|
||
# Check reproducibility | ||
reproduced_train_table, reproduced_test_table = ml.train_test_split( | ||
table, unique_key="key1", test_size=test_size, random_state=42 | ||
) | ||
tm.assert_frame_equal(train_table.execute(), reproduced_train_table.execute()) | ||
tm.assert_frame_equal(test_table.execute(), reproduced_test_table.execute()) | ||
|
||
# make sure it could generate different data with different random state | ||
different_train_table, different_test_table = ml.train_test_split( | ||
table, unique_key="key1", test_size=test_size, random_state=0 | ||
) | ||
assert not train_table.execute().equals(different_train_table.execute()) | ||
assert not test_table.execute().equals(different_test_table.execute()) |