1. 程式人生 > >機器學習技法第一次作業

機器學習技法第一次作業

import os
import random
import math
os.chdir('D:/developEnvironment/libsvm-3.20/python')
from svmutil import *
trainfile='E:/ML/Taiwan_ML/homework2/15/train0.txt'
gamaArr=[0,0,0,0,0]
for time in range(0,100):
    acc=0
    n_acc=0
    print(str(time)+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
    for i in range(0,5):
        parameter='-c 0.1 -g '+str(math.pow(10,i))
        y, x = svm_read_problem(trainfile)
        yt=list()
        xt=list()
        for j in range(0,1000):
            index=random.randint(0,len(x)-1)
            yt.append(y[index])
            xt.append(x[index])
            del y[index]
            del x[index]
        model = svm_train(y, x, parameter)
        p_label, p_acc, p_val = svm_predict(yt, xt, model)
        if(p_acc[0]>acc):
            acc=p_acc[0]
            n_acc=i
        print(parameter)
        print(p_acc[0])
        print(acc)
        print(n_acc)
    gamaArr[n_acc]=gamaArr[n_acc]+1
    print(gamaArr)