dpilger26 / NumCpp

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

how can i choose the specified cols or rows by using slice #178

Closed reverse-2020 closed 1 year ago

reverse-2020 commented 1 year ago

a = np.random.randint(0, 100, (10,10)) print(a)

b = a[:, [1,4,5,7]] # just choose the col 1,4,5,7 or c = a[[2,8],:] # just choose the row 2,8

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

#include <iostream>

int main()
{
    const auto a = nc::random::randInt<int>({ 10, 10 }, 0, 100);
    std::cout << a << '\n';

    const auto b = a(a.rSlice(), nc::NdArray<nc::uint32>{ 1, 4, 5, 7 });
    std::cout << b << '\n';

    const auto c = a(nc::NdArray<nc::uint32>{ 1, 8 }, a.cSlice());
    std::cout << c << '\n';

    return 0;
}

which outputs

[[78, 25, 71, 94, 1, 40, 25, 2, 52, 34, ]
[27, 56, 14, 54, 52, 85, 49, 41, 74, 24, ]
[23, 32, 91, 16, 24, 19, 71, 96, 76, 8, ]
[45, 25, 77, 58, 95, 43, 32, 53, 25, 4, ]
[50, 69, 9, 90, 3, 15, 98, 62, 29, 36, ]
[48, 29, 28, 92, 62, 75, 71, 72, 6, 48, ]
[8, 75, 42, 59, 8, 27, 62, 22, 22, 43, ]
[98, 21, 85, 28, 83, 36, 93, 85, 49, 2, ]
[36, 73, 51, 42, 2, 90, 11, 98, 68, 48, ]
[73, 58, 52, 45, 82, 87, 49, 96, 88, 13, ]]

[[25, 1, 40, 2, ]
[56, 52, 85, 41, ]
[32, 24, 19, 96, ]
[25, 95, 43, 53, ]
[69, 3, 15, 62, ]
[29, 62, 75, 72, ]
[75, 8, 27, 22, ]
[21, 83, 36, 85, ]
[73, 2, 90, 98, ]
[58, 82, 87, 96, ]]

[[27, 56, 14, 54, 52, 85, 49, 41, 74, 24, ]
[36, 73, 51, 42, 2, 90, 11, 98, 68, 48, ]]
reverse-2020 commented 1 year ago
#include "NumCpp.hpp"

#include <iostream>

int main()
{
    const auto a = nc::random::randInt<int>({ 10, 10 }, 0, 100);
    std::cout << a << '\n';

    const auto b = a(a.rSlice(), nc::NdArray<nc::uint32>{ 1, 4, 5, 7 });
    std::cout << b << '\n';

    const auto c = a(nc::NdArray<nc::uint32>{ 1, 8 }, a.cSlice());
    std::cout << c << '\n';

    return 0;
}

which outputs

[[78, 25, 71, 94, 1, 40, 25, 2, 52, 34, ]
[27, 56, 14, 54, 52, 85, 49, 41, 74, 24, ]
[23, 32, 91, 16, 24, 19, 71, 96, 76, 8, ]
[45, 25, 77, 58, 95, 43, 32, 53, 25, 4, ]
[50, 69, 9, 90, 3, 15, 98, 62, 29, 36, ]
[48, 29, 28, 92, 62, 75, 71, 72, 6, 48, ]
[8, 75, 42, 59, 8, 27, 62, 22, 22, 43, ]
[98, 21, 85, 28, 83, 36, 93, 85, 49, 2, ]
[36, 73, 51, 42, 2, 90, 11, 98, 68, 48, ]
[73, 58, 52, 45, 82, 87, 49, 96, 88, 13, ]]

[[25, 1, 40, 2, ]
[56, 52, 85, 41, ]
[32, 24, 19, 96, ]
[25, 95, 43, 53, ]
[69, 3, 15, 62, ]
[29, 62, 75, 72, ]
[75, 8, 27, 22, ]
[21, 83, 36, 85, ]
[73, 2, 90, 98, ]
[58, 82, 87, 96, ]]

[[27, 56, 14, 54, 52, 85, 49, 41, 74, 24, ]
[36, 73, 51, 42, 2, 90, 11, 98, 68, 48, ]]

thanks for your great code , it really works, and I have anthoer question,if i want to choose the row where the value in column 2 > 0.01 ,how can i write the code ? - -

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

#include <iostream>

int main()
{
    const auto a = nc::random::randFloat<double>({ 10, 10 }, -1, 1);
    std::cout << "a =\n" << a << '\n';

    const auto& [rowIndices, _] = (a(a.rSlice(), 2) > 0.1).nonzero();
    const auto result           = a(rowIndices, a.cSlice());
    std::cout << "result = \n" << result;

    return 0;
}

which outputs:

a =
[[0.573642, -0.499039, 0.421342, 0.893336, -0.961458, -0.190196, -0.497364, -0.954575, 0.0412863, -0.310659, ]
[-0.451609, 0.122064, -0.719921, 0.0877121, 0.0438314, 0.714155, -0.00045128, -0.16126, 0.488561, -0.501664, ]
[-0.521447, -0.35987, 0.821002, -0.670334, -0.508934, -0.603361, 0.431785, 0.935649, 0.538863, -0.838585, ]
[-0.0801503, -0.485476, 0.554091, 0.167765, 0.9006, -0.1238, -0.355422, 0.06481, -0.487282, -0.90892, ]
[0.00995275, 0.392477, -0.817562, 0.814239, -0.938171, -0.695927, 0.963041, 0.240823, -0.402467, -0.277351, ]
[-0.0380624, -0.404306, -0.429625, 0.848387, 0.257805, 0.510063, 0.427785, 0.445516, -0.860326, -0.026331, ]
[-0.822278, 0.519244, -0.152365, 0.193965, -0.827168, -0.454078, 0.247615, -0.55573, -0.549105, -0.131719, ]
[0.974614, -0.578021, 0.706429, -0.431771, 0.672731, -0.266561, 0.870295, 0.71981, -0.0181782, -0.955843, ]
[-0.275766, 0.47289, 0.0387413, -0.154095, -0.941934, 0.818483, -0.775641, 0.960333, 0.375657, -0.0253385, ]
[0.471817, 0.177339, 0.0569037, -0.0887109, 0.656607, 0.740784, -0.0170201, 0.939061, 0.777592, -0.723471, ]]

result = 
[[0.573642, -0.499039, 0.421342, 0.893336, -0.961458, -0.190196, -0.497364, -0.954575, 0.0412863, -0.310659, ]
[-0.521447, -0.35987, 0.821002, -0.670334, -0.508934, -0.603361, 0.431785, 0.935649, 0.538863, -0.838585, ]
[-0.0801503, -0.485476, 0.554091, 0.167765, 0.9006, -0.1238, -0.355422, 0.06481, -0.487282, -0.90892, ]
[0.974614, -0.578021, 0.706429, -0.431771, 0.672731, -0.266561, 0.870295, 0.71981, -0.0181782, -0.955843, ]]
reverse-2020 commented 1 year ago
#include "NumCpp.hpp"

#include <iostream>

int main()
{
    const auto a = nc::random::randFloat<double>({ 10, 10 }, -1, 1);
    std::cout << "a =\n" << a << '\n';

    const auto& [rowIndices, _] = (a(a.rSlice(), 2) > 0.1).nonzero();
    const auto result           = a(rowIndices, a.cSlice());
    std::cout << "result = \n" << result;

    return 0;
}

which outputs:

a =
[[0.573642, -0.499039, 0.421342, 0.893336, -0.961458, -0.190196, -0.497364, -0.954575, 0.0412863, -0.310659, ]
[-0.451609, 0.122064, -0.719921, 0.0877121, 0.0438314, 0.714155, -0.00045128, -0.16126, 0.488561, -0.501664, ]
[-0.521447, -0.35987, 0.821002, -0.670334, -0.508934, -0.603361, 0.431785, 0.935649, 0.538863, -0.838585, ]
[-0.0801503, -0.485476, 0.554091, 0.167765, 0.9006, -0.1238, -0.355422, 0.06481, -0.487282, -0.90892, ]
[0.00995275, 0.392477, -0.817562, 0.814239, -0.938171, -0.695927, 0.963041, 0.240823, -0.402467, -0.277351, ]
[-0.0380624, -0.404306, -0.429625, 0.848387, 0.257805, 0.510063, 0.427785, 0.445516, -0.860326, -0.026331, ]
[-0.822278, 0.519244, -0.152365, 0.193965, -0.827168, -0.454078, 0.247615, -0.55573, -0.549105, -0.131719, ]
[0.974614, -0.578021, 0.706429, -0.431771, 0.672731, -0.266561, 0.870295, 0.71981, -0.0181782, -0.955843, ]
[-0.275766, 0.47289, 0.0387413, -0.154095, -0.941934, 0.818483, -0.775641, 0.960333, 0.375657, -0.0253385, ]
[0.471817, 0.177339, 0.0569037, -0.0887109, 0.656607, 0.740784, -0.0170201, 0.939061, 0.777592, -0.723471, ]]

result = 
[[0.573642, -0.499039, 0.421342, 0.893336, -0.961458, -0.190196, -0.497364, -0.954575, 0.0412863, -0.310659, ]
[-0.521447, -0.35987, 0.821002, -0.670334, -0.508934, -0.603361, 0.431785, 0.935649, 0.538863, -0.838585, ]
[-0.0801503, -0.485476, 0.554091, 0.167765, 0.9006, -0.1238, -0.355422, 0.06481, -0.487282, -0.90892, ]
[0.974614, -0.578021, 0.706429, -0.431771, 0.672731, -0.266561, 0.870295, 0.71981, -0.0181782, -0.955843, ]]

the codes help me a lot in my project,respect for your work.