-
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
11 changed files
with
140 additions
and
6 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 |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
src/__pycache__ | ||
build | ||
dist | ||
tulia.egg-info | ||
tulia.egg-info |
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,56 @@ | ||
from typing import Union | ||
|
||
import numpy as np | ||
|
||
from src.base import Model | ||
|
||
|
||
# 1. Ordered Target encoding | ||
# 2. Boostrap data | ||
# 3. Symmetric tree | ||
# 4. | ||
|
||
|
||
class CatBoostClassifier(Model): | ||
""" | ||
CatBoost for classification tasks. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
learning_rate: float = 3e-1, | ||
n_steps: int = 100, | ||
max_depth: int = 3, | ||
cat_features: list = None | ||
): | ||
pass | ||
|
||
|
||
def fit(self, x: np.ndarray, y: np.ndarray): | ||
pass | ||
def predict(self, x: np.ndarray) -> np.ndarray: | ||
pass | ||
|
||
def _predict(self, x: np.ndarray) -> Union[np.ndarray, float, int]: | ||
pass | ||
|
||
def _encode_cat_features(self, x: np.ndarray, y: np.ndarray) -> np.ndarray: | ||
cat_feature_idxs = [] | ||
|
||
encoded_features = [] | ||
for idx in cat_feature_idxs: | ||
encoded_feature = [] | ||
option_count = {} | ||
total_count = {} | ||
for i, x_sample in enumerate(x[:, idx]): | ||
ctr = (option_count.get(x_sample, 0) + 0.05) / (total_count.get(x_sample, 0) + 1) | ||
encoded_feature.append(ctr) | ||
|
||
if y[i] == 1: | ||
option_count[x_sample] = option_count.get(x_sample, 0) + 1 | ||
total_count[x_sample] = total_count.get(x_sample, 0) + 1 | ||
|
||
encoded_features.append(np.array(encoded_feature)) | ||
|
||
x[:, cat_feature_idxs] = encoded_features | ||
return x |
Binary file not shown.
Binary file not shown.
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
Binary file added
BIN
+2.35 KB
tests/metrics/regression/__pycache__/test_r2_score.cpython-310-pytest-7.4.4.pyc
Binary file not shown.
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,63 @@ | ||
import numpy as np | ||
|
||
from src.metrics import r2_score | ||
|
||
|
||
def test_r2_identical_arrays(): | ||
y_true = np.array([1, 2, 3, 4, 5]) | ||
y_pred = np.array([1, 2, 3, 4, 5]) | ||
|
||
expected_r2 = 1.0 | ||
r2 = r2_score(y_true, y_pred) | ||
|
||
assert np.isclose(expected_r2, r2, atol=1e-5, rtol=1e-5) | ||
|
||
|
||
def test_r2_shifted_arrays(): | ||
y_true = np.array([1, 2, 3, 4, 5]) | ||
y_pred = np.array([2, 3, 4, 5, 6]) | ||
|
||
expected_r2 = 0.5 | ||
r2 = r2_score(y_true, y_pred) | ||
|
||
assert np.isclose(expected_r2, r2, atol=1e-5, rtol=1e-5) | ||
|
||
|
||
def test_r2_reversed_arrays(): | ||
y_true = np.array([1, 2, 3, 4, 5]) | ||
y_pred = np.array([5, 4, 3, 2, 1]) | ||
|
||
expected_r2 = -3.0 | ||
r2 = r2_score(y_true, y_pred) | ||
|
||
assert np.isclose(expected_r2, r2, atol=1e-5, rtol=1e-5) | ||
|
||
|
||
def test_r2_large_numbers(): | ||
y_true = np.array([10, 20, 30, 40, 50]) | ||
y_pred = np.array([15, 25, 35, 45, 55]) | ||
|
||
expected_r2 = 0.875 | ||
r2 = r2_score(y_true, y_pred) | ||
|
||
assert np.isclose(expected_r2, r2, atol=1e-5, rtol=1e-5) | ||
|
||
|
||
def test_mse_decimal_numbers(): | ||
y_true = np.array([0.5, 0.6, 0.7, 0.8, 0.9]) | ||
y_pred = np.array([0.6, 0.7, 0.8, 0.9, 1.0]) | ||
|
||
expected_r2 = 0.5 | ||
r2 = r2_score(y_true, y_pred) | ||
|
||
assert np.isclose(expected_r2, r2, atol=1e-5, rtol=1e-5) | ||
|
||
|
||
def test_mse_with_outliers(): | ||
y_true = np.array([10, 20, 30, 40, 135]) | ||
y_pred = np.array([15, 25, 35, 45, 55]) | ||
|
||
expected_r2 = 1 - 6500 / 10180 | ||
r2 = r2_score(y_true, y_pred) | ||
|
||
assert np.isclose(expected_r2, r2, atol=1e-5, rtol=1e-5) |