I have following code to calculate the EuclideanDistance distance using weka.core.EuclideanDistance, where both two instances are all missing values, like below
Instance first are all missing values: ?,?,?,?
instance second are all missing values:?,?,?,?
EuclideanDistance distance = new EuclideanDistance();
distance.setInstances(test);
Instance first = test.get(0);
Instance second = test.get(1);
double d = distance.distance(first, second);
however, when i run the code, i got the result is 4.0, i have no idea where is this 4.0 from,can anyone tell me? Thanks in advance!
Missing values in k-Nearest Neighbours algorithm usually are handled according to the following criteria:
For nominal attributes:
if isMissingValue(a) or isMissingValue(b), then
distance = 1
For numeric attributes:
if isMissingValue(a) and isMissingValue(b), then
distance = 1
if isMissingValue(a) and !isMissingValue(b), then
distance = max(b, 1-b)
if !isMissingValue(a) and isMissingValue(b), then
distance = max(a, 1-a)
You can check the implementation in the source (link provided by Walter).
Related
I'm made a program that calculates the line of best fit of a set of data points using gradient descent. I generate a 1000 random points and then it calculates the line of best fit training on these 1000 points. My confusion lies in the theory of my code.
In the part of my code where the training function is, by using the current m and b values for y= mx +b, the function makes a guess of the y values when it goes through the training points x values. This is supervised learning, so I know what the actual y value is, the function calculates the error and using that error adjusts the m and b values. <-- What is happening in the program when adjusting the line of best fit
I get everything above ^. what I'm confused about is the part of the code that calculates how to adjust these m and b values. Here it is:
guess = m * x + b;
error = y - guess;
m = m + (error * x) * learningrate;
b = b + error * learningrate;
Im confused about why we add instead of subtract that delta m (the (error * x) *learningrate)) part. Ignoring the learningrate, the error * x part is the partial derivative of the error with respect to m. But if we took the partial derivative of something with respect to something, wouldn't it give us the direction of the steepest ascent? Shouldn't we go the opposite direction (subtract the delta m) to get the proper m value? Isn't our goal to reduce the error?
Surprisingly to me, the above code works, if you add the delta m, it adjusts the m and b values in the right direction. So basically my question is: Why aren't we subtracting the delta m part (error *x) as it is pointing in the direction of steepest ascent, and we want to get the opposite of that?
Thanks!
After reading several posts about getting the 2D transformation of 2D points from one image to another, estimateRigidTransform() seems to be the recommendation. I'm trying to use it. I modified the source code (to change the RANSAC parameters, because its hardcoded, and the hardcoded parameters are not very good)(the source code for this function is in lkpyramid.cpp). I have read up on how RANSAC works, and am trying to understand the steps in estimateRigidTransform().
// choose random 3 non-complanar points from A & B
...
// additional check for non-complanar vectors
a[0] = pA[idx[0]];
a[1] = pA[idx[1]];
a[2] = pA[idx[2]];
b[0] = pB[idx[0]];
b[1] = pB[idx[1]];
b[2] = pB[idx[2]];
double dax1 = a[1].x - a[0].x, day1 = a[1].y - a[0].y;
double dax2 = a[2].x - a[0].x, day2 = a[2].y - a[0].y;
double dbx1 = b[1].x - b[0].x, dby1 = b[1].y - b[0].y;
double dbx2 = b[2].x - b[0].x, dby2 = b[2].y - b[0].y;
const double eps = 0.01;
if( fabs(dax1*day2 - day1*dax2) < eps*std::sqrt(dax1*dax1+day1*day1)*std::sqrt(dax2*dax2+day2*day2) ||
fabs(dbx1*dby2 - dby1*dbx2) < eps*std::sqrt(dbx1*dbx1+dby1*dby1)*std::sqrt(dbx2*dbx2+dby2*dby2) )
continue;
Is it a typo that it uses non-coplanar vectors? I mean the 2D points are all on the same plane right?
My second question is what is that if condition doing? I know that the left hand side (gives the area of triangle times 2) would be zero or near zero if the points are collinear, and the right hand side is the multiplication of the lengths of 2 sides of the triangle.
Collinearity is preserved in affine transformations (such as the one you are probably estimating), but this transformations also calculate also changes in rotations in point of view (as if you rotated the object in a 3d world). However, these points will be collinear as well, so for the algorithm it may have not a unique solution. Look at the pictures:
imagine selecting 3 center points of each black square in the first row in the first image. Then map it to the same centers in the next image. It may generate a mapping to that solution, but also a mapping to a zoom version of the first one. The same may happen with the third one, just that this time may map to a zoom out version of the first one (without any other change). However if the points are not collinear, for example, 3 corner squares centers, it will find a unique mapping.
I hope this helps you to clarify your doubts. If not, leave a comment
I'm trying to modify OpenCV's haar Cascade Classifier into specific purpose and looked into its source code from up to down. But I stuck on understanding very final part which is calculating feature value in line 361 of cascadedetect.hpp
return optfeaturesPtr[featureIdx].calc(pwin) * varianceNormFactor;
and its sub part for (a) calculating each rectangles weighted sum in line 398-407 of cascadedetect.hpp
inline float HaarEvaluator::OptFeature :: calc( const int* ptr ) const
{
float ret = weight[0] * CALC_SUM_OFS(ofs[0], ptr) +
weight[1] * CALC_SUM_OFS(ofs[1], ptr);
if( weight[2] != 0.0f )
ret += weight[2] * CALC_SUM_OFS(ofs[2], ptr);
return ret;
}
and (b) calculating varianceNormFactor in line 691-697 and 701 of cascadedetect.cpp
pwin = &sbuf.at<int>(pt) + s.layer_ofs;
const int* pq = (const int*)(pwin + sqofs);
int valsum = CALC_SUM_OFS(nofs, pwin);
unsigned valsqsum = (unsigned)(CALC_SUM_OFS(nofs, pq));
double area = normrect.area();
double nf = area * valsqsum - (double)valsum * valsum;
line:701 varianceNormFactor = (float)(1./nf);
From my little knowledge I guess 'pwin' represents actual window which is currently in progress, but (Q1) What does pq mean? I couldn't find any line which gives value into sbuf variable (which somehow connected to everything above)
According to Rainer Lienhart's paper which was mentioned by OpenCV's team they used this equation for light correction. But in this paper they said we can calculate σ just by looking at 4 values in integral image from each pixel's square. (Q2) But aren't we supposed to subtract average from each pixels BEFORE taking sum of squares in order to calculate standard deviation or σ in this paper represent something different?
And from source I think equation they used must be similar to this instead. (Q3) So If possible, Where can I get maths behind these codes or detailed reference material for this part? I've red OpenCV's reference manual but didn't find anything about it.
(Q1) What does pq mean:
I think it may implies "Pointer to Square";
OpenCV may calculate the Square of all pixels and append it to a buffer B.
So when do normalization, it can quickly refer to it by:
the offset of current window in B, which is pwin.
the offset of square value for each pixel within current window in B based on pwin, which is sqofs.
(Q2) But aren't we supposed to subtract average from each pixels BEFORE taking sum of squares in order to calculate standard deviation > or σ in this paper represent something different?
I have checked it on OpenCV 3.4.4 and, yes, it does NOT subtract the mean.
I guess since for Haar pattern is symmetry, eg for Horizontal 2 one region subtract a region with the same area. Hence the effect sums up to zero and we can ignore it.
Im trying to calculate the angle between two edges in a graph, in order to do that I transfer both edges to origin and then used dot product to calculate the angle. my problem is that for some edges like e1 and e2 the output of angle(e1,e2) is -1.#INDOO.
what is this output? is it an error?
Here is my code:
double angle(Edge e1, Edge e2){
Edge t1 = e1, t2 = e2;
Point tail1 = t1.getTail(), head1 = t1.getHead();
Point u(head1.getX() - tail1.getX(), head1.getY() - tail1.getY());
Point tail2 = t2.getTail(), head2 = t2.getHead();
Point v(head2.getX() - tail2.getX(), head2.getY() - tail2.getY());
double dotProduct = u.getX()*v.getX() + u.getY()*v.getY();
double cosAlpha = dotProduct / (e1.getLength()*e2.getLength());
return acos(cosAlpha);
}
Edge is a class that holds two Points, and Point is a class that holds two double numbers as x and y.
Im using angle(e1,e2) to calculate the orthogonal projection length of a vector like b on to a vector like a :
double orthogonalProjectionLength(Edge b, Edge a){
return (b.getLength()*sin(angle(b, a) * (PI / 180)));
}
and this function also sometimes gives me -1.#INDOO. you can see the implementation of Point and Edge here.
My input is a set S of n Points in 2D space. Iv constructed all edges between p and q (p,q are in S) and then tried to calculate the angle like this:
for (int i = 0; i < E.size(); i++)
for (int j = 0; j < E.size(); j++){
if (i == j)
cerr << fixed << angle(E[i], E[j]) << endl; //E : set of all edges
}
If the problem comes from cos() and sin() functions, how can I fix it? is here other libraries that calculate sin and cos in more efficient way?
look at this example.
the inputs in this example are two distinct points(like p and q), and there are two Edges between them (pq and qp). shouldnt the angle(pq , qp) always be 180 ? and angle(pq,pq) and angle(qp,qp) should be 0. my programm shows two different kinds of behavior, sometimes angle(qp,qp) == angle(pq,pq) ==0 and angle(pq , qp) == angle(pq , qp) == 180.0, and sometimes the answer is -1.#INDOO for all four edges.
Here is a code example.
run it for several times and you will see the error.
You want the projection and you go via all this trig? You just need to dot b with the unit vector in the direction of a. So the final answer is
(Xa.Xb + Ya.Yb) / square_root(Xa^2 + Ya^2)
Did you check that cosAlpha doesn't reach 1.000000000000000000000001? That would explain the results, and provide another reason not to go all around the houses like this.
It seems like dividing by zero. Make sure that your vectors always have 0< length.
Answer moved from mine comment
check if your dot product is in <-1,+1> range ...
due to float rounding it can be for example 1.000002045 which will cause acos to fail.
so add two ifs and clamp to this range.
or use faster way: acos(0.99999*dot)
but that lowers the precision for all angles
and also if 0.9999 constant is too big then the error is still present
A recommended way to compute angles is by means of the atan2 function, taking two arguments. It returns the angle on four quadrants.
You can use it in two ways:
compute the angles of u and v separately and subtract: atan2(Vy, Vx) - atan2(Uy, Ux).
compute the cross- and dot-products: atan2(Ux.Vy - Uy.Vx, Ux.Uy + Vx.Vy).
The only case of failure is (0, 0).
I'm trying to implement some code from here
And I have trained the HMM with my coefficients but do not understand how the Viterbi Decoder algorithm works, for example:
viterbi_decode(MFCC, M, model, q);
where MFCC = coefficents
M = size of MFCC
model = Model of HMM training using the MFCC coefficients
q = unknown (believed to be the outputted path).
But here is what I do not understand: I am attempting to compare two speech signals (training, sample) to find out the closest possible match. With the DTW algorithm for example, a single integer is returned where I can then find the closest, however, with this algorithm it returns a int* array and therefore differentiating is difficult.
Here is how the current program works:
vector<DIMENSIONS_2> MFCC = mfcc.transform(rawData, sample_rate);
int N = MFCC.size();
int M = 13;
double** mfcc_setup = setupHMM(MFCC, N, M);
model_t* model = hmm_init(mfcc_setup, N, M, 10);
hmm_train(mfcc_setup, N, model);
int* q = new int[N];
viterbi_decode(mfcc_setup, M, model, q);
Could anyone please tell me how the Viterbi Decoder works for the problem of identifying which is the best path to take from the training, to the input? I've tried both the Euclidean distance as well as the Hamming Distance on the decode path (q) but had no such luck.
Any help would be greatly appreciated
In this example it seems to me that (q) is the hidden state sequence, so a list of numbers from 0->9. If you have two audio samples say, test and train, and you generate two sequences q_test and q_train, then thinking about |q_test - q_train|, where the norm is componentwise distance, is not useful because it isn't representing a notion of distance correctly, since hidden state labels in HMM may be arbitrary.
A more natural way to think about distance may be the following, given q_train, you are interested in the probability that your test sample took that same path, which you can compute once you have the transition matrix and emission probabilites.
Please let me know if I am misunderstanding your question.