1

Consider the following code:

Eigen::VectorXf classify()
{
   Eigen::VectorXf probability(4);
   probability << 0.9, 0.8, -0.1, 0.2;

   auto y_pred = probability.array() > 0.8; //what is the type of y_pred?
   //y_pred looks like [1 0 0 0]

   // how to return y_pred as a VectorXf? 
   // I'm trying this, but it is not working:
   return static_cast<Eigen::VectorXf>(y_pred); //doesn't work
}

int main() 
{
   classify();
}

2 questions:

  • (for my understanding) What is the type of y_pred? I was thinking it is an ArrayXf but it seems not to be the case
  • (the question this post is about) how can I convert y_pred to an Eigen vector (VectorXf or something else)?
Gaetan
  • 577
  • 2
  • 7
  • 20

3 Answers3

2

y_pred is an abstract expression. It's type is something like (simplified for readability):

CWiseBinaryOp<greater<float>,ArrayWrapper<VectorXf>,CwiseNullaryOp<Constant<float>,ArrayXf>>

It inherits ArrayBase and its Scalar type is bool.

If you want a VectorXf, then cast the bools to floats:

VectorXf foo = y_pred.cast<float>();

The cast from array to matrix is implicit in this case.

ggael
  • 28,425
  • 2
  • 65
  • 71
0

If you take a look at the Eigen documentation, you will notice that Eigen::VectorXf is actually a typedef for Eigen::Matrix<float, Eigen::Dynamic, 1>.

Type of y_pred

The Eigen::Matrix class derives from the Eigen::MatrixBase template class, which is where the array() member function comes from. This method returns a wrapper object, which can basically be considered as an Eigen::Array. Applying the comparison operator to the result of array() yields an Eigen::Array of bool.

Converting to a vector-like type

The documentation has a section dedicated to the use of the Eigen::Array class. One subsection, namely Converting between array and matrix expressions, is of particular interest in the context of your question. There, one can read that an Eigen::Array may be converted to an Eigen::Matrix by using the matrix() method.

Using the above observations, you should be able to convert y_pred to some kind of vector by returning y_pred.matrix() instead of static_cast<Eigen::VectorXf>(y_pred). The return type will likely be something like Eigen::Matrix<bool, Eigen::Dynamic, 1>.

Due to the fact that matrices of bool are less common in linear algebra operations, there are no typedefs associated to them.

J-M. Gorius
  • 606
  • 4
  • 15
  • Thanks for your reply. The answers to the first question is now clear to me. Regarding the second question `return y_pred.matrix()` doesn't work. The only way I see to go around this is defining a new type `typedef Eigen::Array ArrayXb` and use it as the return type instead of `Eigen::VectorXf`. Why does Eigen have no array or vector of bool? – Gaetan Dec 22 '18 at 18:10
  • Eigen does include support for arrays and vectors of `bool`. However, there are no `typedef`s for those types, as they are less commonly used. I will update my answer to reflect your comment. – J-M. Gorius Dec 22 '18 at 18:15
-1

No,progress will operate the right expression so auto a=3>2 it will return bool:1 or 0.(if the right expression is true the value of left is 1) so, the type of y_pred is bool.

kuake56
  • 9
  • 2
  • No, y_pred is a column vector (or maybe a 4by1 array), I can verify it by printing it with std::cout. it give me [1 0 0 0] – Gaetan Dec 22 '18 at 17:47