first commit

This commit is contained in:
2025-11-12 11:34:34 +03:00
commit 29280f3e50
77 changed files with 272246 additions and 0 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

File diff suppressed because one or more lines are too long
Binary file not shown.
+73
View File
@@ -0,0 +1,73 @@
import numpy as np
import typing
from collections import defaultdict
def kfold_split(
num_objects: int, num_folds: int
) -> list[tuple[np.ndarray, np.ndarray]]:
indices = np.arange(num_objects)
fold_size = num_objects // num_folds
result = []
for i in range(num_folds):
start = i * fold_size
if i == num_folds - 1:
end = num_objects
else:
end = (i + 1) * fold_size
val_indices = indices[start:end]
train_indices = np.concatenate([indices[:start], indices[end:]])
result.append((train_indices, val_indices))
return result
def knn_cv_score(
X: np.ndarray,
y: np.ndarray,
parameters: dict[str, list],
score_function: callable,
folds: list[tuple[np.ndarray, np.ndarray]],
knn_class: object,
) -> dict[str, float]:
results = {}
normalizers = parameters.get("normalizers", [(None, None)])
n_neighbors_list = parameters.get("n_neighbors", [5])
metrics_list = parameters.get("metrics", ["euclidean"])
weights_list = parameters.get("weights", ["uniform"])
for normalizer_tuple in normalizers:
normalizer, normalizer_name = normalizer_tuple
for n_neighbors in n_neighbors_list:
for metric in metrics_list:
for weight in weights_list:
fold_scores = []
for train_idx, val_idx in folds:
X_train, X_val = X[train_idx], X[val_idx]
y_train, y_val = y[train_idx], y[val_idx]
if normalizer is not None:
normalizer_fitted = normalizer.fit(X_train)
X_train = normalizer_fitted.transform(X_train)
X_val = normalizer_fitted.transform(X_val)
knn = knn_class(
n_neighbors=n_neighbors, metric=metric, weights=weight
)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_val)
score = score_function(y_val, y_pred)
fold_scores.append(score)
mean_score = np.mean(fold_scores)
key = (normalizer_name, n_neighbors, metric, weight)
results[key] = mean_score
return results
+76
View File
@@ -0,0 +1,76 @@
import numpy as np
import typing
from collections import defaultdict
def kfold_split(
num_objects: int, num_folds: int
) -> list[tuple[np.ndarray, np.ndarray]]:
all_indices = np.arange(num_objects)
fold_size = num_objects // num_folds
splits = []
for fold_idx in range(num_folds):
fold_start = fold_idx * fold_size
fold_end = (
num_objects if fold_idx == num_folds - 1 else (fold_idx + 1) * fold_size
)
validation = all_indices[fold_start:fold_end]
training = np.concatenate([all_indices[:fold_start], all_indices[fold_end:]])
splits.append((training, validation))
return splits
def knn_cv_score(
X: np.ndarray,
y: np.ndarray,
parameters: dict[str, list],
score_function: callable,
folds: list[tuple[np.ndarray, np.ndarray]],
knn_class: object,
) -> dict[str, float]:
cv_results = {}
normalizer_configs = parameters.get("normalizers", [(None, None)])
neighbor_counts = parameters.get("n_neighbors", [5])
distance_metrics = parameters.get("metrics", ["euclidean"])
weight_schemes = parameters.get("weights", ["uniform"])
for norm_obj, norm_label in normalizer_configs:
for num_neighbors in neighbor_counts:
for distance_metric in distance_metrics:
for weight_scheme in weight_schemes:
scores_per_fold = []
for train_indices, val_indices in folds:
X_tr, X_va = X[train_indices], X[val_indices]
y_tr, y_va = y[train_indices], y[val_indices]
if norm_obj is not None:
fitted_normalizer = norm_obj.fit(X_tr)
X_tr = fitted_normalizer.transform(X_tr)
X_va = fitted_normalizer.transform(X_va)
classifier = knn_class(
n_neighbors=num_neighbors,
metric=distance_metric,
weights=weight_scheme,
)
classifier.fit(X_tr, y_tr)
predictions = classifier.predict(X_va)
fold_score = score_function(y_va, predictions)
scores_per_fold.append(fold_score)
avg_score = np.mean(scores_per_fold)
param_key = (
norm_label,
num_neighbors,
distance_metric,
weight_scheme,
)
cv_results[param_key] = avg_score
return cv_results
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,161 @@
import numpy as np
from numpy.testing import assert_equal, assert_allclose
from sklearn import neighbors
from sklearn.metrics import r2_score
from sklearn.preprocessing import MinMaxScaler
from cross_val import kfold_split, knn_cv_score
def test_split_0():
with open('cross_val.py', 'r') as file:
lines = ' '.join(file.readlines())
assert 'import numpy' in lines
assert 'import defaultdict' in lines
assert 'import typing' in lines
assert lines.count('import') == 3
assert 'sklearn' not in lines
def test_split_1():
X_1 = kfold_split(2, 2)
answer = [(np.array([1]), np.array([0])), (np.array([0]), np.array([1]))]
assert type(X_1) == list
assert_equal(X_1, answer)
def test_split_2():
X_1 = kfold_split(5, 3)
answer = [(np.array([1, 2, 3, 4]), np.array([0])),
(np.array([0, 2, 3, 4]), np.array([1])),
(np.array([0, 1]), np.array([2, 3, 4]))]
assert type(X_1) == list
assert_equal(X_1, answer)
def test_split_3():
X_1 = kfold_split(11, 7)
answer = [(np.array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), np.array([0])),
(np.array([ 0, 2, 3, 4, 5, 6, 7, 8, 9, 10]), np.array([1])),
(np.array([ 0, 1, 3, 4, 5, 6, 7, 8, 9, 10]), np.array([2])),
(np.array([ 0, 1, 2, 4, 5, 6, 7, 8, 9, 10]), np.array([3])),
(np.array([ 0, 1, 2, 3, 5, 6, 7, 8, 9, 10]), np.array([4])),
(np.array([ 0, 1, 2, 3, 4, 6, 7, 8, 9, 10]), np.array([5])),
(np.array([0, 1, 2, 3, 4, 5]), np.array([ 6, 7, 8, 9, 10]))]
assert type(X_1) == list
assert_equal(X_1, answer)
def test_cv_4():
X_train = np.array([[2, 1, -1], [1, 1, 1], [0.9, -0.25, 7], [1, 2, -3], [0, 0, 0], [2, -1, 0.5]])
y_train = np.sum(X_train, axis=1)
parameters = {
'n_neighbors': [1, 2, 4],
'metrics': ['euclidean', 'cosine'],
'weights': ['uniform', 'distance'],
'normalizers': [(None, 'None')]
}
folds = kfold_split(6, 3)
out = knn_cv_score(X_train, y_train, parameters, r2_score, folds, neighbors.KNeighborsRegressor)
answer = {
('None', 1, 'euclidean', 'uniform'): -11.29188203967135,
('None', 1, 'euclidean', 'distance'): -11.29188203967135,
('None', 1, 'cosine', 'uniform'): -17.63280796559728,
('None', 1, 'cosine', 'distance'): -17.632807965597276,
('None', 2, 'euclidean', 'uniform'): -7.5333863756105215,
('None', 2, 'euclidean', 'distance'): -7.997305328982919,
('None', 2, 'cosine', 'uniform'): -4.246942433109774,
('None', 2, 'cosine', 'distance'): -6.7448165099645365,
('None', 4, 'euclidean', 'uniform'): -3.6194607932134364,
('None', 4, 'euclidean', 'distance'): -4.211377791660151,
('None', 4, 'cosine', 'uniform'): -3.6194607932134364,
('None', 4, 'cosine', 'distance'): -4.335691384752842
}
assert type(out) == dict
assert len(out) == len(answer)
for key in answer:
assert_allclose(answer[key], out[key])
def test_cv_5():
X_train = np.array([[ 0.62069296, -0.07097426, 0.65172896, -1.14620331],
[ 2.03347616, 0.32524614, -0.71941433, -0.30789854],
[ 0.17100377, 1.63120292, 1.34284446, -2.16397238],
[-1.65370417, 0.62499229, -0.50217293, 2.07813591],
[ 0.84667916, 0.25458428, 0.14720704, -0.18668345],
[ 0.43833344, -1.40348048, -1.37944118, 0.19192659],
[ 0.97229574, -0.54606276, -0.09855294, 1.28961291],
[ 0.25355626, -1.72816511, 0.084554 , -2.14256875],
[ 0.36103462, -1.28930935, 1.34586369, -0.57300728],
[-1.42711933, -0.11832827, -0.58038295, -1.56806583]])
y_train = np.sum(np.abs(X_train), axis=1)
parameters = {
'n_neighbors': [1, 2, 4],
'metrics': ['euclidean', 'cosine'],
'weights': ['uniform', 'distance'],
'normalizers': [(None, 'None')]
}
folds = kfold_split(10, 3)
out = knn_cv_score(X_train, y_train, parameters, r2_score, folds, neighbors.KNeighborsRegressor)
answer = {
('None', 1, 'euclidean', 'uniform'): -3.8869140469579033,
('None', 1, 'euclidean', 'distance'): -3.8869140469579033,
('None', 1, 'cosine', 'uniform'): -3.8967543637841557,
('None', 1, 'cosine', 'distance'): -3.8967543637841557,
('None', 2, 'euclidean', 'uniform'): -2.8537893891104353,
('None', 2, 'euclidean', 'distance'): -2.8723718210868676,
('None', 2, 'cosine', 'uniform'): -0.9110922868244854,
('None', 2, 'cosine', 'distance'): -1.2935713889809644,
('None', 4, 'euclidean', 'uniform'): -1.0722930212962776,
('None', 4, 'euclidean', 'distance'): -1.291339953080277,
('None', 4, 'cosine', 'uniform'): 0.010193326582544423,
('None', 4, 'cosine', 'distance'): -0.38359677639174855
}
assert type(out) == dict
assert len(out) == len(answer)
for key in answer:
assert_allclose(answer[key], out[key])
def test_cv_6():
X_train = np.array([[ 0.62069296, -0.07097426, 0.65172896, -1.14620331],
[ 2.03347616, 0.32524614, -0.71941433, -0.30789854],
[ 0.17100377, 1.63120292, 1.34284446, -2.16397238],
[-1.65370417, 0.62499229, -0.50217293, 2.07813591],
[ 0.84667916, 0.25458428, 0.14720704, -0.18668345],
[ 0.43833344, -1.40348048, -1.37944118, 0.19192659],
[ 0.97229574, -0.54606276, -0.09855294, 1.28961291],
[ 0.25355626, -1.72816511, 0.084554 , -2.14256875],
[ 0.36103462, -1.28930935, 1.34586369, -0.57300728],
[-1.42711933, -0.11832827, -0.58038295, -1.56806583]])
y_train = np.sum(np.abs(X_train), axis=1)
scaler = MinMaxScaler()
parameters = {
'n_neighbors': [1, 2, 4],
'metrics': ['euclidean', 'cosine'],
'weights': ['uniform', 'distance'],
'normalizers': [(scaler, 'MinMaxScaler')]
}
folds = kfold_split(10, 3)
out = knn_cv_score(X_train, y_train, parameters, r2_score, folds, neighbors.KNeighborsRegressor)
answer = {
('MinMaxScaler', 1, 'euclidean', 'uniform'): -3.886914104013526,
('MinMaxScaler', 1, 'euclidean', 'distance'): -3.886914104013526,
('MinMaxScaler', 1, 'cosine', 'uniform'): -3.339427669385479,
('MinMaxScaler', 1, 'cosine', 'distance'): -3.339427669385479,
('MinMaxScaler', 2, 'euclidean', 'uniform'): -2.821522070818421,
('MinMaxScaler', 2, 'euclidean', 'distance'): -2.909515284414977,
('MinMaxScaler', 2, 'cosine', 'uniform'): -3.0373126577073877,
('MinMaxScaler', 2, 'cosine', 'distance'): -2.7197802024893374,
('MinMaxScaler', 4, 'euclidean', 'uniform'): -1.229118435031323,
('MinMaxScaler', 4, 'euclidean', 'distance'): -1.4848798788742938,
('MinMaxScaler', 4, 'cosine', 'uniform'): -0.3586698577110674,
('MinMaxScaler', 4, 'cosine', 'distance'): -0.7914850319051477
}
assert type(out) == dict
assert len(out) == len(answer)
for key in answer:
assert_allclose(answer[key], out[key])
@@ -0,0 +1,21 @@
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
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
+69
View File
@@ -0,0 +1,69 @@
#!/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')))
max_mark = 3
grade_mapping = [3]
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
if len(argv) != 3:
print(f'Usage: {argv[0]} test/unittest test_name')
exit(0)
mode = argv[1]
test_name = argv[2]
test_dir = glob(f'public_tests/[0-9][0-9]_{mode}_{test_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.

After

Width:  |  Height:  |  Size: 41 KiB