LogQL Group dynamic request in one line - grafana-loki

sum (count_over_time({filename=“/var/log/nginx/access.log”}[1m] | pattern <_> <_> "<_> <request> <_>" <_> <_> "<_>" "<_>" "<_>" <_> <_> |~ “/json//users/*********” )) by(request)
I need to merge into one line. Otherwise it's overloaded.

Related

Decision tree training for Multi-View face detection

I am working for multi-view face detection and following the Jones's multiple-view face detection algorithm.
In the paper "Fast Multi-view Face Detection", Jones trained C4.5 decision tree with images of different face poses. In Section 3.3 of Decision Tree Training, it is mentioned as "the training algorithm is almost identical to the boosting algorithm. The two main differences are the criteria for feature selection and the splitting of the training set at each node"
I learned and understood C4.5 algorithm here.
I can't figure out how to train images of different face poses for C4.5 decision tree.
EDIT 1:
Stage 0 and Stage 1 features of training the ADABOOST algorithm for cascade classifier is shown below.
<!-- stage 0 -->
<_>
<maxWeakCount>3</maxWeakCount>
<stageThreshold>-0.7520892024040222</stageThreshold>
<weakClassifiers>
<!-- tree 0 -->
<_>
<internalNodes>
0 -1 46 -67130709 -21569 -1426120013 -1275125205 -21585
-16385 587145899 -24005</internalNodes>
<leafValues>
-0.6543210148811340 0.8888888955116272</leafValues></_>
<!-- tree 1 -->
<_>
<internalNodes>
0 -1 13 -163512766 -769593758 -10027009 -262145 -514457854
-193593353 -524289 -1</internalNodes>
<leafValues>
-0.7739216089248657 0.7278633713722229</leafValues></_>
<!-- tree 2 -->
<_>
<internalNodes>
0 -1 2 -363936790 -893203669 -1337948010 -136907894
1088782736 -134217726 -741544961 -1590337</internalNodes>
<leafValues>
-0.7068563103675842 0.6761534214019775</leafValues></_></weakClassifiers></_>
<!-- stage 1 -->
<_>
<maxWeakCount>4</maxWeakCount>
<stageThreshold>-0.4872078299522400</stageThreshold>
<weakClassifiers>
<!-- tree 0 -->
<_>
<internalNodes>
0 -1 84 2147483647 1946124287 -536870913 2147450879
738132490 1061101567 243204619 2147446655</internalNodes>
<leafValues>
-0.8083735704421997 0.7685696482658386</leafValues></_>
<!-- tree 1 -->
<_>
<internalNodes>
0 -1 21 2147483647 263176079 1879048191 254749487 1879048191
-134252545 -268435457 801111999</internalNodes>
<leafValues>
-0.7698410153388977 0.6592915654182434</leafValues></_>
<!-- tree 2 -->
<_>
<internalNodes>
0 -1 106 -98110272 1610939566 -285484400 -850010381
-189334372 -1671954433 -571026695 -262145</internalNodes>
<leafValues>
-0.7506558895111084 0.5444605946540833</leafValues></_>
<!-- tree 3 -->
<_>
<internalNodes>
0 -1 48 -798690576 -131075 1095771153 -237144073 -65569 -1
-216727745 -69206049</internalNodes>
<leafValues>
-0.7775990366935730 0.5465461611747742</leafValues></_></weakClassifiers></_>
EDIT2:
My consideration for how to train the decision is described in the following picture
I am still figuring out what are the features to use, but I think the training should be as shown in the attached image.
Thanks
Did not read the paper but frm what I know from early face recognition experiments, the attributes you are looking for are probably just the grey level inputs of the face images. Usually, images are rescaled, say to 32x32 pixels, so you have a 1024 dimensionnal vector to train your decision tree. Have a closer look at the article if they use other features, they will be written, or at least given a reference to?

Suggest Data Structure which implement this

Can any one suggest any data structure/code such that if we map 1 to a, 2 to b, 3 to c i.e.
1 --> a
2 --> b
3 --> c
then we can also reverse lookup such as if i query 'a' then it should output 1 and similarly 2 and 3 for 'b' and 'c' respectively.
Also if i change the mapping afterwards from above mapping to :
1 --> a
2 --> a
3 --> c
then after reverse lookup i should get (1,2) for 'a' and (3) for 'c'.
The number to letter direction: a hash.
For the letter to number direction: multimap (or a hash where each value is a list)

Using XPATH to get child nodes having index divisible by n

My XML is some how like this:
<a>
<b></b> <!-- 1 -->
<b></b> <!-- 2 -->
<b></b> <!-- 3 -->
<b></b> <!-- 4 -->
<b></b> <!-- 5 -->
<b></b> <!-- 6 -->
<b></b> <!-- 7 -->
<b></b> <!-- 8 -->
<b></b> <!-- 9 -->
<b></b> <!-- 10 -->
<b></b> <!-- 11 -->
</a>
Is the a way to write XPATH that get child nodes of a having index divisible by n? For example, with n = 3, it will get b nodes with index 3, 6, 9.
get child nodes of a having index divisible by n
Like this:
/a/*[position() mod 3 = 0]
Note that position() returns a 1-based index.

golang array referencing eg. b[1:4] references elements 1,2,3

The golang blog states :
"A slice can also be formed by "slicing" an existing slice or array. Slicing is done by specifying a half-open range with two indices separated by a colon. For example, the expression b[1:4] creates a slice including elements 1 through 3 of b (the indices of the resulting slice will be 0 through 2)."
Can someone please explain to me the logic in the above. IE. Why doesn't b[1:4] reference elements 1 through 4? Is this consistent with other array referencing?
Indexes point to the "start" of the element. This is shared by all languages using zero-based indexing:
| 0 | first | 1 | second | 2 | third | 3 | fourth | 4 | fifth | 5 |
[0] = ^
[0:1] = ^ --------> ^
[1:4] = ^-------------------------------------> ^
[0:5] = ^ ----------------------------------------------------------> ^
It's also common to support negative indexing, although Go doesn't allow this:
|-6 | |-5 | |-4 | |-3 | |-2 | |-1 |
| 0 | first | 1 | second | 2 | third | 3 | fourth | 4 | fifth | 5 |
The reason is given in the Go Language Specification section on Slices.
For a string, array, or slice a, the
primary expression
a[low : high]
constructs a substring or slice. The
index expressions low and high select
which elements appear in the result.
The result has indexes starting at 0
and length equal to high - low.
For convenience, any of the index
expressions may be omitted. A missing
low index defaults to zero; a missing
high index defaults to the length of
the sliced operand.
It's easy and efficient to calculate the length of the slice as high - low.
Half-open intervals make sense for many reasons, when you get down to it. For instance, with a half-open interval like this, the number of elements is:
n = end - start
which is a pretty nice and easy formula. For a closed interval, it would be:
n = (end - start) + 1
which is (not a lot, but still) more complicated.
It also means that for e.g. a string, the entire string is [1, len(s)] which also seems intuitive. If the interval was closed, to get the entire string you would need [1, len(s) + 1].
Go uses half-open intervals for slices like many other languages. In a more mathematical notation, the slice b[1:4] is the interval [1,4) which excludes the upper endpoint.

Matrix, algorithm interview question

This was one of my interview questions.
We have a matrix containing integers (no range provided). The matrix is randomly populated with integers. We need to devise an algorithm which finds those rows which match exactly with a column(s). We need to return the row number and the column number for the match. The order of of the matching elements is the same. For example, If, i'th row matches with j'th column, and i'th row contains the elements - [1,4,5,6,3]. Then jth column would also contain the elements - [1,4,5,6,3]. Size is n x n.
My solution:
RCEQUAL(A,i1..12,j1..j2)// A is n*n matrix
if(i2-i1==2 && j2-j1==2 && b[n*i1+1..n*i2] has [j1..j2])
use brute force to check if the rows and columns are same.
if (any rows and columns are same)
store the row and column numbers in b[1..n^2].//b[1],b[n+2],b[2n+3].. store row no,
// b[2..n+1] stores columns that
//match with row 1, b[n+3..2n+2]
//those that match with row 2,etc..
else
RCEQUAL(A,1..n/2,1..n/2);
RCEQUAL(A,n/2..n,1..n/2);
RCEQUAL(A,1..n/2,n/2..n);
RCEQUAL(A,n/2..n,n/2..n);
Takes O(n^2). Is this correct? If correct, is there a faster algorithm?
you could build a trie from the data in the rows. then you can compare the columns with the trie.
this would allow to exit as soon as the beginning of a column do not match any row. also this would let you check a column against all rows in one pass.
of course the trie is most interesting when n is big (setting up a trie for a small n is not worth it) and when there are many rows and columns which are quite the same. but even in the worst case where all integers in the matrix are different, the structure allows for a clear algorithm...
You could speed up the average case by calculating the sum of each row/column and narrowing your brute-force comparison (which you have to do eventually) only on rows that match the sums of columns.
This doesn't increase the worst case (all having the same sum) but if your input is truly random that "won't happen" :-)
This might only work on non-singular matrices (not sure), but...
Let A be a square (and possibly non-singular) NxN matrix. Let A' be the transpose of A. If we create matrix B such that it is a horizontal concatenation of A and A' (in other words [A A']) and put it into RREF form, we will get a diagonal on all ones in the left half and some square matrix in the right half.
Example:
A = 1 2
3 4
A'= 1 3
2 4
B = 1 2 1 3
3 4 2 4
rref(B) = 1 0 0 -2
0 1 0.5 2.5
On the other hand, if a column of A were equal to a row of A then column of A would be equal to a column of A'. Then we would get another single 1 in of of the columns of the right half of rref(B).
Example
A=
1 2 3 4 5
2 6 -3 4 6
3 8 -7 6 9
4 1 7 -5 3
5 2 4 -1 -1
A'=
1 2 3 4 5
2 6 8 1 2
3 -3 -7 7 4
4 4 6 -5 -1
5 6 9 3 -1
B =
1 2 3 4 5 1 2 3 4 5
2 6 -3 4 6 2 6 8 1 2
3 8 -7 6 9 3 -3 -7 7 4
4 1 7 -5 3 4 4 6 -5 -1
5 2 4 -1 -1 5 6 9 3 -1
rref(B)=
1 0 0 0 0 1.000 -3.689 -5.921 3.080 0.495
0 1 0 0 0 0 6.054 9.394 -3.097 -1.024
0 0 1 0 0 0 2.378 3.842 -0.961 0.009
0 0 0 1 0 0 -0.565 -0.842 1.823 0.802
0 0 0 0 1 0 -2.258 -3.605 0.540 0.662
1.000 in the top row of the right half means that the first column of A matches on of its rows. The fact that the 1.000 is in the left-most column of the right half means that it is the first row.
Without looking at your algorithm or any of the approaches in the previous answers, but since the matrix has n^2 elements to begin with, I do not think there is a method which does better than that :)
IFF the matrix is truely random...
You could create a list of pointers to the columns sorted by the first element. Then create a similar list of the rows sorted by their first element. This takes O(n*logn).
Next create an index into each sorted list initialized to 0. If the first elements match, you must compare the whole row. If they do not match, increment the index of the one with the lowest starting element (either move to the next row or to the next column). Since each index cycles from 0 to n-1 only once, you have at most 2*n comparisons unless all the rows and columns start with the same number, but we said a matrix of random numbers.
The time for a row/column comparison is n in the worst case, but is expected to be O(1) on average with random data.
So 2 sorts of O(nlogn), and a scan of 2*n*1 gives you an expected run time of O(nlogn). This is of course assuming random data. Worst case is still going to be n**3 for a large matrix with most elements the same value.

Resources