Skip to content

NDArray operation '&' #129

Open
Open
@xiaozhu1988

Description

@xiaozhu1988

this is the code i write by C#

var ndarrya = np.array(new double[] { 10, 20, 30, 40, 50, 60,70,80,90,100 });
var conditions = new NDarray<bool>[2];
conditions[0] = ndarrya <= 30;
conditions[1] = (ndarrya > 50)&(ndarrya <= 100);

var choiselist = new NDarray<float>[2];
choiselist[0] = np.array(new float[] { 10 });
choiselist[1] = np.array(new float[] { 20 });
var des = np.select(conditions, choiselist, 0);

there is an error in the following line
conditions[1] = (ndarrya > 50)&(ndarrya <= 100);
Visual studio warn that 'the operation & can not be userd between NDarray[bool]
I just want to know how to write the Multiple Conditions
wish for you reply

Activity

henon

henon commented on Aug 30, 2024

@henon
Contributor

Can you provide the equivalent python snippet for comparison?

xiaozhu1988

xiaozhu1988 commented on Sep 1, 2024

@xiaozhu1988
Author

Can you provide the equivalent python snippet for comparison?

import numpy as np
data = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100,110,120])
conlist = [data <= 30, (data >=50) & (data < 100), data>=100]
choiselist = [66, 77, 88]
dataReClass = np.select(conlist, choiselist, default=0)
print(dataReClass)

the result is below
[66 66 66 0 77 77 77 77 77 88 88 88]
sorry for reply so late,

henon

henon commented on Sep 1, 2024

@henon
Contributor

The operators were only defined for scalars, now I added overloads for arrays and it works:

        [TestMethod]
        public void IssueByXiaozhu1988()
        {
            //>>> data = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120])
            //>>> (data >= 50) & (data < 100)
            //array([False, False, False, False,  True,  True,  True,  True,  True,
            //       False, False, False])

            var data = np.array(new[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120 });
            Console.WriteLine(((data >= 50) & (data < 100)).repr);
            Assert.AreEqual("array([False, False, False, False,  True,  True,  True,  True,  True,\n       False, False, False])", ((data >= 50) & (data < 100)).repr);
            Console.WriteLine(((data >= 50) | (data < 100)).repr);
            Assert.AreEqual("array([ True,  True,  True,  True,  True,  True,  True,  True,  True,\n        True,  True,  True])", ((data >= 50) | (data < 100)).repr);
            Console.WriteLine(((data >= 50) ^ (data < 100)).repr);
            Assert.AreEqual("array([ True,  True,  True,  True, False, False, False, False, False,\n        True,  True,  True])", ((data >= 50) ^ (data < 100)).repr);
        }
xiaozhu1988

xiaozhu1988 commented on Sep 1, 2024

@xiaozhu1988
Author
henon

henon commented on Sep 1, 2024

@henon
Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @henon@xiaozhu1988

        Issue actions

          NDArray operation '&' · Issue #129 · SciSharp/Numpy.NET