0

I am a student and new to the concept of Artificial intelligence using Tensorflow library.I have the below code and I need an interpretation for the part that has === "setosa" ? 1 : 0,

  //tensor of output for training data
  const outputData = tf.tensor2d(
  iris.map((item) => [
  item.species === "setosa" ? 1 : 0,
  item.species === "virginica" ? 1 : 0,
  item.species === "versicolor" ? 1 : 0,
  ])
);

What does this ? 1 : 0 mean here.

1
  • 1
    It means: if item.species is the same as "setosa" then it is 1 else it is 0. The outcome (1 or 0) will be then placed in your array. Commented Apr 10, 2021 at 1:41

1 Answer 1

1

The ? : operator is called a Conditional (ternary) operator

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy. This operator is frequently used as a shortcut for the if statement.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

In your case either 1 or 0 will be the outcome.
Imagine it being like the if - else functionality. If the statement before the question mark ? is not false or undefined the resulting value is 1 if not it's 0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.