Usage of Pydatalog Aggregate Functions - logic-programming

I have been playing around with the various aggregate functions to get a feel for them, and after being confused for the past few days I am in need of clarification. I either get completely unintuitive behavior or unhelpful errors. For instance, I test:
(p[X]==min_(Y, order_by=Z)) <= Y.in_((4,6,2)) & Z.in_((6,))
looking at sample output:
p[0]==X,Y,Z
([(6,)], [4, 6, 2], [6, 6, 6])
p[1]==X,Y,Z
([(6,)], [6, 4, 2], [6, 6, 6])
p[2]==X,Y,Z
([(6,)], [4, 2, 6], [6, 6, 6])
Why is the minimum 6? 2. Why has the value bound to Z been repeated 3 times? 3. What exactly is the purpose of 'order_by' in relation to the list from which a minimum value is found? 4. Why does the output change based upon if there are multiple values in the 'order_by' list; why does a specific value--6, in this case--in the 'order_by' list effect the output as it has? Another example:
(p[X]==min_(Y, order_by=Z)) <= Y.in_((4,6,2)) & Z.in_((0,))
Output:
p[0]==X,Y,Z
([(6,)], [4, 6, 2], [0, 0, 0])
p[1]==X,Y,Z
([(6,)], [2, 6, 4], [0, 0, 0])
p[2]==X,Y,Z
([(2,)], [2, 6, 4], [0, 0, 0])
Why did the output of X change--from 6 to 2--based upon the indexed provided? Even though the output was wrong in the previous example, at least it was consistent for the indexes used; with there only being one min/max, this makes since.
I at least get to see the output using the min_, max_, sum_ functions; but, I am lost when it comes to rank_ and running_sum_. I follow a similar process when defining my function:
(p[X]==running_sum_(Z, group_by=Z, order_by=Z)) <= Z.in_((43,34,65))
I try to view the output:
p[0]==X
I get the error:
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/UserList.py", line 16, in repr
def repr(self): return repr(self.data)
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyParser.py", line 109, in data
self.todo.ask()
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyParser.py", line 566, in ask
self._data = Body(self.pre_calculations, self).ask()
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyParser.py", line 686, in ask
self._data = literal.lua.ask()
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyEngine.py", line 909, in _
invoke(subgoal)
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyEngine.py", line 664, in invoke
todo.do() # get the thunk and execute it
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyEngine.py", line 640, in do
self.thunk()
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyEngine.py", line 846, in
aggregate.complete(base_subgoal, subgoal))
File "/usr/local/lib/python3.4/dist-packages/pyDatalog/pyParser.py", line 820, in complete
result = [ tuple(l.terms) for l in list(base_subgoal.facts.values())]
AttributeError: 'bool' object has no attribute 'values'
What does this mean? What was done incorrectly? What are the relations shared by the running_sum_ (and rank_) parameters--'group_by' and 'order_by'?
As there seems to be no examples on the web, 2 or 3 short examples of rank_ and running_sum_ usage would be greatly appreciated.

Aggregate clauses are solved in 2 steps :
first resolve the unknowns in the clause, while ignoring the aggregate function
then apply the aggregate function on the result
Here is how you could write the first clause :
(p[None]==min_(Y, order_by=Y)) <= Y.in_((4,6,2))
The variable(s) in the bracket after p is used as the "group by" in SQL, and must also appear in the body of the clause. In this case, it does not vary, so I use None. The order_by variable is needed when you want to retrieve another value than the one you order by.
Let's say you want to retrieve the names of the youngest pupil in each class of a school. The base predicate would be pupil(ClassName, Name, Age).
+ pupil('1A', 'John', 8)
+ pupil('1B', 'Joe', 9)
The aggregate clause would be :
(younger[ClassName] == min_(Name, order_by= Age)) <= pupil(ClassName, Name, Age)
The query would then be :
(younger[ClassName]==X)

Related

Two if statements in a for loop?

class Solution:
def transformArray(self, arr: List[int]) -> List[int]:
x=arr
while True:
f=True
for i in range(1,len(arr)-1):
if arr[i-1]<arr[i] and arr[i]>arr[i+1]:
f=False
x[i]=x[i]-1
print(x[i])
if arr[i-1]>arr[i] and arr[i]<arr[i+1]:
f=False
x[i]=x[i]+1
print(x[i])
#print(x)
x=arr
if f==True:
break
return x
In the above code both the if statements don't execute , only the second one does. I have tried using elif but it still doesn't work. What am i missing here?
For your code, I considered two types of examples as input for the array list
For example 1, when the arr = [1, 2, 1, 4], the 2nd element is bigger than 1st and 3rd
The first if statement (if arr[i-1]<arr[i] and arr[i]>arr[i+1]:) is working, because both the conditions are met and it gives the output x = [1, 1, 1, 4]
In example 2, when the arr = [3, 2, 3, 4], the 2nd element is smaller than the 1st and 3rd
The second if statement (if arr[i-1]>arr[i] and arr[i]<arr[i+1]:) is working, because both the conditions are met and it gives the output x = [3, 3, 3, 4]
So, the working of if statements largely depends on the elements in the array. Both the if statements' purpose is totally opposite. If one satisfies the condition the other will not.
Hope my answer provides some clarification.

Tensorflow JS Probabilties

i have multiple feature columns and a result column where i want to predict if something happens or not.
so I'm training my model and finally i do
const predictions = model.predict(xTest).argMax(-1);
this returns a tensor and when getting the data with:
predictions.dataSync ()
i get values like [0, 1, 1, 1, 0, ...]
is there any way to get probabilities like in python? [0.121, 0.421, 0.8621, ...]
I only found one result:
https://groups.google.com/a/tensorflow.org/g/tfjs/c/TvcB69MUj_I?pli=1
is this still the case? are there no probabilities in javascript?
tf.argMax returns the indices of the maximum value along the axis. If you rather want to have the maximum value itself you could use tf.max instead
const x = tf.tensor2d([[1, 2, 3],[ 4, 8, 4]]);
x.max(-1).print() // [3, 8]
x.argMax(-1).print() // [2, 1]

What would be the best data structure to search and update integer values of an array?

If I have the array as [7, 11, 13, 9, 4, 6], and the user input is 10
I want the integer just above (or equal to ) 10 (in this case 11) and replace it with integer - 10 (11 - 10)
I can't order the list and binary search as I have to return the index of updated element.
I looked into ordering the (index, integer) pairs and then binary searching but then after updating I have to re sort the array of pairs for next user input.
original array = [7, 11, 13, 9, 4, 6]
sorted array = [4, 6, 7, 9, 11, 13]
user input = 10
output = 1 (index of updated integer)
updated sorted array = [4, 6, 7, 9, 1, 13]
re sorted array = [1, 4, 6, 7, 9, 13]
user input = 4
...
What data structure would be suitable to implement this?
You can use an ordered set to store (value, index) tuples. You can define custom comparison functions to fit your goal (it depends on the programming language in question).
Ordered set operations include:
Add an element.
Remove an element.
Search for an element.
Search for an element greater or equal than given element.
Search for an element greater than given element.
The given problem can be solved using an ordered set with operations as described above.
Some programming languages (like set in c++) have predefined data structures so you don't need to implement it from scratch (only to use it). Ordered set internal implementation usually is Red-Black Tree (c++) or AVL Tree.
Check if your programming language has a built-in ordered set or use some library that include it. It's a very useful and common data structure so it shouldn't be hard to find.

whats wrong with the for loop in the list used?

I am trying to define a list using a for loop:
import numpy as np
# 2 input neurons , next 3 hidden , 5 hidden , 2 output neurons
layer_sizes = { 2,3,5,2 }
# for matrix shapes 3,2 5,3 and 2,5
weight_shapes = [{a,b} for a,b in zip(layer_sizes[1:],layer_sizes[:-1])]
#weight_shapes = [ {3,2},{5,3},{2,5}]
weights = [np.zeros(s) for s in weight_shapes]
print(weight_shapes)
print(weights)
But I kept getting this error:
Traceback (most recent call last):
File "C:\Users\USER\NNe2.py", line 5, in <module>
weight_shapes = [{a,b} for a,b in zip(layer_sizes[1:],layer_sizes[:-1])]
TypeError: 'set' object is not subscriptable
I expect print statements to provide the content of the lists.
Your layer_sizes is the set {2, 3, 5},
but your comment suggests you were hoping it would be a list of length 4,
as if it were declared in this way:
layer_sizes = [2, 3, 5, 2]

Ruby: Remove first and last element of an Array - why the solution works one way & not the other

I's like to know why the second solution works but the first one, which has chained methods, doesn't work.
This chained method doesn't work:
nopers = [5, 6, 7, 8, 9]
class Array
define_method(:trimy) do
self.shift().pop()
end
end
When I test it, nopers.trimy(), it gives an undefined error message. "method 'pop' for 1:Fixnum, in 'block in '" and only executes the .pop() method, removing the 5.
But, this version works:
yuppers = [1, 2, 3, 4, 5, 6]
class Array
define_method(:trim) do
self.shift()
self.pop()
end
end
yuppers.trim()
When I test it, yuppers gives me: [2, 3, 4, 5]
I would say that:
yuppers[1..-2]
is the most simple solution
This is because both shift and pop return the value that is removed:
[1, 2, 3].pop # => returns 3
[1, 2, 3].shift # => returns 1
So when you chain them together you're calling #pop on the result of #shift, which is an Integer which isn't allowed.

Resources