Browse Source

started prediction work

Nikolaos Alexopoulos 7 years ago
parent
commit
ae100b787f
4 changed files with 74 additions and 26 deletions
  1. BIN
      .apt-sec.py.swp
  2. 1 0
      .gitignore
  3. 12 7
      apt-sec.py
  4. 61 19
      timeseries.py

BIN
.apt-sec.py.swp


+ 1 - 0
.gitignore

@@ -13,6 +13,7 @@ __pycache__
 # Swap files#
 # ###############
 *.swp
+*.~lock.*
 # Packages #
 ############
 # it's better to unpack these files and commit the raw source

+ 12 - 7
apt-sec.py

@@ -465,7 +465,7 @@ def processCVEs(pkg, now, src2dsa, dsa2cve, cvetable, config):
     count = sum(cvestats.values())
 
     print(pkg + ' ' + str(count))
-    if pkg == 'linux':
+    if pkg == 'chromium-browser':
         print(src2dsa[pkg])
         pkg_plot(pkg, cvestats)
 
@@ -503,8 +503,6 @@ def pkg_plot(pkg, cvestats):
     for i in range(len(x)):
 #        print(str(x[i].year) + str(x[i].month))
         monthyear[x[i].year-1995][x[i].month-1] += y[i]
-
-    #flatten list
     newx = []
     yearsx = []
     year = []
@@ -514,6 +512,13 @@ def pkg_plot(pkg, cvestats):
     m2 = 0
     k = 0
 
+    label_months = []
+    months_list = [item for sublist in monthyear for item in sublist]
+    for i in range(len(months_list)):
+        label_months.append(i)
+
+    plt.bar(label_months, months_list)
+
     for i in range(len(monthyear)):
         year.append(0)
         cc = 0
@@ -542,15 +547,15 @@ def pkg_plot(pkg, cvestats):
             k += 1
     
 #    plt.xticks(datapoints, monthlabel)
-    #print(year)
+    print(year)
 #    plt.plot.hist(yearsx,year)
 #    plt.bar(yearsx, year, 1, color='blue')   
 #    plt.bar(datapoints, month, 1, color='blue')   
 
-    ts.predictdeep(month)
+#    ts.predict(month)
 
-#    plt.legend([pkg], loc='upper left')
-#    plt.show()
+    plt.legend([pkg], loc='upper left')
+    plt.show()
     return 0
 
 ###############################################################################

+ 61 - 19
timeseries.py

@@ -1,31 +1,73 @@
+from __future__ import print_function
 import numpy as np
 import matplotlib.pyplot as plt
-import pandas
+import pandas as pd
 import math
-from keras.models import Sequential
-from keras.layers import Dense, Activation, Dropout
-from keras.layers.recurrent import LSTM
+from scipy import stats
+from statsmodels.graphics.api import qqplot
+#from keras.models import Sequential
+#from keras.layers import Dense, Activation, Dropout
+#from keras.layers.recurrent import LSTM
+import statsmodels.api as sm
 
-# convert an array of values into a dataset matrix
-def create_dataset(dataset, look_back=1):
-	dataX, dataY = [], []
-	for i in range(len(dataset)-look_back-1):
-		a = dataset[i:(i+look_back), 0]
-		dataX.append(a)
-		dataY.append(dataset[i + look_back, 0])
-	return numpy.array(dataX), numpy.array(dataY)
-
-
-def predictdeep(rawdata):
+def predict(rawdata):
     np.random.seed(1234)
     i = 0
     while rawdata[i] == 0:
         i += 1
 
+    indexes = []
+    year = 1995
+
+    print(rawdata)
+    for i in range(len(rawdata)):
+        indexes[i] = str(year) 
+
     usefuldata = rawdata[i:len(rawdata)-1]
+    print(usefuldata)
+    
+    dta = pd.Series(usefuldata)
+    dta = dta.astype(float)
+    print(dta)
+    dta.plot(figsize=(12,8))
+    fig = plt.figure(figsize=(12,8))
+    ax1 = fig.add_subplot(211)
+    fig = sm.graphics.tsa.plot_acf(dta.values.squeeze(), lags=20, ax=ax1)
+    ax2 = fig.add_subplot(212)
+    fig = sm.graphics.tsa.plot_pacf(dta, lags=20, ax=ax2)
+
+    arma_mod20 = sm.tsa.ARMA(dta, (2,0)).fit()
+    print(arma_mod20.params)
+    
+    arma_mod30 = sm.tsa.ARMA(dta, (3,0)).fit()
+    print(arma_mod20.aic, arma_mod20.bic, arma_mod20.hqic)
+
+    print(arma_mod30.params)
+    print(arma_mod30.aic, arma_mod30.bic, arma_mod30.hqic)
+
+    sm.stats.durbin_watson(arma_mod30.resid.values)
+
+#    fig = plt.figure(figsize=(12,8))
+#    ax = fig.add_subplot(111)
+#    ax = arma_mod20.resid.plot(ax=ax);
+
+
+
+#    fig = plt.figure(figsize=(12,8))
+#    ax = fig.add_subplot(111)
+#    ax = arma_mod30.resid.plot(ax=ax);
+
+    resid = arma_mod30.resid
+
+#    fig = plt.figure(figsize=(12,8))
+#    ax = fig.add_subplot(111)
+#    fig = qqplot(resid, line='q', ax=ax, fit=True)
+    print('this is the data indexes ' + str(dta.ix[0]))
+
+    fig, ax = plt.subplots(figsize=(12, 8))
+    ax = dta.ix['0':].plot(ax=ax)
+    fig = arma_mod30.plot_predict('29', '30', dynamic=True, ax=ax, plot_insample=False)
+    
 
-    dataframe = pandas.DataFrame(usefuldata)
-    dataset = dataframe.values
-    dataset = dataset.astype('float64')
-    plt.plot(usefuldata)
     plt.show()
+