<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

use strict;
use warnings;
use File::Copy;
use Statistics::R;

my $inputData = "E:\\NUS\\Project\\SVM\\Data\\data.txt";
my $path = "E:\\NUS\\Project\\SVM\\Results\\";

open DATA,"&lt;","$inputData" or die "$!";
chomp(my $data = &lt;DATA&gt;);
close DATA;
my @data = split /\t/,$data;
my $total = scalar(@data);
copy("$inputData","$path"."data_$total.txt") or die "$!";

open LOG,"&gt;","$path"."log.txt" or die "$!";
my $outputData = "";
while($total &gt; 2){
   my $inputData = "data_"."$total.txt";
#   print LOG "$input:\n";
   open IN,"&lt;","$path"."$inputData" or die "$!";
   my $drop = $total - 1;
   $outputData = "data_"."$drop.txt";
   my %drop_acc = ();
   for my $count (2..$total){
   &amp;drop($count);
   my $R = Statistics::R -&gt; new();
   $R -&gt; startR;
   $R -&gt; send(qq'library(class)');
   $R -&gt; send(qq'library(e1071)');
   $R -&gt; send(qq'svmdata &lt;- read.table(past($path,"$outputData",sep = ""),sep = "\t")');
   $R -&gt; send(qq'n &lt;- length(svmdata)');
   $R -&gt; send(qq'trainset &lt;- svmdata[1:20,2:n]');
   $R -&gt; send(qq'trainlabel &lt;- svmdata[1:20,1]');
   $R -&gt; send(qq'testset &lt;- svmdata[21:30,2:n]');
   $R -&gt; send(qq'testlabel &lt;- svmdata[21:30,1]');
   $R -&gt; send(qq'model &lt;- svm(trainset,trainlabel,type="C-classification")');
   $R -&gt; send(qq'predlabel &lt;- predict(model,testset)');
   $R -&gt; send(qq'result &lt;- table(predlabel,testlabel)');
   $R -&gt; send(qq'acc &lt;- (result[1,1]+result[2,2])/(result[1,1]+result[1,2]+result[2,1]+result[2,2])');
   $R -&gt; send(qq'print(acc)');
   my $read = $R -&gt; read;
   $read =~ s/\[\d\]\s+(\d+)/$1/g;
   $drop_acc{$count} = $read;
   #$R -&gt; send(qq'write.table(acc,"D:/personalfolders/liuyifang/SVM/Results/$read.txt",sep="\t",row.names=F,col.names=F)');
   $R -&gt; stopR();
   }
   #print "$read\n";
   my @keys = sort{$drop_acc{$a} &lt;=&gt; $drop_acc{$b}} keys %drop_acc;
   for(@keys){print LOG "$_ -&gt; $drop_acc{$_}\n";}
   print LOG "drop: $keys[0]\n";
   &amp;drop($keys[0]);
   $total--;
}

sub drop {
    my $drop = shift;
    open OUT,"&gt;","$path"."$outputData" or die "$!";
    seek IN,0,0;
    while(&lt;IN&gt;){
           chomp;
           my @data = split /\t/;
           for my $index (0..$total-1){
               print OUT $data[$index],"\t" if $index+1 != $drop;
           }
           print OUT "\n";
    }
    close OUT;
#    system(qq(perl -i".bak" -p -e "~s/\t\n/\n/g" D:\\personalfolders\\liuyifang\\SVM\\Data\\$outputData));
}</pre></body></html>