unable to optimise the following using linprog
1 次查看(过去 30 天)
显示 更早的评论
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
is minimum.
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/888620/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/888620/image.png)
Let me know step by step approach on how to solve it .Thanks . if any further information is required let me know ..thanks ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/888500/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/888500/image.jpeg)
0 个评论
采纳的回答
Abolfazl Chaman Motlagh
2022-2-9
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);
for i=1:N
for j=1:N
I = (i-1)*N + j;
if Y(i)==Y(j)
A(I,:) = [((X(i,:)-X(j,:)).^2) -1];
b(I) = 0;
else
A(I,:) = [-((X(i,:)-X(j,:)).^2) 0];
b(I) = -1;
end
end
end
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!