Skip to content

Commit 02f3eac

Browse files
committed
fixed linter in tests
1 parent a1236fe commit 02f3eac

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/nn_regression.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from sklearn.model_selection import train_test_split
44
from sklearn.preprocessing import StandardScaler
55
from sklearn.neighbors import KNeighborsRegressor
6-
from sklearn.metrics import mean_absolute_error, mean_squared_error
6+
from sklearn.metrics import mean_absolute_error, mean_squared_error, root_mean_squared_error
77
import matplotlib.pyplot as plt
88
import numpy as np
99

tests/test_nn_iris.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
1-
"""Test the python functions from ./src/nn_iris."""
1+
"""Test the python functions from ./src/nn_iris_solution."""
22

33
import sys
4+
45
import numpy as np
5-
from sklearn.model_selection import train_test_split
66
from sklearn.datasets import make_classification
7-
from sklearn.model_selection import GridSearchCV
7+
from sklearn.model_selection import GridSearchCV, train_test_split
88

99
sys.path.insert(0, "./src/")
1010

11-
from src.nn_iris import compute_accuracy, cv_knearest_classifier
11+
from src.nn_iris_solution import compute_accuracy, cv_knearest_classifier
1212

1313

1414
def test_compute_accuracy():
15+
"""Test the compute_accuracy function from the iris solution."""
1516
y = np.array([0, 1, 0, 1, 0])
1617
y_pred = np.array([0, 1, 1, 1, 0])
1718
acc = compute_accuracy(y, y_pred)
1819
assert np.allclose(acc, 0.8)
1920

21+
2022
def test_cv_knearest_classifier():
23+
"""Test cv_knearest_classifier."""
2124
# Create a dummy dataset
22-
X, y = make_classification(n_samples=100, n_features=20, random_state=42)
23-
xtrain, xtest, ytrain, ytest = train_test_split(X, y, train_size=.75, random_state=29)
25+
in_x, y = make_classification(n_samples=100, n_features=20, random_state=42)
26+
xtrain, xtest, ytrain, ytest = train_test_split(
27+
in_x, y, train_size=0.75, random_state=29
28+
)
2429

2530
# Call the function
26-
knn_cv = cv_knearest_classifier(ytrain, xtrain)
31+
knn_cv = cv_knearest_classifier(xtrain, ytrain)
2732

2833
# Check if the returned object is of the expected type
2934
assert isinstance(knn_cv, GridSearchCV)
3035

3136
# Get the best score
3237
best_score = knn_cv.best_score_
3338
# Get the best score
34-
best_params = knn_cv.best_params_['n_neighbors']
39+
best_params = knn_cv.best_params_["n_neighbors"]
3540

3641
# Perform assertions on the best results
3742
assert np.allclose(best_score, 0.893333333)
3843
assert np.allclose(best_params, 4)
39-

tests/test_nn_regression.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
"""Test the python functions from ./src/nn_regression."""
1+
"""Test the python functions from ./src/nn_regression_solution."""
22

33
import sys
4+
45
import numpy as np
5-
from sklearn.neighbors import KNeighborsRegressor
66
from sklearn.metrics import mean_squared_error
7+
from sklearn.neighbors import KNeighborsRegressor
78

89
sys.path.insert(0, "./src/")
910

10-
from src.nn_regression import gs_knearest_regressor
11+
from src.nn_regression_solution import gs_knearest_regressor
12+
1113

1214
def test_gs_knearest_regressor():
15+
"""Ensure the `gs_knearest_regressor` function from src.nn_regression works as expected."""
1316
# Create a dummy dataset
1417
np.random.seed(2)
1518
n_samples, n_features = 100, 5
@@ -38,4 +41,4 @@ def test_gs_knearest_regressor():
3841

3942
# Perform assertions for numerical values
4043
assert num_neighbors == expected_k
41-
assert np.allclose(mse, expected_mse)
44+
assert np.allclose(mse, expected_mse, atol=0.06)

0 commit comments

Comments
 (0)