unable to optimise the following using linprog
2 views (last 30 days)
Malloju Dinesh on 9 Feb 2022
I am trying to implement equation 9(in the image attached below) in linprog .. the v vector has (w,r): w is the weights vector which i have to find so that r is min. . i am trying to generate weights vector from this optmisation to be used in KNN(k-nearest neighbors) algorithm. I have evaluated the A and b matrices from the data set ...
1)The size of my data set is 100*4(4 is the number of features which is D)
2)size of f , v vector is (D+1) which is 5
3)size of A(10000*5), b(10000*1).I have evaluated A and b matrices from the data set.So i have A and b.
4)The vector to be optmised is v which is represented as(w,r).w is weights vector(size 4) which i have to find so that r
5)r is the maximum of r1,r2. r1:maximum of intraclass1 distance. r2: maximum of intraclass2 distance.
The each observation of data set belongs to two classes (1 or 2).
6)distance is the mahalanobis weighted distance between two observations.
Let me know step by step approach on how to solve it .Thanks . if any further information is required let me know ..thanks
Abolfazl Chaman Motlagh on 9 Feb 2022
HI. here's how you should do it:
First i create a sample database for my self to use as parameters: (i use 100 points in 4D feature space with 3 label, change every part for your problem)
N = 100; % number of points
D = 4; % Dimension of feature space
Class_count = 3; % number of Classes
%% Create random dataset for parametrization
X = rand(N,D); % Position of Points in feature space
Y = randi([1 Class_count],N,1); % Label of Points
Now let us create Matrix of Coefficients A, right-hand-side vector B and Cost weights f.
f = [zeros(1,D) 1];
A = zeros(N^2,D+1);
b = zeros(N^2,1);
I = (i-1)*N + j;
A(I,:) = [((X(i,:)-X(j,:)).^2) -1];
b(I) = 0;
A(I,:) = [-((X(i,:)-X(j,:)).^2) 0];
b(I) = -1;
lb = zeros(D+1,1); % lower bound
Now time to use linprog:
Solution = linprog(f,A,b,,,lb,) % no equality constraint and no upper bound
W = Solution(1:D)