Skip to content

Commit

Permalink
Merge pull request #33 from HippolyteGuigon/features_building_dockerf…
Browse files Browse the repository at this point in the history
…iles

pushing_final_tests
  • Loading branch information
HippolyteGuigon authored Apr 19, 2023
2 parents 45adb0f + 651dee9 commit bd17a7d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Random_forest/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,27 @@ def predict(self, full_X_to_predict: np.array) -> np.array:
prediction = self.individual_predict(x)
predicted.append(prediction)

predicted = np.array(predicted)

return predicted

def score(self, y_pred: np.array, y_test: np.array) -> float:
"""
The goal of this function is, once
the model has been fitted, to compute
its score regarding its objective (regression
or classification)
Arguments:
-y_pred: np.array: The predictions made
by the model
-y_test: np.array: The real values
"""

if self.objective == "classification":
return np.mean(y_pred == y_test)
else:
rss = ((y_pred - y_test) ** 2).sum()
tss = ((y_test - y_test.mean()) ** 2).sum()
r_squared = 1 - rss / tss
return r_squared
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from Random_forest.model.model import RandomForest
from Random_forest.decision_tree.array_functions import get_random_set, is_float
from Random_forest.decision_tree.array_functions import get_random_set
from Random_forest.configs.confs import load_conf

main_params = load_conf("configs/main.yml", include=True)
Expand Down
47 changes: 47 additions & 0 deletions test_unnitest.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,53 @@ def test_model_prediction(self) -> None:
self.assertTrue(np.all(classification_predictions))
self.assertTrue(np.all(regression_predictions))

def test_score(self) -> None:
"""
The goal of this function
is to test the model ability
to score a result
Arguments:
-None
Returns:
-None
"""

X, y = get_random_set(
row_size_test_dataset=row_size_test_dataset, objective="classification"
)
X_predict_classification, y_predict_classification = get_random_set(
row_size_test_dataset=30, objective="classification"
)

model_classification = RandomForest(objective="classification")
model_classification.fit(X, y)

classification_predictions = model_classification.predict(
X_predict_classification
)

X, y = get_random_set(
row_size_test_dataset=row_size_test_dataset, objective="regression"
)
X_predict_regression, y_predict_regression = get_random_set(
row_size_test_dataset=30, objective="regression"
)

model_regression = RandomForest(objective="regression")
model_regression.fit(X, y)

regression_predictions = model_regression.predict(X_predict_regression)

accuracy = model_classification.score(
classification_predictions, y_predict_classification
)
r_squared = model_regression.score(regression_predictions, y_predict_regression)

self.assertTrue((accuracy >= 0 and accuracy <= 1))
self.assertTrue(isinstance(r_squared, float))


if __name__ == "__main__":
main()
Expand Down

0 comments on commit bd17a7d

Please sign in to comment.