1
votes

I'm trying to implement a Normalizer within Eigen.

The functionality it tries to achieve is as follows:

Xnorm = (X - np.mean(X, axis=0))/(np.std(X, axis=0))(equivalent numpy)

In the main normalization step I have a function as follows:

typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
    matrix_eig;
typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::RowMajor> vector_eig;

matrix_eig Normalizer::Transform(const matrix_eig &X) {
  // mean_ and std_ are vector_eig types
  matrix_eig centered = X.rowwise() - mean_.transpose();
  // Below line doesnt work since '/' is not allowed for matrices
  return centered.rowwise()/std_;
}

My question is how exactly do I do something like centered.rowwise().array()?

1
What operation are you trying to accomplish with centered.rowwise()/std_? As far as I know, mathematics does not have a standard way to divide one matrix by another, so I don't see what the intent is.JaMiT
As I had explained above, I'm trying to simply normalize the data(i.e. zero mean and unit standard deviation). Xnorm = (X - np.mean(X, axis=0))/(np.std(X, axis=0)) is the equivalent numpy operation.tangy
No, you did not explain that above. You just said that this is "a function" in the main normalization step, but you neglected to say what this particular "Transform" is supposed to accomplish within that step. (Please make that clearer if you wish.) Anyway, the normalizations that I am familiar with involve multiplication/division by a scalar, not a vector, so I'll leave this for people who understand what you're talking about.JaMiT
Sure, I've added the description in the question. Thanks for your suggestion.tangy

1 Answers

5
votes

The the question:

how exactly do I do something like centered.rowwise().array()

the answer is as simple as:

centered.array().rowwise()

You should thus write the division as:

return centered.array().rowwise() / std_.array();

BTW, there is also an error in the definition of vector_eig. If you want a row vector, then it's:

typedef Eigen::Matrix<float, 1, Eigen::Dynamic> vector_eig;

or simply:

typedef Eigen::RowVectorXf vector_eig;