matlab编程 根据机器学习的知识编写程序,创建LDA(线性判别分析)建模与分类函数,并load fisheriris,对其中的两个类别(1~50,51~100)的特征进行分类识别。

下面是一个使用MATLAB编写LDA(Linear Discriminant Analysis)建模与分类函数的示例程序:

% Load the fisheriris data set
load fisheriris;

% Select two classes (1~50, 51~100) for classification
X = [meas(1:50,:); meas(51:100,:)];
y = [ones(50,1); 2*ones(50,1)];

% Compute the means of the two classes
mu1 = mean(X(y==1,:));
mu2 = mean(X(y==2,:));

% Compute the within-class scatter matrix
S1 = zeros(size(X,2));
for i = 1:50
    if y(i) == 1
        S1 = S1 + (X(i,:) - mu1)'*(X(i,:) - mu1);
    end
end

S2 = zeros(size(X,2));
for i = 51:100
    if y(i) == 2
        S2 = S2 + (X(i,:) - mu2)'*(X(i,:) - mu2);
    end
end

Sw = S1 + S2;

% Compute the LDA projection vector
w = inv(Sw)*(mu2 - mu1)';

% Project the data onto the LDA vector
X_lda = X * w;

% Perform classification on the projected data
y_pred = zeros(size(X,1),1);
for i = 1:size(X,1)
    if X_lda(i) > 0
        y_pred(i) = 1;
    else
        y_pred(i) = 2;
    end
end

% Compute the classification accuracy
ac
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值