Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3. import math
  4. import statistics
  5. import visualizer
  6. import numpy as np
  7. from datagen import constructData
  8. from sklearn import svm
  9.  
  10. # Applies Support Vector Regression to the electricity dataset,
  11. # prints out the accuracy rate to the terminal and plots
  12. # predictions against actual values
  13. def suppVectorRegress():
  14.  
  15. kernelList = ["linear","rbf",polyKernel]
  16. names = ["linear","radial basis","poly"]
  17. preds = []
  18.  
  19. # Retrieve time series data & apply preprocessing
  20. data = constructData()
  21.  
  22. cutoff = len(data)-30
  23. xTrain = data[0][0:cutoff]
  24. yTrain = data[1][0:cutoff]
  25. xTest = data[0][cutoff:]
  26. yTest = data[1][cutoff:]
  27.  
  28. # Fill in missing values denoted by zeroes as an average of
  29. # both neighbors
  30. statistics.estimateMissing(xTrain,0.0)
  31. statistics.estimateMissing(xTest,0.0)
  32.  
  33. # Logarithmically scale the data
  34. xTrain = [[math.log(y) for y in x] for x in xTrain]
  35. xTest = [[math.log(y) for y in x] for x in xTest]
  36. yTrain = [math.log(x) for x in yTrain]
  37.  
  38. # Detrend the time series
  39. indices = np.arange(len(data[1]))
  40. trainIndices = indices[0:cutoff]
  41. testIndices = indices[cutoff:]
  42. detrended,slope,intercept = statistics.detrend(trainIndices,yTrain)
  43. yTrain = detrended
  44.  
  45. for gen in range(len(kernelList)):
  46.  
  47. # Use SVR to predict test observations based upon training observations
  48. pred = svrPredictions(xTrain,yTrain,xTest,kernelList[gen])
  49. # Add the trend back into the predictions
  50. trendedPred = statistics.reapplyTrend(testIndices,pred,slope,intercept)
  51. # Reverse the normalization
  52. trendedPred = [np.exp(x) for x in trendedPred]
  53. # Compute the NRMSE
  54. err = statistics.normRmse(yTest,trendedPred)
  55.  
  56. print ("The Normalized Root-Mean Square Error is " + str(err) + " using kernel " + names[gen] + "...")
  57.  
  58. preds.append(trendedPred)
  59.  
  60. names.append("actual")
  61. preds.append(yTest)
  62.  
  63. # Change the parameters 2017,2,1 based on the month you want to predict.
  64. visualizer.comparisonPlot(2017,2,1,preds,names,plotName="Support Vector Regression Load Predictions vs. Actual",
  65. yAxisName="Predicted Kilowatts")
  66.  
  67. # Construct a support vector machine and get predictions
  68. # for the test set
  69. # Returns a 1-d vector of predictions
  70. def svrPredictions(xTrain,yTrain,xTest,k):
  71. clf = svm.SVR(C=2.0,kernel=k)
  72. clf.fit(xTrain,yTrain)
  73. return clf.predict(xTest)
  74.  
  75. # A scale invariant kernel (note only conditionally semi-definite)
  76. def polyKernel(x,y):
  77. return (np.dot(x,y.T)+1.0)**0.95
  78.  
  79. if __name__=="__main__":
  80. suppVectorRegress()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement