Simplest_Selected_Feature_Matlab_Code

function select_feature_use_index(is_last_column_label, o_data_path, select_indexes, save_seleced_data_path)
% is_last_column_label :    1表示数据文件的最后一列是类别,0表示数据文件中没有类别信息
% o_data_path :             原始数据文件
%                           如果is_last_column_label==1 则size为(sample_num*feature_num+1)
%                           如果is_last_column_label==0 则size为sample_num*feature_num
% select_indexes :          存储要选择特征下标的向量,size为1*select_indexes_num
% save_seleced_data_path :  原始数据文件
%                           如果is_last_column_label==1 则size为(sample_num*select_indexes_num+1)
%                           如果is_last_column_label==0 则size为sample_num*select_indexes_num
o_data = load(o_data_path);
[sample_num o_data_column_num] = size(o_data);
select_indexes_num = size(select_indexes, 2);
if 1==is_last_column_label
    select_indexes_num = select_indexes_num + 1;
    select_indexes = [select_indexes, o_data_column_num];
end
selected_data = zeros(sample_num, select_indexes_num);
for R=1:select_indexes_num
    selected_data(:, R) = o_data(:, select_indexes(1, R));
end


if 1==is_last_column_label
    writeToLIBSVMFile(selected_data, save_seleced_data_path);
else
    writeToFile(selected_data, save_seleced_data_path);
end
end


function writeToFile(Matrix, fileName)
%param         Matix 表示待写入的矩阵 N*c
%param         fileName 表示要写入的文件的路径名称
%function      该函数实现了按行优先存储,把Matrix矩阵写入fileName文件中
%CopyRight     NUST CS726 Jun.H(111060881)
fid = fopen(fileName, 'w');
[N c] = size(Matrix);
for R=1:N
    for L=1:c
        fprintf(fid, '%.6f\t', Matrix(R, L));
    end
    fprintf(fid, '\n');
end
fclose(fid);
end


function writeToLIBSVMFile(feature_label_matrix, savePath)


[H L] = size(feature_label_matrix);
feature_label = feature_label_matrix(:, L);     %记录下样本的类标号  类标号在文件的最后一列
feature_data = feature_label_matrix(:, 1:L-1);  %存储除去类标号后的数据信息
%获取集合每个向量的长度
[Num, Long] = size(feature_data);
%-----------------------------------------------------------
                     
% write to  file
fid = fopen(savePath, 'w');
for R=1:Num
    if feature_label(R, 1)~= 1
        fprintf(fid, '-1 ');
    else
        fprintf(fid, '+1 ');
    end
    for C=1:Long
        fprintf(fid, '%d:%.7f ', C, feature_data(R,C));
    end
    fprintf(fid, '\n');
end
fclose(fid);
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jun-H

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值