Skip to content

Commit

Permalink
add const for functions predict and score
Browse files Browse the repository at this point in the history
  • Loading branch information
qbarthelemy committed Apr 7, 2022
1 parent df77f02 commit 6dbf50a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
4 changes: 2 additions & 2 deletions include/CSimpleLinearRegression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SimpleLinearRegression
* whose data type is double to keep the maximum of numerical precision.
*/
template<template<typename, typename> class ContType, typename ValType, typename Alloc>
ContType<double, std::allocator<double>> predict(const ContType<ValType, Alloc>& x)
ContType<double, std::allocator<double>> predict(const ContType<ValType, Alloc>& x) const
{
auto x_it = x.begin();
ContType<double, std::allocator<double>> y(x.size());
Expand All @@ -91,7 +91,7 @@ class SimpleLinearRegression
* @return R² of `predict(x)` wrt. `y`.
*/
template<template<typename, typename> class ContType, typename ValType, typename Alloc>
double score(const ContType<ValType, Alloc>& x, const ContType<ValType, Alloc>& y)
double score(const ContType<ValType, Alloc>& x, const ContType<ValType, Alloc>& y) const
{
size_t size = x.size();
if (size != y.size())
Expand Down
6 changes: 3 additions & 3 deletions include/CSimpleLogisticRegression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ class SimpleLogisticRegression
* whose data type is int to provide a binary target value.
*/
template<template<typename, typename> class ContType, typename ValType, typename Alloc>
ContType<int, std::allocator<int>> predict(const ContType<ValType, Alloc>& x)
ContType<int, std::allocator<int>> predict(const ContType<ValType, Alloc>& x) const
{
ContType<double, std::allocator<double>> y_lin = Maths::linear(x, get_coeff(), get_intercept());
ContType<double, std::allocator<double>> y_sig = Maths::sigmoid(y_lin);

ContType<int, std::allocator<int>> y(x.size());
std::transform(y_sig.begin(), y_sig.end(), y.begin(), [](double e) { return e >= 0.5 ? 1 : 0; });

Expand All @@ -123,7 +123,7 @@ class SimpleLogisticRegression
* @return Accuracy of `predict(x)` wrt. `y`.
*/
template<template<typename, typename> class ContType, typename ValType, typename Alloc>
double score(const ContType<ValType, Alloc>& x, const ContType<int, std::allocator<int>>& y)
double score(const ContType<ValType, Alloc>& x, const ContType<int, std::allocator<int>>& y) const
{
ContType<int, std::allocator<int>> y_predict = predict(x);
return Stats::accuracy_score(y, y_predict);
Expand Down
8 changes: 3 additions & 5 deletions include/Stats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,11 @@ namespace Stats

double sxx = std::inner_product(x_cent.begin(), x_cent.end(), x_cent.begin(), 0.0);
double syy = std::inner_product(y_cent.begin(), y_cent.end(), y_cent.begin(), 0.0);
double sxy = std::inner_product(x_cent.begin(), x_cent.end(), y_cent.begin(), 0.0);

double denom = std::sqrt(sxx) * std::sqrt(syy);
if (denom <= 0)
if (sxx <= 0 || syy <= 0)
return std::numeric_limits<double>::quiet_NaN();

double r = sxy / denom;
double sxy = std::inner_product(x_cent.begin(), x_cent.end(), y_cent.begin(), 0.0);
double r = sxy / (std::sqrt(sxx) * std::sqrt(syy));
r = max(min(r, 1.0), -1.0);
return r;
}
Expand Down

0 comments on commit 6dbf50a

Please sign in to comment.