How to fill in the gaps in the crossfilter group.all() results? - crossfilter

I have a data set defined as such:
var hour = data.crossfilter.dimension(function(d){ return d.date.getHours() + d.date.getMinutes() / 60; });
var hours = hour.group(Math.floor);
The generated result (hours.all()) looks like this:
[0, 124]
[1, 25]
[2, 5]
[3, 2]
[5, 339]
[6, 11648]
[7, 16334]
[8, 15919]
[9, 14078]
[10, 13816]
[11, 14646]
[12, 15079]
[13, 13718]
[14, 13947]
[15, 14055]
[16, 13995]
[17, 16166]
[18, 14958]
[19, 13991]
[20, 12769]
[21, 10638]
[22, 3939]
[23, 892]
Notice that there is a data gap between 3, 5 (4 is missing). How to configure crossfilter to fill in the gaps with 0 instead?

I think you'll have to fill them in yourself, because I don't see how Crossfilter could group a value you don't give to it. That responsibility should fall to whatever object you're using to present the data.
Are you using d3? I think the linear scale would do that automatically. If not, it would be pretty trivial to do manually.

Related

Using gather() to retrieve rows from 3d tensor with 2d tensor in Pytorch

I'm new to Pytorch and having an issue with the gather() function:
I have a 3d tensor, x[i,j,k]:
x=tensor([[[1,2,3],
[4,5,6],
[7,8,9]],
[[10,11,12],
[13,14,15],
[16,17,18]]])
I have an index tensor:
index=tensor([[1,2,0]])
I want to use the values of index to iterate over x[j] and fetch the (complete) rows. I've tried gather() with all dims, squeezing, unsqueezing and it never seems to get the output I'm looking for, which would be:
output=tensor([[[4,5,6],
[7,8,9],
[1,2,3]],
[[13,14,15],
[16,17,18],
[10,11,12]]])
I've also tried repeating the values of index to get the same shape as x but it did not work.
I know I can do this with an if loop, but I'm pretty sure I can do it with gather() as well. Thanks for the help
Let us set up the two tensors x and index:
>>> x = torch.arange(1,19).view(2,3,3)
>>> index = torch.tensor([[1,2,0]])
What you are looking for is the torch.gather operation:
out[i][j][k] = x[i][index[i][j][k]][k]
In other to apply this function, you need to expand index to the same shape as out. Additionally, a transpose operation is required to flip your original index tensor.
>>> i = index.T.expand_as(x)
tensor([[[1, 1, 1],
[2, 2, 2],
[0, 0, 0]],
[[1, 1, 1],
[2, 2, 2],
[0, 0, 0]]])
If you compare with the pseudo code line above, you can see how every element of i represents the row of the original tensor x the operator will gather values from.
Applying the function gets us to the desired result:
x.gather(dim=1, index=index.T.expand_as(x))
tensor([[[ 4, 5, 6],
[ 7, 8, 9],
[ 1, 2, 3]],
[[13, 14, 15],
[16, 17, 18],
[10, 11, 12]]])

Is there a basic implementation of bellman ford's algorithm in julia?

I'm a first year student who's learning Julia as a first programming language. I have a project about bellman ford's algorithm but it seems every code is a bit more advanced than I can currently understand. Is there a basic code like Dfs or Bfs for this that a starter could understand, if u have, do share.
This is implemented in LightGraphs
using LightGraphs
g = erdos_renyi(20, 100, seed=1)
bf_state = bellman_ford_shortest_paths(g, 1)
And now we can display all paths found in the graph:
julia> enumerate_paths(bf_state)
20-element Vector{Vector{Int64}}:
[]
[1, 4, 2]
[1, 3]
[1, 4]
[1, 5]
[1, 11, 6]
[1, 7]
[1, 3, 8]
[1, 3, 9]
[1, 7, 10]
[1, 11]
[1, 12]
[1, 3, 13]
[1, 3, 14]
[1, 15]
[1, 4, 16]
[1, 17]
[1, 3, 18]
[1, 19]
[1, 5, 20]

Sum of integers with restrictrions

Getting right to the gist of the problem:
In how many ways can we add k positive integers to reach a sum of exactly n if each number is smaller or equal to given number m?
The problem is solvable with dynamic programming but I am stuck because I cannot find the optimal substructure or recursion for the solution.
Here's a simple function in Python 3 that should fit your description. I assume that 0 is not an acceptable value but it's a trivial change if it is.
def howMany(k, n, m):
def sub(pos, currentSum, path):
if currentSum == n and pos == k: # reached the sum, print result and increase counter by 1
print(path)
return 1
elif currentSum < n and pos < k: # still worth trying
count = 0
for i in range(1, m):
count += sub(pos + 1, currentSum + i, path+[i])
return count
else: # abort
return 0
return sub(0, 0, [])
print(howMany(3, 10, 6))
yields
[1, 4, 5]
[1, 5, 4]
[2, 3, 5]
[2, 4, 4]
[2, 5, 3]
[3, 2, 5]
[3, 3, 4]
[3, 4, 3]
[3, 5, 2]
[4, 1, 5]
[4, 2, 4]
[4, 3, 3]
[4, 4, 2]
[4, 5, 1]
[5, 1, 4]
[5, 2, 3]
[5, 3, 2]
[5, 4, 1]
18
It could be optimised but that would obfuscate the logic at this stage.

SML: Combining Two Lists

I have the following function for combining two lists into one. It's supposedly of type:
# : 'a list * 'a list -> 'a list
fun # (nil, k) = k
| # (x::l, k) = x :: #(l,k);
Let's say we have two lists: [1, 2, 3] and [4, 5, 6]. If I call:
#([1, 2, 3], [4, 5, 6])
1::#([2, 3], [4, 5, 6])
1::2::#([3], [4, 5, 6])
1::2::3::#(nil, [4, 5, 6])
But here we reach the base case and our # call returns the list [4, 5, 6], yielding:
1::2::3::[4, 5, 6]
which is obviously not what I want. Is the function definition correct or am I misunderstanding something?
Yes your function definition is correct.
:: or Cons as it is called in Lisp and other functional programming languages is used for creating lists. It takes a value and a list (which may be empty) and creates a new list with the former prepended to the latter. So for example 42::[17, 23] equals [42, 17, 23].
Cons is right associative which means that your list
1::2::3::[4,5,6]
can be written as
(1::(2::(3::[4,5,6])))
and by successive reductions we get
[1,2,3,4,5,6]

Swift for-in loop dictionary experiment

I'm almost a complete programming beginner and I've started to go through a Swift ebook from Apple.
The things I read are pretty clear, but once you start to experiment things get tricky :).
I'm stuck with the experiment in the Control Flow section. Here is the initial code:
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]
var largest = 0
for (kind, numbers) in interestingNumbers {
for number in numbers {
if number > largest {
largest = number
}
}
}
largest
And here is the task:
Add another variable to keep track of which kind of number was the
largest, as well as what that largest number was.
As I understand, they want me to add up all the values in each number kind (get a total sum for Prime, Fibonacci and Square) and then compare the result to show the largest result.
But I can't figure out the syntax.
Can someone share any advice on how to tackle this experiment?
Maybe I'm not understanding the problem?
They're just asking you to keep track of which number category the largest number belongs to:
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]
var largest = 0
var largestkind = ""
for (kind, numbers) in interestingNumbers {
for number in numbers {
if number > largest {
largest = number
largestkind = kind
}
}
}
largest
largestkind
Alternately you can use closure to make the tasks simpler.
The for loop calculate the sum of each series.
The final reduce finds the series tuple that contains maximum number.
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]
var sums = Array<(String, Int)>()
for (kind, numbers) in interestingNumbers {
sums = sums + [(kind, numbers.reduce(0, +))]
}
let maxSeries = sums.reduce(("", Int.min), { $0.1 > $1.1 ? $0 : $1 })
println(sums)
println(maxSeries)
Here it from playground using Xcode 8.3 and Swift 3.0
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]
let largest = interestingNumbers.map{$0.value}.flatMap{$0}.max()
print(largest)
Optional(25)

Resources