0

I am using eigen library.

The reason i used eigen is i want do linear regression in my c++ code.

y = W * x + b

x and y is my data, with format eigen matrix, there may be NAN in them.

but when my x or y contains NAN value, the linear regression will fail with w = [nan,....]

I want to avoid this, so i need to remove the nan line.

for example:

#include <eigen3/Eigen/Dense>
#include <iostream>

using namespace Eigen;
using namespace std;
int main() {
  VectorXd e = VectorXd::Random(5) * 3;
  cout << " e:" << endl << e << endl;

  VectorXd b = VectorXd::Random(2) * 18; 
  cout << " b:" << endl << b << endl;

  MatrixXd x = MatrixXd::Random(5, 2) * 100;
  cout << " x:" << endl << x << endl;

  MatrixXd y = x * b + e;
  cout << " y:" << endl << y << endl;

  y(3,0) = NAN;  // i made the third element of y as NAN, this will cause linear regression failed
  // what i want is, just ignore the 3rd line of y and x, then do linear regression

}

this is quite similar with python pandas operation:

df = pd.DataFrame(...)
df = df.dropna()

could you help on this?

nick
  • 832
  • 3
  • 12
  • Why don't you go over every element, check if they are nan and set to whatever value you want? – ALX23z Feb 03 '22 at 07:06
  • @ALX23z there is some unexpected cases will cause NAN, and it is not preditable. For example, x is always good(without nan), but y is given by external signal, which sometimes can be nan – nick Feb 03 '22 at 07:08
  • 1
    Dupe? [How to remove a certain row or column while using Eigen Library c++](https://stackoverflow.com/questions/13290395/how-to-remove-a-certain-row-or-column-while-using-eigen-library-c) – Evg Feb 03 '22 at 07:28
  • 3
    Don't know, forcing a NaN element of a matrix to some arbitrary number feels like sweeping the problem under the rug. – MatG Feb 03 '22 at 09:38

0 Answers0