dpilger26 / NumCpp

C++ implementation of the Python Numpy library
https://dpilger26.github.io/NumCpp
MIT License
3.51k stars 548 forks source link

How do I change this python expression? #181

Closed YoungjaeDev closed 1 year ago

YoungjaeDev commented 1 year ago
out: 2d-array, shape [-1, 6]
out = out[~np.isnan(out).any(axis=1)]

What I want to do is delete the row that contains nan.

dpilger26 commented 1 year ago
#include "NumCpp.hpp"

#include <iostream>

int main()
{
    nc::random::seed(666);

    auto a    = nc::random::randFloat<double>({ 4, 6 }, 1);
    a(0, 0)   = nc::constants::nan;
    a(-1, -1) = nc::constants::nan;
    std::cout << "a:\n" << a << '\n';

    auto [_, rowNonNanIndices] = (!nc::isnan(a).any(nc::Axis::COL)).nonzero();
    auto noNanRows             = a(rowNonNanIndices, a.cSlice());
    std::cout << "noNanRows:\n" << noNanRows;

    return 0;
}

which outputs:

a:
[[nan, 0.454037, 0.275594, 0.066487, 0.397731, 0.550428, ]
[0.267218, 0.349375, 0.818072, 0.883664, 0.474568, 0.25094, ]
[0.738563, 0.0835627, 0.0451471, 0.803801, 0.510884, 0.651005, ]
[0.668859, 0.493873, 0.534184, 0.867498, 0.984127, nan, ]]

noNanRows:
[[0.267218, 0.349375, 0.818072, 0.883664, 0.474568, 0.25094, ]
[0.738563, 0.0835627, 0.0451471, 0.803801, 0.510884, 0.651005, ]]
YoungjaeDev commented 1 year ago

I'll close issue after testing. thank you

dpilger26 commented 1 year ago

Closing due to inactivity.