Created
June 2, 2016 09:15
-
-
Save zxteloiv/43414731e3d05f74ba9bf6fab5f5864f to your computer and use it in GitHub Desktop.
do 10-fold cross-validation using libsvm and the iris dataset.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
libsvmpath="./libsvm-3.21" | |
# 1. build the libsvm | |
cd $libsvmpath | |
make | |
if [ $? -ne 0 ]; then | |
echo "failed to build, exit"; | |
exit 1; | |
fi | |
cd ../ | |
# 2. set the config variables | |
dataset=./iris.scale.all | |
model=$libsvmpath/iris.model | |
trainbin=$libsvmpath/svm-train | |
predictbin=$libsvmpath/svm-predict | |
output=./iris.predicted.out | |
fold=10 # do 10-fold cross validation | |
gamma_choices="0.001 0.005 0.01 0.05 0.1 0.5 1 2 5 10 20 50 100 200 1000" | |
# 3. find the parameter that achieves the best 10-fold accuracy | |
maxacc=0 | |
maxgamma=0 | |
for gamma in $gamma_choices; do | |
acc=$($trainbin -g $gamma -v $fold $dataset $model | tail -1 | awk -F '[ \t%]' '{print $(NF - 1)}') | |
echo "gamma=$gamma acc=$acc" | |
cond=$(echo "$acc > $maxacc" | bc) | |
if [ $cond -gt 0 ]; then | |
maxacc=$acc | |
maxgamma=$gamma | |
echo "max gamma changed: $gamma" | |
fi | |
done | |
# 4. retrain the model using the best parameter | |
$trainbin -g $gamma $dataset $model | |
# 5. use the model to do testing | |
$predictbin $dataset $model $output | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment