⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pca_parzen_smooth.m

📁 用mushrooms数据对模式识别课程讲述的各种模式分类方法[线性分类
💻 M
字号:
clc;clear;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%读取数据,取16个特征%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%samples = textread('data2000.txt');samples = samples(:,[1:6,9:15,19:22]);  %17列 第1列标号,16列特征%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%对样本进行归一化处理%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%[ms ns]=size(samples);TMax=max(samples);TMin=min(samples);% 第一列是样本标签,从第二列开始归一化for i=2:ns    samples(:,i)=(samples(:,i)-TMin(i))/(TMax(i)-TMin(i));end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%对样本进行降维,PCA变换%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%k_reduction=10;%降维后的维数cov_data=cov(samples(:,2:end));[pc,latent,explained] = pcacov(cov_data);pc1=pc(:,1:k_reduction);pc1=pc1';y=pc1*samples(:,2:end)';y = [samples(:,1)';y];dimenReduct_samples = y';%带标签for w = 1:5    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %将样本分为测试样本,第一类训练样本,第二类训练样本    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    p = randperm(2000);%对1:2000的整数随机排序    experiment_test=dimenReduct_samples(p(1:100),:);%测试样本    exper_test=experiment_test(:,2:end);%测试样本,不带标签    experiment_train=dimenReduct_samples(p(101:300),:);%训练样本    index1=find(experiment_train(:,1)==1);%找到训练样本中第一类的行号    index2=find(experiment_train(:,1)==2);%找到训练样本中第二类的行号    exper_train_class1=experiment_train(index1,2:end);%训练样本里属于第一类的样本,不带标签    exper_train_class2=experiment_train(index2,2:end);%训练样本里属于第二类的样本,不带标签    [m n]=size(exper_test);    [m1 n1]=size(exper_train_class1);    [m2 n2]=size(exper_train_class2);    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %计算类条件概率密度    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %构造一个正态函数,以每个训练样本为中心(均值)                                                        SIGMA=eye(k_reduction);    R1=zeros(m,1);%记录待测样本在第一类的测试结果    R2=zeros(m,1);%记录待测样本在第二类的测试结果    %对第一类    Result=zeros(m,1);%记录最后分类结果    for i=1:m        for j=1:m1            R1(i)=R1(i)+mvnpdf(exper_test(i,:),exper_train_class1(j,:),SIGMA);        end    end    R1=R1./m1;    %对第二类    for i=1:m        for j=1:m2            R2(i)=R2(i)+mvnpdf(exper_test(i,:),exper_train_class2(j,:),SIGMA);        end    end    R2=R2./m2;    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %画类条件概率密度图,并分类    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %绘图    plot(R1,'-r','LineWidth',2);    hold on;    plot(R2,'-b','LineWidth',2);    xlabel ('待测样本');     ylabel ('类条件概率密度 P(x|wi)');    title ('类条件概率密度图');    %分类    for i=1:m        if R1(i,1)>R2(i,1)            Result(i,1)=1;        else            Result(i,1)=2;        end    end    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    %分析结果    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    [correct(w,1),error(w,1),ROC(w,:)] = analyse_result(experiment_test,Result);end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -