def svrmodel(traininput, traintarget, testinput, testtarget): # [points, label] = weeklydataset_shogun(filesource, []) # clusteredpoints, cdata = create_clustered_samples(points, 5, 1) # cluster0 = clusteredpoints[0] # input, target = aggregatebymins_sg(cluster0[0]) ## testinput, testtarget = aggregatebymins_sg(cluster0[0]) # print len(input) # traininput, traintarget, testinput, testtarget = traintest(input, target, 20, 1) svr = SVR(traininput, testinput, traintarget,2,256,0.1,0.5) out = svr.svr_req(testinput) sme = svr.calc_sme(testtarget, out) x = array(testinput, dtype=int32) y = array(testtarget, dtype=int32) xp = array(testinput, dtype=int32) yp = array(out, dtype=int32) fig = figure() ax1 = fig.add_subplot(1,1,1) ax1.plot(x, y) ax1.plot(xp,yp,"r") ax1.axis([0,max(x)+10,0,max(y)+10]) ax1.set_xlabel('minutes of the week') ax1.set_ylabel('number of requests') fig.savefig("svr_model_%f" % (time.time()), format='png') print "SME = %f" % sme
def svr(C, gamma, eps): #initialization of data wmproxy traininput, traintarget, testinput, testtarget = initialize_wmproxy() #training of the SVR #scaling values in training and test targets for i in range(len(traintarget)): if(traintarget[i] != 0): traintarget[i] = log(traintarget[i]) if(traininput[i] != 0): traininput[i] = log(traininput[i]) for i in range(len(testtarget)): if(testtarget[i] != 0): testtarget[i] = log(testtarget[i]) if(testinput[i] != 0): testinput[i] = log(testinput[i]) avg = mean(traintarget) sigma = std(traintarget) maxtrain = len(traintarget) C = max([abs(avg + sigma), abs(avg - sigma)]) print "C is equal to %f" % C svr = SVR(traininput[maxtrain-1440:maxtrain], testinput, traintarget[maxtrain-1440:maxtrain],gamma,C,eps,eps) out = svr.svr_req(testinput[0:30]) error = 0 for i in range(len(out)): error += (out[i] - testtarget[i]) mean_error = error / len(out) variance = 0 for i in range(len(out)): variance = abs(out[i] - mean_error) variance /= len(out) print "Variance = %f" % variance epsilon = 3*variance*sqrt(log(len(out))/len(out)) print "Epsilon = %f" % epsilon #calculation of the metrics sme = svr.calc_sme(testtarget[0:30], out) mape = svr.calc_mape(out, testtarget[0:30]) predx = svr.calc_pred(out, testtarget[0:30], 25) rsq = svr.calc_rsqr(out, testtarget[0:30]) print out print testtarget[0:30] # print model results! x = array(testinput[0:30], dtype=int32) y = array(testtarget[0:30], dtype=int32) xp = array(testinput[0:30], dtype=int32) yp = array(out, dtype=int32) fig = figure() ax1 = fig.add_subplot(1,1,1) ax1.title.set_text("Predizioni modello SVR con C= %f, Gamma = %f, Eps = %f" % (C, gamma, eps)) realvalues = ax1.plot(x, y) predictedvalues = ax1.plot(xp,yp,"r") ax1.axis([8.9,max(xp)+0.5,0,max(y)+10]) ax1.set_xlabel('minutes of the week') ax1.set_ylabel('number of requests') legend([realvalues,predictedvalues], ["Real Values","Predicted Values"]) fig.savefig("svr_model_%f" % time(), format='png') print "SME = %f" % sme print "MAPE = %f" % mape print "R^2 = %f" % rsq print "PREDX = %f" % predx