EDIT At the suggestion of #HighPerformanceMark, I've moved the question to mathematica.stackexchange.com: my question, so I attempted to close the question here. But SO doesn't allow me to do it properly, hence this up-front warning.
Setup
Say, I'm given a dataset, like the one below:
titanic = ExampleData[{"Dataset", "Titanic"}]; titanic
Answering with:
And I want to count the occurrences of any combination between { "1st", "2nd"} and {"female", "male"}, using the Counts operator on the dataset, like:
genderclasscounts = titanic[All, {"class", "sex"}][Counts]
Problem statement
This is not a "flat" dataset and I don't have a clue how to query in the usual way, like:
genderclasscount[Select[ ... ], ...]
The resulting dataset doesn't provide "column" names to be used as parameters in the Select nor can I refer to the number representing the count by a name.
And I've no clue how to express an Association as a value in a Select!?
Furthermore, try genderclasscount[Print], this demonstrates the values presented to the operation over this dataset are just numbers!
An unsatisfactory attempt
Of course, I can "flatten" the Counts result, by doing something horrific and inefficient like:
temp = Dataset[(row \[Function]
AssociationThread[{"class", "sex", "count"} -> row]) /# (Nest[
Normal, genderclasscounts, 3] /.
Rule[{Rule["class", class_], Rule["sex", sex_]},
count_] -> {class, sex, count})]
In this form it is easy to query a count result:
First#temp[Select[#class == "1st" \[And] #sex == "female" &], "count"]
Question
So, my questions are
How can I query the (immediate) result of the Count operation in a convenient and efficient fashion, like using a Select operation on the resulting dataset? Or, if that is not possible;
Is there an efficient and convenient transformation of the Counts result dataset possible facilitating such a query? With "convenient" I mean, for example, that you just provide the dataset and the transformation handles the rest. So, not something like I've shown above in my unsatisfactory "solution" ;-)
Thanks for reading this far and I'm looking forward to anwsers and inspiration.
/#nanitous
Good day! I would like to ask for help in understanding Prolog code as well as learning how to extend its functionality. Based on my limited yet newbie knowledge of programming, the function of the first given code (is quite like a main file), everything originates from it and thus it is the first file that is run before calling any other function.
main.pl
printSequences([]).
printSequences([Sequence|Sequences]):-
writeln(Sequence),
printSequences(Sequences).
loadHelpers:-
['helpers'],
['part01'],
['part02'],
['part03'],
['part04'].
part01:-
readExtremePegSolitaireFile('test04.eps',_,Game),
printGame(Game),
columnsAndRows(Game).
part02:-
readExtremePegSolitaireFile('part01test01.eps',_,Game),
printGame(Game),
openSpaces(Game).
part03:-
readExtremePegSolitaireFile('test04.single.eps',_,Game),
printGame(Game),
setof(Moves,fewestMoves(Game,Moves),AllMoves),
writeln(moves),
printSequences(AllMoves).
part04:-
readExtremePegSolitaireFile('test04.eps',_,Game),
printGame(Game),
noIslands(Game).
I don't think I have any problems understanding the first given code above, but my problem is mostly with this second given code and how to go about manipulating other files. I can't seem to understand the prefix part (is this the definition of a list of lists?) Also am I correct that most of the other functions are declared in this helper file to make the code more organized?
helpers.pl
:- module( helpers,
[ readExtremePegSolitaireFile/3
, printGame/1
]
).
prefix([H],[]).
prefix([H|T],[H|PreT]):-
prefix(T,PreT).
readExtremePegSolitaireFile(File,Moves,Game):-
open(File,read,Input),
read(Input,Moves),
readGame(Input,Temp),
prefix(Temp,Game),
close(Input).
readGame(Input,[]):-
at_end_of_stream(Input),
!.
readGame(Input,[Row|Rows]):-
\+ at_end_of_stream(Input),
read(Input,Row),
readGame(Input,Rows).
printGame(Game):-
writeln(game),
printRows(Game).
printRows([]).
printRows([Row|Rows]):-
writeln(Row),
printRows(Rows).
Last is a peg solitaire board that is given with the first line being the list of moves performed and the following lines are the board declarations (1,2,3,4 - players, x - peg, and '-' as empty spaces)
test04.eps
[r,d,u,r,l,l,l,d,l,u,r,r].
[2,-,x,x,x,x,x].
[x,x,-,x,-,x,x].
[x,3,x,-,x,-,x].
[x,-,4,x,x,x,x].
[x,x,-,x,x,-,x].
[x,x,x,1,-,x,x].
[x,x,x,x,x,x,x].
I would like to know how one would be able to calculate the number of columns and rows via a query columnsAndRows(Game). My first plan of action was to use something like this: (Which would be able to calculate the length of the rows by counting each element in the list however, it seems to have calculated all the elements in the list. 2 things that I noticed was:
It didn't stop at the end of the row
Apparently it didn't print the entire board, it was missing the last line of the board!
columnsAndRows(Game) :-
flatten(Game, FlatList),
length(FlatList,FlatListSize),
writeln(FlatListSize).
?- [a04tests].
?- loadHelpers.
?- part01.
game
[2,-,x,x,x,x,x]
[x,x,-,x,-,x,x]
[x,3,x,-,x,-,x]
[x,-,4,x,x,x,x]
[x,x,-,x,x,-,x]
[x,x,x,1,-,x,x]
42
true
I'm honestly really lost and I'd appreciate any guidance as to where to begin, or even a process flow for this program. Many thanks!
.//*[preceding::*[text()='Taco Salad'] and following::*[text()='Fajita Salad']]
I have text on this page and both queries for .//*[text()='Taco Salad'] and .//*[text()='Fajita Salad'] return what I expected. But I am a bit confused how to try to combine these with following and preceeding to grab some of the options nodes that present in the middle.
Does anyone have any working examples of using two axes?
I found this thread: combining XPATH axes (preceding-sibling & following-sibling) and tried to model my axes after it, but they don't seem to be valid xapths. Am I missing something obvious?
Unfortunately you haven't really told us what you want to achieve. Yes, you can use multiple axes; you can combine them in various different ways, depending on what result you want. So you need to explain what result you want. Using "and" at the top level simply tests whether both operands of the "and" select something.
Your question "does anyone have any working examples" isn't going to help. Yes, we can give you thousands of working examples, but it's entirely possible that none of them does what you want.
Here are some possible ways of combining two axis steps X and Y:
X/Y - select X, and from there, select Y
X and Y - return true if both X and Y select something
X or Y - return true if either X or Y selects something
X | Y - return the union of what X and Y select
X intersect Y - return the intersection of what X and Y select
The nearest you have come to a requirements statement is "grab some of the options nodes that present in the middle". That's hard to interpret without seeing your source document. It also might turn out to be a query that's easier in XPath 2.0 than in 1.0, so you really need to tell us which version you are using.
A working example of combining axes in single xpath can be:
//*[following-sibling::*[#class="bottom-notice"] and preceding-sibling::*[#name="new-answer"]]
It looks for the element form to write the answer on this page.
I'd like to calculate the standard deviation over two fields from the same dataset.
example:
MyFields1 = 10, 10
MyFields2 = 20
What I want now, is the standard deviation for (10,10,20), the expected result is 4.7
In SSRS I'd like to have something like this:
=StDevP(Fields!MyField1.Value + Fields!MyField2.Value)
Unfortunately this isn't possible, since (Fields!MyField1.Value + Fields!MyField2.Value) returns a single value and not a list of values. Is there no way to combine two fields from the same dataset into some kind of temporary dataset?
The only solutions I have are:
To create a new Dataset that contains all values from both fields. But this is very annoying because I need about twenty of those and I have six report parameters that need to filter every query. => It's probably getting very slow and annoying to maintain.
Write the formula by hand. But I don't really know how yet. StDevP is not that trivial to me. This is how I did it with Avg which is mathematically simpler:
=(SUM(Fields!MyField1.Value)+SUM(Fields!MyField2.Value))/2
found here: http://social.msdn.microsoft.com/Forums/is/sqlreportingservices/thread/7ff43716-2529-4240-a84d-42ada929020e
Btw. I know that it's odd to make such a calculation, but this is what my customer wants and I have to deliver somehow.
Thanks for any help.
CTDevP is standard deviation.
Such expression works fine for me
=StDevP(Fields!MyField1.Value + Fields!MyField2.Value) but it's deviation from one value (Fields!MyField1.Value + Fields!MyField2.Value) which is always 0.
you can look here for formula:
standard deviation (wiki)
I believe that you need to calculate this for some group (or full dataset), to do this you need set in the CTDevP your scope:
=StDevP(Fields!MyField1.Value + Fields!MyField2.Value, "MyDataSet1")
I heard that we can use the English words to express the number in Mathematica. Like using One hundred to express 100. Which function can do it?
A solution basically equivalent to dreeves's solution (but not available at the time of his answer) would be to call WolframAlpha[] directly from Mathematica (this requires an internet connection). For example,
WolframAlpha["6 million 2 hundred and 12 thousand and fifty two",
{{"Input", 1}, "Plaintext"}]
returns the string
"6212052"
So we can construct the following function that returns the actual number
textToNumber[num_String] :=
Module[{in = WolframAlpha[num, {{"Input", 1}, "Plaintext"}]},
If[StringMatchQ[in, NumberString], ToExpression[in], $Failed]]
It also works with decimals and negative numbers, e.g., textToNumber["minus one point one"].
Note that we could ask for things other than "Plaintext" output. The easiest way to find out what's available is to enter some number, eg,WolframAlpha["twelve"], and explore the options available when you press the ⨁ signs on the right of each "pod". It is also worth exploring the documentation, where you find useful output "formats" such as "MathematicaParse" and "PodIDs".
We can also go in the other direction:
numberToText[num_Integer] := WolframAlpha[ToString[num],
{{"NumberName", 1}, "Plaintext"}]
I couldn't find the right incantations to get the spoken phrase form for non-integers. If someone knows the right spell, or if W|A gains this ability, please feel free to update this answer. It's a shame that SpokenString does not have an option for reading numbers as their spoken phrases.
I see that Wolfram Alpha can do that, so here's a kludgy little function that sends the English string to Wolfram Alpha and parses the result:
w2n[s_String] := ToExpression[StringCases[
Import["http://www.wolframalpha.com/input/?i=" <> StringReplace[s, " "->"+"],
"String"],
RegularExpression["Hold\\[([^\\]]*)\\]"] -> "$1"][[1]]]
Example:
w2n["two million six hundred sixty-six"]
> 2000666
Does Wolfram Alpha provide an actual API? That would be really great!
PS: They have one now but it's expensive: http://products.wolframalpha.com/api/
PPS: I notice that the wolframalpha results page changed a bit and my scraping no longer works. Some variant on that regular expression should work though.
This is the code:
IntegerName[78372112345]
This is the output:
78 billion 372 million 112 thousand 345
not available back in '09...
SemanticInterpretation["one hundred fifty thousand three hunded and six"]
or
Interpreter["SemanticNumber"]["one hundred fifty thousand three hunded and six"]
150306
( notice my spelling error didn't phase it..)
The two functions are not the same by the way,
SemanticInterpretation["six and forty two thousandths"]//N (* 6.042 *)
Interpreter["SemanticNumber"]["six and forty two thousandths"] (*fails*)