.
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
import numpy as np
|
||||
|
||||
|
||||
class Preprocessor:
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def fit(self, X, Y=None):
|
||||
pass
|
||||
|
||||
def transform(self, X):
|
||||
pass
|
||||
|
||||
def fit_transform(self, X, Y=None):
|
||||
pass
|
||||
|
||||
|
||||
class MyOneHotEncoder(Preprocessor):
|
||||
|
||||
def __init__(self, dtype=np.float64):
|
||||
super(Preprocessor).__init__()
|
||||
self.dtype = dtype
|
||||
self.types = {}
|
||||
|
||||
def fit(self, X, Y=None):
|
||||
"""
|
||||
param X: training objects, pandas-dataframe, shape [n_objects, n_features]
|
||||
param Y: unused
|
||||
"""
|
||||
for col in X.columns:
|
||||
self.types[col] = sorted(X[col].unique())
|
||||
|
||||
def transform(self, X):
|
||||
"""
|
||||
param X: objects to transform, pandas-dataframe, shape [n_objects, n_features]
|
||||
returns: transformed objects, numpy-array, shape [n_objects, |f1| + |f2| + ...]
|
||||
"""
|
||||
n_objects = X.shape[0]
|
||||
n_features = sum(len(categories) for categories in self.types.vals())
|
||||
res = np.zeros((n_objects, n_features))
|
||||
|
||||
shift_indexes = 0
|
||||
for column, categories in self.types.items():
|
||||
for i, category in enumerate(categories):
|
||||
indices = np.where(X[column] == category)
|
||||
res[indices, shift_indexes + i] = 1
|
||||
shift_indexes += len(categories)
|
||||
|
||||
return res
|
||||
|
||||
def fit_transform(self, X, Y=None):
|
||||
self.fit(X)
|
||||
return self.transform(X)
|
||||
|
||||
def get_params(self, deep=True):
|
||||
return {"dtype": self.dtype}
|
||||
|
||||
|
||||
class SimpleCounterEncoder:
|
||||
|
||||
def __init__(self, dtype=np.float64):
|
||||
self.dtype = dtype
|
||||
self.count = {}
|
||||
|
||||
def fit(self, X, Y):
|
||||
"""
|
||||
param X: training objects, pandas-dataframe, shape [n_objects, n_features]
|
||||
param Y: target for training objects, pandas-series, shape [n_objects,]
|
||||
"""
|
||||
for col in X.columns:
|
||||
uniq_vals = X[col].unique()
|
||||
self.count[col] = {}
|
||||
|
||||
for val in uniq_vals:
|
||||
indexes = X[col] == val
|
||||
self.count[col][val] = [Y[indexes].mean(), np.mean(indexes)]
|
||||
|
||||
def transform(self, X, a=1e-5, b=1e-5):
|
||||
"""
|
||||
param X: objects to transform, pandas-dataframe, shape [n_objects, n_features]
|
||||
param a: constant for counters, float
|
||||
param b: constant for counters, float
|
||||
returns: transformed objects, numpy-array, shape [n_objects, 3 * n_features]
|
||||
"""
|
||||
n_obj, n_feach = X.shape
|
||||
res = np.zeros((n_obj, 3 * n_feach))
|
||||
|
||||
for i, col in enumerate(X.columns):
|
||||
for j in range(n_obj):
|
||||
val = X.iloc[j, i]
|
||||
mean_expected, frac = self.count[col][val]
|
||||
res[j, 3 * i] = mean_expected
|
||||
res[j, 3 * i + 1] = frac
|
||||
res[j, 3 * i + 2] = (mean_expected + a) / (frac + b)
|
||||
|
||||
return res
|
||||
|
||||
def fit_transform(self, X, Y, a=1e-5, b=1e-5):
|
||||
self.fit(X, Y)
|
||||
return self.transform(X, a, b)
|
||||
|
||||
def get_params(self, deep=True):
|
||||
return {"dtype": self.dtype}
|
||||
|
||||
|
||||
def group_k_fold(size, n_splits=3, seed=1):
|
||||
idx = np.arange(size)
|
||||
np.random.seed(seed)
|
||||
idx = np.random.permutation(idx)
|
||||
n = size // n_splits
|
||||
|
||||
for i in range(n_splits - 1):
|
||||
yield idx[i * n:(i + 1) * n], np.hstack((idx[: i * n], idx[(i + 1) * n:]))
|
||||
yield idx[(n_splits - 1) * n:], idx[:(n_splits - 1) * n]
|
||||
|
||||
|
||||
class FoldCounters:
|
||||
|
||||
def __init__(self, n_folds=3, dtype=np.float64):
|
||||
self.dtype = dtype
|
||||
self.n_folds = n_folds
|
||||
self.fold_count = []
|
||||
|
||||
def fit(self, X, Y, seed=1):
|
||||
"""
|
||||
param X: training objects, pandas-dataframe, shape [n_objects, n_features]
|
||||
param Y: target for training objects, pandas-series, shape [n_objects,]
|
||||
param seed: random seed, int
|
||||
"""
|
||||
for fold_idx, rest_idx in group_k_fold(X.shape[0], self.n_folds, seed):
|
||||
fold_counter = {}
|
||||
X_fold, Y_fold = X.iloc[rest_idx], Y.iloc[rest_idx]
|
||||
|
||||
for column in X.columns:
|
||||
unique_val = X_fold[column].unique()
|
||||
fold_counter[column] = {}
|
||||
|
||||
for val in unique_val:
|
||||
fold_counter[column][val] = [
|
||||
Y_fold[X_fold[column] == val].mean(),
|
||||
np.mean(X_fold[column] == val),
|
||||
]
|
||||
|
||||
self.fold_count.append((fold_idx, fold_counter))
|
||||
|
||||
def transform(self, X, a=1e-5, b=1e-5):
|
||||
"""
|
||||
param X: objects to transform, pandas-dataframe, shape [n_objects, n_features]
|
||||
param a: constant for counters, float
|
||||
param b: constant for counters, float
|
||||
returns: transformed objects, numpy-array, shape [n_objects, 3 * n_features]
|
||||
"""
|
||||
n_obj, n_feach = X.shape
|
||||
res = np.zeros((n_obj, 3 * n_feach))
|
||||
|
||||
for fold_idx, fold_count in self.fold_count:
|
||||
for i, column in enumerate(X.columns):
|
||||
for j in fold_idx:
|
||||
val = X.iloc[j, i]
|
||||
mean_expected, frac = fold_count[column][val]
|
||||
|
||||
res[j, 3 * i] = mean_expected
|
||||
res[j, 3 * i + 1] = frac
|
||||
res[j, 3 * i + 2] = (mean_expected + a) / (frac + b)
|
||||
|
||||
return res
|
||||
|
||||
def fit_transform(self, X, Y, a=1e-5, b=1e-5):
|
||||
self.fit(X, Y)
|
||||
|
||||
return self.transform(X, a, b)
|
||||
|
||||
|
||||
def weights(x, y):
|
||||
"""
|
||||
param x: training set of one feature, numpy-array, shape [n_objects,]
|
||||
param y: target for training objects, numpy-array, shape [n_objects,]
|
||||
returns: optimal weights, numpy-array, shape [|x unique vals|,]
|
||||
"""
|
||||
uniq_vals = np.unique(x)
|
||||
enc_x = np.eye(uniq_vals.shape[0])[x]
|
||||
weight = np.zeros(enc_x.shape[1])
|
||||
lr = 1e-2
|
||||
|
||||
for _ in range(1000):
|
||||
p = np.dot(enc_x, weight)
|
||||
grad = np.dot(enc_x.T, (p - y))
|
||||
weight -= grad * lr
|
||||
|
||||
return weight
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,23 @@
|
||||
set -o xtrace
|
||||
|
||||
setup_root() {
|
||||
apt-get install python3.12 -qq -y
|
||||
apt-get install -qq -y \
|
||||
python3-pip \
|
||||
python3-tk
|
||||
|
||||
python3 --version
|
||||
|
||||
pip install --upgrade pip
|
||||
pip --version
|
||||
|
||||
echo -e "catboost==1.2.8\ngdown==5.2.0\nh5py==3.14.0\nhyperopt==0.2.7\nipympl==0.9.7\nipywidgets==7.7.1\nlightgbm==4.6.0\nmatplotlib-inline==0.1.7\nmatplotlib==3.10.0\nnumpy==2.0.2\npandas==2.2.2\npep8==1.7.1\nplotly==5.24.1\npycodestyle==2.14.0\npytest==8.4.1\nscikit-image==0.25.2\nscikit-learn==1.6.1\nscipy==1.16.1\nseaborn==0.13.2\ntqdm==4.67.1\numap-learn==0.5.9.post2\nxgboost==3.0.4" > requirements.txt
|
||||
|
||||
pip install -r ./requirements.txt
|
||||
}
|
||||
|
||||
setup_checker() {
|
||||
python3 -c 'import matplotlib.pyplot'
|
||||
}
|
||||
|
||||
"$@"
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,22 @@
|
||||
catboost==1.2.8
|
||||
gdown==5.2.0
|
||||
h5py==3.14.0
|
||||
hyperopt==0.2.7
|
||||
ipympl==0.9.7
|
||||
ipywidgets==7.7.1
|
||||
lightgbm==4.6.0
|
||||
matplotlib-inline==0.1.7
|
||||
matplotlib==3.10.0
|
||||
numpy==2.0.2
|
||||
pandas==2.2.2
|
||||
pep8==1.7.1
|
||||
plotly==5.24.1
|
||||
pycodestyle==2.14.0
|
||||
pytest==8.4.1
|
||||
scikit-image==0.25.2
|
||||
scikit-learn==1.6.1
|
||||
scipy==1.16.1
|
||||
seaborn==0.13.2
|
||||
tqdm==4.67.1
|
||||
umap-learn==0.5.9.post2
|
||||
xgboost==3.0.4
|
||||
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from json import load, dumps
|
||||
from glob import glob
|
||||
from os import environ
|
||||
from os.path import join
|
||||
from sys import argv, exit
|
||||
|
||||
|
||||
def run_single_test(data_dir, output_dir):
|
||||
from pytest import main
|
||||
exit(main(['-vv', '-p', 'no:cacheprovider', join(data_dir, 'test.py')]))
|
||||
|
||||
|
||||
def check_test(data_dir):
|
||||
pass
|
||||
|
||||
|
||||
def grade(data_path):
|
||||
results = load(open(join(data_path, 'results.json')))
|
||||
grade_mapping = [0.5, 0.5, 1, 1]
|
||||
max_mark = sum(grade_mapping)
|
||||
total_grade = 0
|
||||
ok_count = 0
|
||||
for result, grade in zip(results, grade_mapping):
|
||||
if result['status'] == 'Ok':
|
||||
total_grade += grade
|
||||
ok_count += 1
|
||||
total_count = len(results)
|
||||
description = '%02d/%02d' % (ok_count, total_count)
|
||||
mark = total_grade / sum(grade_mapping) * max_mark
|
||||
res = {'description': description, 'mark': mark}
|
||||
if environ.get('CHECKER'):
|
||||
print(dumps(res))
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if environ.get('CHECKER'):
|
||||
# Script is running in testing system
|
||||
if len(argv) != 4:
|
||||
print('Usage: %s mode data_dir output_dir' % argv[0])
|
||||
exit(0)
|
||||
|
||||
mode = argv[1]
|
||||
data_dir = argv[2]
|
||||
output_dir = argv[3]
|
||||
|
||||
if mode == 'run_single_test':
|
||||
run_single_test(data_dir, output_dir)
|
||||
elif mode == 'check_test':
|
||||
check_test(data_dir)
|
||||
elif mode == 'grade':
|
||||
grade(data_dir)
|
||||
else:
|
||||
# Script is running locally
|
||||
name = argv[1]
|
||||
test_dir = glob(f'tests/[0-9][0-9]_unittest_{name}_input')
|
||||
if not test_dir:
|
||||
print('Test not found')
|
||||
exit(0)
|
||||
|
||||
from pytest import main
|
||||
exit(main(['-vv', join(test_dir[0], 'test.py')]))
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,67 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from Task import MyOneHotEncoder, SimpleCounterEncoder, FoldCounters, weights
|
||||
|
||||
def test_imports():
|
||||
with open('Task.py', 'r') as file:
|
||||
lines = ' '.join(file.readlines())
|
||||
assert 'import numpy' in lines
|
||||
assert lines.count('import') == 1
|
||||
assert 'sklearn' not in lines
|
||||
assert 'get_dummies' not in lines
|
||||
|
||||
def test_one_hot_small():
|
||||
data = {'col_1': [0, 1, 0, 1, 0, 1], 'col_2': ['a', 'b', 'c', 'c', 'b', 'a']}
|
||||
df_test = pd.DataFrame.from_dict(data)
|
||||
enc = MyOneHotEncoder(dtype=int)
|
||||
enc.fit(df_test)
|
||||
onehot = enc.transform(df_test)
|
||||
ans = np.array([[1,0,1,0,0],[0,1,0,1,0],[1,0,0,0,1],[0,1,0,0,1],[1,0,0,1,0],[0,1,1,0,0]])
|
||||
assert len(onehot.shape) == 2
|
||||
assert onehot.shape[0] == 6
|
||||
assert onehot.shape[1] == 5
|
||||
assert (ans == onehot).all()
|
||||
assert type(onehot) == np.ndarray
|
||||
|
||||
def test_one_hot_big():
|
||||
data = {'col_1': [1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 2, 1, 2, 0, 2, 1, 2, 0, 0, 2, 0, 1, 2, 2, 0, 1, 1, 2, 0], 'col_2': [1, 1, 1, 1, 0, 4, 1, 0, 0, 3, 2, 1, 0, 3, 1, 1, 3, 4, 0, 1, 3, 4, 2, 4, 0, 3, 1, 2, 0, 4], 'col_3': [1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]}
|
||||
df_test = pd.DataFrame.from_dict(data)
|
||||
enc = MyOneHotEncoder(dtype=int)
|
||||
enc.fit(df_test)
|
||||
onehot = enc.transform(df_test)
|
||||
ans = np.array([[0, 1, 0, 0, 1, 0, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
|
||||
[0, 1, 0, 0, 1, 0, 0, 0, 0, 1],
|
||||
[0, 1, 0, 1, 0, 0, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 0, 0, 0, 1, 1, 0],
|
||||
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
|
||||
[0, 1, 0, 1, 0, 0, 0, 0, 1, 0],
|
||||
[1, 0, 0, 1, 0, 0, 0, 0, 1, 0],
|
||||
[0, 1, 0, 0, 0, 0, 1, 0, 0, 1],
|
||||
[1, 0, 0, 0, 0, 1, 0, 0, 0, 1],
|
||||
[0, 0, 1, 0, 1, 0, 0, 0, 0, 1],
|
||||
[0, 1, 0, 1, 0, 0, 0, 0, 1, 0],
|
||||
[0, 0, 1, 0, 0, 0, 1, 0, 0, 1],
|
||||
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
|
||||
[0, 0, 1, 0, 1, 0, 0, 0, 1, 0],
|
||||
[0, 1, 0, 0, 0, 0, 1, 0, 0, 1],
|
||||
[0, 0, 1, 0, 0, 0, 0, 1, 0, 1],
|
||||
[1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
|
||||
[0, 0, 1, 0, 0, 0, 1, 0, 1, 0],
|
||||
[1, 0, 0, 0, 0, 0, 0, 1, 1, 0],
|
||||
[0, 1, 0, 0, 0, 1, 0, 0, 1, 0],
|
||||
[0, 0, 1, 0, 0, 0, 0, 1, 1, 0],
|
||||
[0, 0, 1, 1, 0, 0, 0, 0, 1, 0],
|
||||
[1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
|
||||
[0, 1, 0, 0, 1, 0, 0, 0, 0, 1],
|
||||
[0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
|
||||
[0, 0, 1, 1, 0, 0, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 0, 0, 0, 1, 0, 1]])
|
||||
assert len(onehot.shape) == 2
|
||||
assert onehot.shape[0] == 30
|
||||
assert onehot.shape[1] == 10
|
||||
assert (onehot == ans).all()
|
||||
assert type(onehot) == np.ndarray
|
||||
@@ -0,0 +1,32 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from Task import MyOneHotEncoder, SimpleCounterEncoder, FoldCounters, weights
|
||||
|
||||
def test_imports():
|
||||
with open('Task.py', 'r') as file:
|
||||
lines = ' '.join(file.readlines())
|
||||
assert 'import numpy' in lines
|
||||
assert lines.count('import') == 1
|
||||
assert 'sklearn' not in lines
|
||||
assert 'get_dummies' not in lines
|
||||
|
||||
def test_weights_small():
|
||||
np.random.seed(1)
|
||||
x = np.array([1, 1, 1, 1, 0, 4, 1, 0, 0, 3, 2, 1, 0, 3, 1, 1, 3, 4, 0, 1, 3, 4, 2, 4, 0, 3, 1, 2, 0, 4])
|
||||
y = np.array([1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0])
|
||||
w = weights(x, y)
|
||||
ans = [0.5714285714285714, 0.4, 0.6666666666666666, 1.0, 0.2]
|
||||
assert len(w) == 5
|
||||
assert np.allclose(w, ans, atol=1e-8)
|
||||
assert type(w) == np.ndarray
|
||||
|
||||
def test_weights_big():
|
||||
np.random.seed(1)
|
||||
x = np.random.choice([0, 1, 2, 3, 4, 5], size=(300,))
|
||||
y = np.random.choice([0, 1], size=(300,))
|
||||
w = weights(x, y)
|
||||
ans = [0.38596491228070173, 0.5384615384615384, 0.4523809523809524, 0.3409090909090909, 0.44642857142857145, 0.42857142857142855]
|
||||
assert len(w) == 6
|
||||
assert np.allclose(w, ans, atol=1e-8)
|
||||
assert type(w) == np.ndarray
|
||||
Binary file not shown.
@@ -0,0 +1,96 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from Task import SimpleCounterEncoder
|
||||
|
||||
def test_imports():
|
||||
with open('Task.py', 'r') as file:
|
||||
lines = ' '.join(file.readlines())
|
||||
assert 'import numpy' in lines
|
||||
assert lines.count('import') == 1
|
||||
assert 'sklearn' not in lines
|
||||
assert 'get_dummies' not in lines
|
||||
|
||||
def test_simple_counters_small():
|
||||
data = {'col_1': [0, 0, 0, 1, 1, 1], 'col_2': ['a', 'b', 'c', 'a', 'b', 'c'], 'col_3': [0, 1, 2, 3, 4, 5]}
|
||||
df_test = pd.DataFrame.from_dict(data)
|
||||
enc = SimpleCounterEncoder()
|
||||
enc.fit(df_test[['col_1', 'col_2']], df_test['col_3'])
|
||||
counts = enc.transform(df_test[['col_1', 'col_2']], a=1, b=1)
|
||||
|
||||
print("Counters small")
|
||||
print(counts)
|
||||
|
||||
ans = np.array([[1, 0.5, 4/3, 1.5, 1/3, 1.875],\
|
||||
[1, 0.5, 4/3, 2.5, 1/3, 2.625],\
|
||||
[1, 0.5, 4/3, 3.5, 1/3, 3.375],\
|
||||
[4, 0.5, 10/3, 1.5, 1/3, 1.875],\
|
||||
[4, 0.5, 10/3, 2.5, 1/3, 2.625],\
|
||||
[4, 0.5, 10/3, 3.5, 1/3, 3.375]])
|
||||
assert len(counts.shape) == 2
|
||||
assert counts.shape[0] == 6
|
||||
assert counts.shape[1] == 6
|
||||
assert np.allclose(counts, ans, atol=1e-8)
|
||||
assert type(counts) == np.ndarray
|
||||
|
||||
def test_simple_counters_diff_shape_small():
|
||||
data_enc = {'col_1': [0, 0, 0, 1, 1, 1], 'col_2': ['a', 'b', 'c', 'a', 'b', 'c'], 'col_3': [0, 1, 2, 3, 4, 5]}
|
||||
data_trans = {'col_1': [0, 1, 0, 1, 0], 'col_2': ['c', 'a', 'b', 'b', 'c']}
|
||||
df_test_enc = pd.DataFrame.from_dict(data_enc)
|
||||
df_test_trans = pd.DataFrame.from_dict(data_trans)
|
||||
enc = SimpleCounterEncoder()
|
||||
enc.fit(df_test_enc[['col_1', 'col_2']], df_test_enc['col_3'])
|
||||
counts = enc.transform(df_test_trans[['col_1', 'col_2']], a=1, b=1)
|
||||
ans = np.array([[1, 0.5, 4/3, 3.5, 1/3, 3.375],\
|
||||
[4, 0.5, 10/3, 1.5, 1/3, 1.875],\
|
||||
[1, 0.5, 4/3, 2.5, 1/3, 2.625],\
|
||||
[4, 0.5, 10/3, 2.5, 1/3, 2.625],\
|
||||
[1, 0.5, 4/3, 3.5, 1/3, 3.375]])
|
||||
assert len(counts.shape) == 2
|
||||
assert counts.shape[0] == 5
|
||||
assert counts.shape[1] == 6
|
||||
assert np.allclose(counts, ans, atol=1e-8)
|
||||
assert type(counts) == np.ndarray
|
||||
|
||||
def test_simple_counters_big():
|
||||
data = {'col_1': [1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 2, 1, 2, 0, 2, 1, 2, 0, 0, 2, 0, 1, 2, 2, 0, 1, 1, 2, 0], 'col_2': [1, 1, 1, 1, 0, 4, 1, 0, 0, 3, 2, 1, 0, 3, 1, 1, 3, 4, 0, 1, 3, 4, 2, 4, 0, 3, 1, 2, 0, 4], 'col_3': [1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], "target": [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0]}
|
||||
df_test = pd.DataFrame.from_dict(data)
|
||||
enc = SimpleCounterEncoder()
|
||||
enc.fit(df_test[['col_1', 'col_2', 'col_3']], df_test['target'])
|
||||
counts = enc.transform(df_test[['col_1', 'col_2', 'col_3']], a=1, b=2)
|
||||
ans = np.array([[0.6, 0.3333333333333333, 0.6857142857142857, 0.4, 0.3333333333333333, 0.6, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 0.4, 0.3333333333333333, 0.6, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 0.4, 0.3333333333333333, 0.6, 0.36363636363636365, 0.36666666666666664, 0.5761843790012805 ],\
|
||||
[0.6, 0.3333333333333333, 0.6857142857142857, 0.4, 0.3333333333333333, 0.6, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.6, 0.3333333333333333, 0.6857142857142857, 0.5714285714285714, 0.23333333333333334, 0.7036247334754797, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 0.2, 0.16666666666666666, 0.5538461538461539, 0.36363636363636365, 0.36666666666666664, 0.5761843790012805 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 0.4, 0.3333333333333333, 0.6, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.6, 0.3333333333333333, 0.6857142857142857, 0.5714285714285714, 0.23333333333333334, 0.7036247334754797, 0.36363636363636365, 0.36666666666666664, 0.5761843790012805 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 0.5714285714285714, 0.23333333333333334, 0.7036247334754797, 0.36363636363636365, 0.36666666666666664, 0.5761843790012805 ],\
|
||||
[0.6, 0.3333333333333333, 0.6857142857142857, 1.0, 0.16666666666666666, 0.9230769230769231, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 0.6666666666666666, 0.1, 0.7936507936507935, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.75, 0.26666666666666666, 0.7720588235294118, 0.4, 0.3333333333333333, 0.6, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.6, 0.3333333333333333, 0.6857142857142857, 0.5714285714285714, 0.23333333333333334, 0.7036247334754797, 0.36363636363636365, 0.36666666666666664, 0.5761843790012805 ],\
|
||||
[0.75, 0.26666666666666666, 0.7720588235294118, 1.0, 0.16666666666666666, 0.9230769230769231, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 0.4, 0.3333333333333333, 0.6, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.75, 0.26666666666666666, 0.7720588235294118, 0.4, 0.3333333333333333, 0.6, 0.36363636363636365, 0.36666666666666664, 0.5761843790012805 ],\
|
||||
[0.6, 0.3333333333333333, 0.6857142857142857, 1.0, 0.16666666666666666, 0.9230769230769231, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.75, 0.26666666666666666, 0.7720588235294118, 0.2, 0.16666666666666666, 0.5538461538461539, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 0.5714285714285714, 0.23333333333333334, 0.7036247334754797, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 0.4, 0.3333333333333333, 0.6, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.75, 0.26666666666666666, 0.7720588235294118, 1.0, 0.16666666666666666, 0.9230769230769231, 0.36363636363636365, 0.36666666666666664, 0.5761843790012805 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 0.2, 0.16666666666666666, 0.5538461538461539, 0.36363636363636365, 0.36666666666666664, 0.5761843790012805 ],\
|
||||
[0.6, 0.3333333333333333, 0.6857142857142857, 0.6666666666666666, 0.1, 0.7936507936507935, 0.36363636363636365, 0.36666666666666664, 0.5761843790012805 ],\
|
||||
[0.75, 0.26666666666666666, 0.7720588235294118, 0.2, 0.16666666666666666, 0.5538461538461539, 0.36363636363636365, 0.36666666666666664, 0.5761843790012805 ],\
|
||||
[0.75, 0.26666666666666666, 0.7720588235294118, 0.5714285714285714, 0.23333333333333334, 0.7036247334754797, 0.36363636363636365, 0.36666666666666664, 0.5761843790012805 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 1.0, 0.16666666666666666, 0.9230769230769231, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.6, 0.3333333333333333, 0.6857142857142857, 0.4, 0.3333333333333333, 0.6, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.6, 0.3333333333333333, 0.6857142857142857, 0.6666666666666666, 0.1, 0.7936507936507935, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.75, 0.26666666666666666, 0.7720588235294118, 0.5714285714285714, 0.23333333333333334, 0.7036247334754797, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ],\
|
||||
[0.3333333333333333, 0.4, 0.5555555555555556, 0.2, 0.16666666666666666, 0.5538461538461539, 0.631578947368421, 0.6333333333333333, 0.619586942038641 ]])
|
||||
assert len(counts.shape) == 2
|
||||
assert counts.shape[0] == 30
|
||||
assert counts.shape[1] == 9
|
||||
assert np.allclose(counts, ans, atol=1e-8)
|
||||
assert type(counts) == np.ndarray
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,103 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from Task import MyOneHotEncoder, SimpleCounterEncoder, FoldCounters, weights
|
||||
|
||||
def test_imports():
|
||||
with open('Task.py', 'r') as file:
|
||||
lines = ' '.join(file.readlines())
|
||||
assert 'import numpy' in lines
|
||||
assert lines.count('import') == 1
|
||||
assert 'sklearn' not in lines
|
||||
assert 'get_dummies' not in lines
|
||||
|
||||
def test_kfold_counters_small():
|
||||
data = {'col_1': [0,1,0,1,0,1,0,1,0,1,0,1], 'col_2':['a','b','c','a','b','c','a','b','c','a','b','c'], 'col_3': [1,2,3,4,1,2,3,4,1,2,3,4]}
|
||||
df_test = pd.DataFrame.from_dict(data)
|
||||
enc = FoldCounters(n_folds=2)
|
||||
enc.fit(df_test[['col_1', 'col_2']], df_test['col_3'], seed=6)
|
||||
counts = enc.transform(df_test[['col_1', 'col_2']], a=0, b=0)
|
||||
ans = np.array([[7/3,0.5,14/3,3,1/3,9],\
|
||||
[8/3,0.5,16/3,2,1/3,6],\
|
||||
[5/3,0.5,10/3,2.5,1/3,7.5],\
|
||||
[10/3,0.5,20/3,2,1/3,6],\
|
||||
[5/3,0.5,10/3,3,1/3,9],\
|
||||
[10/3,0.5,20/3,2.5,1/3,7.5],\
|
||||
[7/3,0.5,14/3,3,1/3,9],\
|
||||
[8/3,0.5,16/3,2,1/3,6],\
|
||||
[7/3,0.5,14/3,2.5,1/3,7.5],\
|
||||
[10/3,0.5,20/3,2,1/3,6],\
|
||||
[5/3,0.5,10/3,3,1/3,9],\
|
||||
[8/3,0.5,16/3,2.5,1/3,7.5]])
|
||||
assert len(counts.shape) == 2
|
||||
assert counts.shape[0] == 12
|
||||
assert counts.shape[1] == 6
|
||||
assert np.allclose(counts, ans, atol=1e-8)
|
||||
assert type(counts) == np.ndarray
|
||||
|
||||
def test_kfold_counters_diffshape_idx_small():
|
||||
data = np.array([[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1], [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]])
|
||||
index = pd.Index([11, 0, 9, 7, 6, 10, 1, 2, 5, 4, 3, 8])
|
||||
df_test = pd.DataFrame(data.reshape(12, 2), index=index, columns=['col_1', 'col_2'])
|
||||
enc = FoldCounters(n_folds=2)
|
||||
enc.fit(df_test[['col_1']], df_test['col_2'], seed=6)
|
||||
counts = enc.transform(df_test[['col_1']], a=0, b=0)
|
||||
ans = np.array([[ 1. , 0.66666667, 1.5 ],\
|
||||
[ 1. , 0.66666667, 1.5 ],\
|
||||
[ 1. , 0.33333333, 3. ],\
|
||||
[ 1. , 0.33333333, 3. ],\
|
||||
[ 1. , 0.33333333, 3. ],\
|
||||
[ 1. , 0.33333333, 3. ],\
|
||||
[ 2. , 0.16666667, 12. ],\
|
||||
[ 4. , 0.16666667, 24. ],\
|
||||
[ 2. , 0.16666667, 12. ],\
|
||||
[ 4. , 0.33333333, 12. ],\
|
||||
[ 2. , 0.33333333, 6. ],\
|
||||
[ 4. , 0.16666667, 24. ]])
|
||||
assert len(counts.shape) == 2
|
||||
assert counts.shape[0] == 12
|
||||
assert counts.shape[1] == 3
|
||||
assert np.allclose(counts, ans, atol=1e-8)
|
||||
assert type(counts) == np.ndarray
|
||||
|
||||
def test_fold_counters_big():
|
||||
data = {'col_1': [1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 2, 1, 2, 0, 2, 1, 2, 0, 0, 2, 0, 1, 2, 2, 0, 1, 1, 2, 0], 'col_2': [1, 1, 1, 1, 0, 4, 1, 0, 0, 3, 2, 1, 0, 3, 1, 1, 3, 4, 0, 1, 3, 4, 2, 4, 0, 3, 1, 2, 0, 4], 'col_3': [1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], "target": [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0]}
|
||||
df_test = pd.DataFrame.from_dict(data)
|
||||
enc = FoldCounters(n_folds=3)
|
||||
enc.fit(df_test[['col_1', 'col_2', 'col_3']], df_test['target'], seed=1)
|
||||
counts = enc.transform(df_test[['col_1', 'col_2', 'col_3']], a=1, b=2)
|
||||
ans = np.array([[0.3333333333333333, 0.3, 0.5797101449275363, 0.2857142857142857, 0.35, 0.547112462006079, 0.5384615384615384, 0.65, 0.5805515239477503 ],\
|
||||
[0.2857142857142857, 0.35, 0.547112462006079, 0.42857142857142855, 0.35, 0.60790273556231, 0.6666666666666666, 0.6, 0.641025641025641 ],\
|
||||
[0.2857142857142857, 0.35, 0.547112462006079, 0.42857142857142855, 0.35, 0.60790273556231, 0.5, 0.4, 0.625 ],\
|
||||
[0.7142857142857143, 0.35, 0.729483282674772, 0.5, 0.3, 0.6521739130434783, 0.6923076923076923, 0.65, 0.6386066763425254 ],\
|
||||
[0.7142857142857143, 0.35, 0.729483282674772, 1.0, 0.2, 0.9090909090909091, 0.6666666666666666, 0.6, 0.641025641025641 ],\
|
||||
[0.3333333333333333, 0.45, 0.5442176870748299, 0.3333333333333333, 0.15, 0.6201550387596899, 0.2857142857142857, 0.35, 0.547112462006079 ],\
|
||||
[0.2857142857142857, 0.35, 0.547112462006079, 0.42857142857142855, 0.35, 0.60790273556231, 0.6666666666666666, 0.6, 0.641025641025641 ],\
|
||||
[0.7142857142857143, 0.35, 0.729483282674772, 1.0, 0.2, 0.9090909090909091, 0.5, 0.4, 0.625 ],\
|
||||
[0.3333333333333333, 0.45, 0.5442176870748299, 0.25, 0.2, 0.5681818181818181, 0.2857142857142857, 0.35, 0.547112462006079 ],\
|
||||
[0.3333333333333333, 0.3, 0.5797101449275363, 1.0, 0.15, 0.9302325581395349, 0.5384615384615384, 0.65, 0.5805515239477503 ],\
|
||||
[0.375, 0.4, 0.5729166666666667, 1.0, 0.05, 0.9756097560975611, 0.6923076923076923, 0.65, 0.6386066763425254 ],\
|
||||
[0.8, 0.25, 0.8, 0.2857142857142857, 0.35, 0.547112462006079, 0.5384615384615384, 0.65, 0.5805515239477503 ],\
|
||||
[0.3333333333333333, 0.3, 0.5797101449275363, 0.25, 0.2, 0.5681818181818181, 0.2857142857142857, 0.35, 0.547112462006079 ],\
|
||||
[0.8333333333333334, 0.3, 0.7971014492753624, 1.0, 0.15, 0.9302325581395349, 0.6666666666666666, 0.6, 0.641025641025641 ],\
|
||||
[0.375, 0.4, 0.5729166666666667, 0.5, 0.3, 0.6521739130434783, 0.6923076923076923, 0.65, 0.6386066763425254 ],\
|
||||
[0.8, 0.25, 0.8, 0.2857142857142857, 0.35, 0.547112462006079, 0.2857142857142857, 0.35, 0.547112462006079 ],\
|
||||
[0.3333333333333333, 0.3, 0.5797101449275363, 1.0, 0.15, 0.9302325581395349, 0.5384615384615384, 0.65, 0.5805515239477503 ],\
|
||||
[0.6, 0.25, 0.7111111111111111, 0.0, 0.15, 0.46511627906976744, 0.6923076923076923, 0.65, 0.6386066763425254 ],\
|
||||
[0.2857142857142857, 0.35, 0.547112462006079, 1.0, 0.2, 0.9090909090909091, 0.6666666666666666, 0.6, 0.641025641025641 ],\
|
||||
[0.375, 0.4, 0.5729166666666667, 0.5, 0.3, 0.6521739130434783, 0.6923076923076923, 0.65, 0.6386066763425254 ],\
|
||||
[0.6, 0.25, 0.7111111111111111, 1.0, 0.2, 0.9090909090909091, 0.2857142857142857, 0.35, 0.547112462006079 ],\
|
||||
[0.375, 0.4, 0.5729166666666667, 0.0, 0.15, 0.46511627906976744, 0.2857142857142857, 0.35, 0.547112462006079 ],\
|
||||
[0.7142857142857143, 0.35, 0.729483282674772, 1.0, 0.05, 0.9756097560975611, 0.2857142857142857, 0.35, 0.547112462006079 ],\
|
||||
[0.8333333333333334, 0.3, 0.7971014492753624, 0.25, 0.2, 0.5681818181818181, 0.5, 0.4, 0.625 ],\
|
||||
[0.6, 0.25, 0.7111111111111111, 0.5, 0.3, 0.6521739130434783, 0.2857142857142857, 0.35, 0.547112462006079 ],\
|
||||
[0.2857142857142857, 0.35, 0.547112462006079, 1.0, 0.15, 0.9302325581395349, 0.6666666666666666, 0.6, 0.641025641025641 ],\
|
||||
[0.7142857142857143, 0.35, 0.729483282674772, 0.5, 0.3, 0.6521739130434783, 0.6923076923076923, 0.65, 0.6386066763425254 ],\
|
||||
[0.7142857142857143, 0.35, 0.729483282674772, 0.5, 0.1, 0.7142857142857143, 0.6666666666666666, 0.6, 0.641025641025641 ],\
|
||||
[0.8, 0.25, 0.8, 0.25, 0.2, 0.5681818181818181, 0.5384615384615384, 0.65, 0.5805515239477503 ],\
|
||||
[0.3333333333333333, 0.45, 0.5442176870748299, 0.3333333333333333, 0.15, 0.6201550387596899, 0.5384615384615384, 0.65, 0.5805515239477503 ]])
|
||||
assert len(counts.shape) == 2
|
||||
assert counts.shape[0] == 30
|
||||
assert counts.shape[1] == 9
|
||||
assert np.allclose(counts, ans, atol=1e-8)
|
||||
assert type(counts) == np.ndarray
|
||||
+145461
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user