Group Abstract Group Abstract

Message Boards Message Boards

How to list the supported functions on a variable?

Posted 3 years ago

Hi, How to find what methods are supported on a variable. In the following code "trainingData" is a variable that holds 60K image and its numeric value in the image.

trainingData = ResourceData["MNIST", "TrainingData"]

If I want to browse the images how to do it. I guessed this might behave as list(https://www.wolfram.com/language/fast-introduction-for-programmers/en/lists/) and tried

trainingData[[5]]

which worked. But how to do this more systematically without guessing. Since in Mathematica everything is a symbol, is there a way to list all functions supported on a variable?

PS: I am new to Mathematica. I have intermediate experience in Matlab and Python. What I am looking for is similar to the "methods" function in Matlab or "dir" function in Python. Thanks

POSTED BY: Bob Rick
4 Replies
Posted 3 years ago

Does the "Rule" function actually supports list syntax. ie. using "[ ]"

Don't think about it as "list syntax". The double square brackets, [[]], is just syntactic sugar. It's just a convenient alternative to Part. Part is just a normal "function" that can be applied to any expression.

Rohit showed you how to inline the selection function. The Last function that it uses is very common. Other useful "destructuring" functions are Head, First, Most, Rest, and Part. Part has some interesting features, including things similar to Python's slice function.

When filtering/searching lists, Select, Cases, and Position are frequently used. Reorganizing lists can be done with GroupBy and GatherBy.

That's just the tip of the iceberg.

POSTED BY: Eric Rimbey
Posted 3 years ago

Thanks, Eric for this wonderful overview.

You can use Head to determine the head of the expression

I was looking for something like this. The idea is to learn about the variable by querying it.

I want to retrieve all the "0"s in the trainingData and store them in another variable. Basically, filter by the label. Looks like this function https://reference.wolfram.com/language/ref/Select.html seems appropriate.

The "Head" method on a single element of "trainingData[[1]]" output it is "Rule". Looks like "lhs" is image and "rhs" is integer value. I could not find the documentation for retrieving the "rhs" of "Rule". By experimentation, I found the list syntax works. ie. "trainingData[[1]][[2]]" outputs "0".

With this knowledge, I could retrieve all the samples containing 0's to a variable.

fzero[s_] := s[[2]] == 0
zerosTrainingData = Select[trainingData, fzero]

Two follow-up questions. 1. Does the "Rule" function actually supports list syntax. ie. using "[ ]". https://reference.wolfram.com/language/ref/Rule.html

  1. Can the same be written without using "fzero". i.e could the same be achieved in a single line.
POSTED BY: Bob Rick
Posted 3 years ago

Hi Bob,

Can the same be written without using "fzero". i.e could the same be achieved in a single line.

trainingData // Select[Last@# == 0 &]

Does the "Rule" function actually supports list syntax. ie. using "[ ]"

r = 1 -> 2
FullForm@r
(* Rule[1, 2] *)

Part[r, 0]
(* Rule *)

Part[r, 1]
(* 1 *)

Part [[ ]] can be used on any non-atomic symbol.

r[[1]]
(* 1 *)
POSTED BY: Rohit Namjoshi
Posted 3 years ago
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard