Example #1
0
def load_models():
    return {
        "Sugar Price Index": ARIMAResults.load("model/SugarARIMA.pkl"),
        "Meat Price Index": ARIMAResults.load("model/MeatARIMA.pkl"),
        "Dairy Price Index": ARIMAResults.load("model/DairyARIMA.pkl"),
        "Cereals Price Index": ARIMAResults.load("model/CerealsARIMA.pkl"),
        "Oils Price Index": ARIMAResults.load("model/OilsARIMA.pkl")
    }
Example #2
0
def predict(model, n=6):
    '''
    进行预测
    :param model: 模型
    :param n:一般3个点是一个月
    :return:预测结果
    '''
    if isinstance(model, str):
        # 模型加载
        loaded = ARIMAResults.load('model.pkl')
        # 预测未来3个单位,即为1个月
        predictions = loaded.forecast(n)
        # 预测结果为:
        pre_result = predictions[0]
        print('预测结果为:', pre_result)
        # 标准误差为:
        error = predictions[1]
        print('标准误差为:', error)
        # 置信区间为:
        confidence = predictions[2]
        print('置信区间为:', confidence)
    else:
        # 预测未来3个单位,即为1个月
        predictions = model.forecast(n)
        # 预测结果为:
        pre_result = predictions[0]
        print('预测结果为:', pre_result)
        # 标准误差为:
        error = predictions[1]
        print('标准误差为:', error)
        # 置信区间为:
        confidence = predictions[2]
        print('置信区间为:', confidence)
    return pre_result
Example #3
0
def predict_and_plot_closing_price():
    df = pd.read_csv(INPUT_FILE, parse_dates=['Date'], usecols=["Date", "Close"])
    df = df[::-1]
    df.columns = ["ds", "y"]
    print(df.head())
    data = np.array(df["y"])
    split_per = split_ratio*len(df)
    split_per = int(math.ceil(split_per))
    training, test = data[:split_per], data[split_per:]
    history = [x for x in training]
    predictions = []
    for t in range(len(test)):
        model = ARIMA(history, order=(p, d, q))
        model_fit = model.fit(disp=0)
        model_fit.save('model.pkl')
        output = model_fit.forecast()
        yhat = output[0]
        predictions.append(yhat)
        obs = test[t]
        history.append(obs)
        print('predicted=%f, expected=%f' % (yhat,obs))
    for t in range(7):
        loaded = ARIMAResults.load('model.pkl')
        output = loaded.forecast()
        yhat = output[0]
        predictions.append(yhat)
        history.append(yhat)
        print('predicted=%f' % (yhat))
    plt.plot(test,color="#39FF14",linewidth=7.0)
    plt.plot(predictions, color="black")
def districts():
    start = request.args.get('start')
    end = request.args.get('end')
    rows = DistrictQuantity.query.join(District).with_entities(
        District.name.label('name'),
        DistrictQuantity.date_occurrence.label('date'),
        DistrictQuantity.theft.label('theft')).filter(
            DistrictQuantity.date_occurrence >= start).filter(
                DistrictQuantity.date_occurrence <= end).all()
    values = __transform(rows)
    if datetime.strptime(start, '%Y-%m-%d') >= datetime.strptime(
            '2018-01-01', '%Y-%m-%d'):
        d = pandas.date_range(start='1/1/2018', end='12/1/2018', freq='MS')
        d = d.format(formatter=lambda current: current.strftime('%Y-%m'))
        values["labels"] = d
        for row in District.query.with_entities(
                District.id.label('id'), District.name.label('name')).all():
            loaded = ARIMAResults.load(ROOT + '/{}.pkl'.format(row.id))
            x = [int(round(x)) for x in loaded.forecast(steps=12)[0]]
            total = sum(x)
            values["total"] += total
            values["data"].append({
                "values": x,
                "label": "{} Previsão".format(row.name),
                "total": total
            })
    return jsonify(values)
Example #5
0
def predict(hours):
    series = pp.get_data()
    X = series.values
    history = [x for x in X]
    hours_in_week = 168
    validation = pp.get_validate()
    y = validation.values

    model_fit = ARIMAResults.load('model.pkl')

    predictions = list()
    yhat = model_fit.forecast()[0]
    yhat = inverse_difference(history, yhat, hours_in_week)
    predictions.append(yhat)
    history.append(yhat)

    for i in range(1, hours):
        diff = difference(history, hours_in_week)

        model = ARIMA(diff, order=(1, 0, 0))
        model_fit = model.fit(trend='nc', disp=0)
        yhat = model_fit.forecast()[0]
        yhat = inverse_difference(history, yhat, hours_in_week)
        history.append(yhat)
        predictions.append(yhat)

    return predictions
def build_arima_model(train,
                      target,
                      params=(24, 0, 24),
                      adfuller_test=True,
                      maxiter=100):

    df_target = get_df_arma(train, target)

    if adfuller_test:
        check_stationarity(df_target[target])

    model = sm.tsa.ARIMA(df_target, params)
    start = train['date'].head(1)[0].strftime('%d_%m_%Y_%H_%M')
    end = pd.to_datetime(
        train['date'].tail(1).values[0]).strftime('%d_%m_%Y_%H_%M')

    filename = '../models/arima_model_p_%d_d_%d_q_%d_%s_from_%s_to_%s.pkl' % (
        *params, target, start, end)
    if os.path.exists(filename):
        print("Loading...")
        result = ARIMAResults.load(filename)
    else:
        result = model.fit(solver='bfgs', maxiter=maxiter)
        result.save(filename)

    return result
Example #7
0
def main_task():

    # load and prepare datasets
    dataset = Series.from_csv(file_path)
    X = dataset.values.astype('float32')
    history = [x for x in X]
    validation = Series.from_csv(file_path)
    y = validation.values.astype('float32')
    # load model
    model_fit = ARIMAResults.load('car.pkl')
    # make first prediction
    predictions = list()
    yhat = float(model_fit.forecast()[0])
    predictions.append(yhat)
    history.append(y[0])
    print('>Predicted=%.3f, Expected=%3.f' % (yhat, y[0]))
    # rolling forecasts
    for i in range(1, len(y)):
        # predict
        model = ARIMA(history, order=(2, 1, 0))
        model_fit = model.fit(trend='nc', disp=0)
        yhat = float(model_fit.forecast()[0])
        predictions.append(yhat)
        # observation
        obs = y[i]
        history.append(obs)
        print('>Predicted=%.3f, Expected=%3.f' % (yhat, obs))
    # report performance
    rmse = sqrt(mean_squared_error(y, predictions))
    print('RMSE: %.3f' % rmse)
    pyplot.plot(y)
    pyplot.plot(predictions, color='red')
    pyplot.show()
Example #8
0
def more_prediction():
    dataset = pd.Series.from_csv('dataset.csv')
    X = dataset.values.astype('float32')
    history = [x for x in X]
    validation = pd.Series.from_csv('validation.csv')
    y = validation.values.astype('float32')
    # load model
    model_fit = ARIMAResults.load('model.pkl')
    bias = np.load('model_bias.npy')
    # 做出第一个预测
    predictions = list()
    yhat = bias + float(model_fit.forecast()[0])
    predictions.append(yhat)
    history.append(y[0])
    print('>Predicted=%.3f, Expected=%3.f' % (yhat, y[0]))
    # 滚动预测
    for i in range(1, len(y)):
        # predict
        model = ARIMA(history, order=(4, 0, 0))
        model_fit = model.fit(disp=-1)
        yhat = bias + float(model_fit.forecast()[0])
        predictions.append(yhat)
        # observation
        obs = y[i]
        history.append(obs)
        print('>Predicted=%.3f, Expected=%3.f' % (yhat, obs))
    # 报告性能
    mse = mean_squared_error(y, predictions)
    rmse = math.sqrt(mse)
    print('RMSE: %.3f' % rmse)
    pyplot.plot(y)
    pyplot.plot(predictions, color='red')
    pyplot.show()
def finalize(filename=None):
    dataset = Series.from_csv('Krakow_data.csv')
    X = dataset.values.astype('float32')
    history = [x for x in X]
    months_in_year = 12
    diff = difference.difference(X, months_in_year)
    model = ARIMA(diff, order=(6, 0, 2))
    model_fit = model.fit(trend='nc', disp=0)
    model_fit.save('model.pkl')
    np.save('model_bias.npy', [0.237574])
    validation = Series.from_csv('Krakow_validation.csv')
    y = validation.values.astype('float32')
    model_fit = ARIMAResults.load('model.pkl')
    bias = np.load('model_bias.npy')
    predictions = list()
    yhat = float(model_fit.forecast()[0])
    yhat = bias + difference.inverse_difference(history, yhat, months_in_year)
    predictions.append(yhat)
    history.append(y[0])
    label_for_x = [
        '2017-06', '2017-07', '2017-08', '2017-09', '2017-10', '2017-11',
        '2017-12', '2018-01', '2018-02', '2018-03', '2018-04', '2018-05',
        '2018-06', '2018-07', '2018-08'
    ]
    print('For month: {} Predicted= {} , Expected= {}'.format(
        label_for_x[0], str(yhat), str(y[0])))

    for i in range(1, len(y)):
        months_in_year = 12
        diff = difference.difference(history, months_in_year)
        model = ARIMA(diff, order=(6, 0, 2))
        model_fit = model.fit(trend='nc', disp=0)
        yhat = model_fit.forecast()[0]
        yhat = bias + difference.inverse_difference(history, yhat,
                                                    months_in_year)
        predictions.append(yhat)
        obs = y[i]
        history.append(obs)
        print('For month: {} Predicted= {} , Expected= {}'.format(
            label_for_x[i], str(yhat), str(obs)))

    mse = mean_squared_error(y, predictions)
    rmse = sqrt(mse)
    print('RMSE: {:0.3f}'.format(rmse))

    plt.figure(figsize=(10, 7))
    plt.title('Forecast for temperature in Kraków')
    plt.xlabel('Data')
    plt.ylabel('Temperature')
    plt.xticks(list(range(0, 15)), label_for_x)
    plt.xticks(rotation=45)
    plt.grid()
    plt.plot(y, label="Observational values")
    plt.plot(predictions, color='red', label="Foreseen values")
    plt.legend()
    plt.savefig(filename)

    return rmse
def recursive_test1(data, arima_Order, numOfP):
    """def make_predictions(xList, yList, model, bias, numOfPredictions):
        count = 0
        
        while count < numOfPredictions:
            model = ARIMA(xList, arima_Order)
            model_fit = model.fit(trend = 'nc', disp=0)
            
            prdct = bias + float(model_fit.forecast()[0])
            
            xList.append(prdct[0])
            print("Prediction ",count," : ",prdct[0])
            yList.append(prdct[0])
            
            count = count + 1
        
        return yList
    """

    D = data.values.astype('float32')
    history = [x for x in D]

    modelF = ARIMAResults.load('model.pkl')
    biasF = numpy.load('model_bias.npy')

    #Make first prediction
    predictions = []
    predic = biasF + float(modelF.forecast()[0])

    print("\nRecursive Orig D: ", D)
    print("\nRecursive Orig history: ", history)

    predictions.append(predic[0])
    history.append(predic[0])

    count = 0
    #Rolling forecasts
    while count < numOfP:
        #Make predictions
        model = ARIMA(history, arima_Order)
        model_fit = model.fit(trend='nc', disp=0)
        predic = bias + float(model_fit.forecast()[0])

        predictions.append(predic)
        history.append(predic)

        count = count + 1

    print("\nRecursive D: ", D)
    print("\nRecursive history: ", history)
    print("\nRecursive predictions: ", predictions)

    pyplot.plot(D, color='blue')
    pyplot.plot(predictions, color='red')
    pyplot.show()

    return
Example #11
0
 def predict_model(self,
                   timeseries_series,
                   fitted_model_name,
                   num_steps = 1,
                   differencing_interval=1):
     
     model_fit = ARIMAResults.load(fitted_model_name + '.pkl')
     bias = np.load(fitted_model_name + '_bias.npy')
     yhat, err95, ci95 = model_fit.forecast(steps=num_steps)
     yhat = bias + self.reverse_difference_decomposition(timeseries_series.values, yhat, differencing_interval)
     return yhat
Example #12
0
def forecast_1day(arg_dict):
    '''load finalized model and make a prediction'''

    # Load model
    model_fit = ARIMAResults.load(r'data/model.pkl')
    bias = np.load(r'data/model_bias.npy')

    # Pick a future to predict. 0 means literally tomorrow.
    yhat = bias + float(model_fit.forecast()[0])
    print(
        f"The predicted cumulative {arg_dict['dependent_variable']} for {arg_dict['place']} for tomorrow (input data plus 1 day) are {place_value(yhat)}"
    )
Example #13
0
def train_and_evaluate_arima_model(train_data, test_data, ARIMA_ORDER,
                                   is_saved_model):
    predictions = []
    actuals = []
    arima_model_fit = None
    recur_train_data = [x for x in train_data]

    if is_saved_model == False:

        ## Train the model using walk-forward validation technique / rolling forecast
        for i in range(len(test_data)):
            model = ARIMA(recur_train_data, order=ARIMA_ORDER)
            arima_model_fit = model.fit(disp=0)  # Fit the ARIMA model

            output = arima_model_fit.forecast()
            predicted_value = output[0]
            actual_value = test_data[i]

            recur_train_data.append(actual_value)
            predictions.append(predicted_value)
            actuals.append(actual_value)

            # print('Predicted Value: %f, Actual Value: %f' % (predicted_value, actual_value))

    elif is_saved_model == True:

        ## Load the saved ARIMA model
        arima_loaded_model = ARIMAResults.load(model_filename)
        arima_model_fit = arima_loaded_model
        # print('Training the ARIMA{} model..'.format(arima_order))

        for i in range(len(test_data)):
            model = ARIMA(recur_train_data, order=ARIMA_ORDER)
            arima_model_fit = model.fit(disp=0)  # Fit the ARIMA model

            output = arima_model_fit.forecast()
            predicted_value = output[0]
            actual_value = test_data[i]

            recur_train_data.append(actual_value)
            predictions.append(predicted_value)
            actuals.append(actual_value)

    ## Evaluate the ARIMA model using several metrics (Mean Absolute Error, Mean Absolute Percentage Error, Root Mean Squared Error)
    mae = metrics.mean_absolute_error(actuals, predictions)
    mape = np.mean(
        np.abs((np.array(actuals) - np.array(predictions)) /
               np.array(actuals))) * 100
    mse = metrics.mean_squared_error(actuals, predictions)
    rmse = np.sqrt(mse)

    return mae, mape, rmse, predictions, actuals, arima_model_fit
Example #14
0
    def arimainference(self, pickleFileLocation, storeLocation,
                       daysintothefuture):
        print(pickleFileLocation)
        model = ARIMAResults.load(pickleFileLocation)
        predictions = model.forecast(daysintothefuture)
        predictions = pd.DataFrame({"predictions": predictions})
        print(predictions)
        # ran=random.randint(100,999)
        csvresults = predictions.to_csv()
        inferenceDataResultsPath = os.path.join(storeLocation, "inference.csv")
        inference = open(inferenceDataResultsPath, "w+")
        inference.write(csvresults)
        inference.close()

        return inferenceDataResultsPath
Example #15
0
    def graph_print(called_status, graph_to_print, training_data, test_data, len_training):
        results_ar = ARIMAResults.load("../trained_model/"+called_item+".pkl")
        global original_data_plot, predicted_data
        if called_status == 0:

            #making prediction
            pred = results_ar.forecast(steps=60)[0]
            y = pred.tolist()
            s = pd.Series(y, copy=False)
            s = np.exp(s)
            training_data = training_data.reset_index()
            for x in range(len(s)):
                if(training_data.Month[len(training_data)-1].month < 12):
                    m = training_data.Month[len(training_data)-1].month + 1
                    y = training_data.Month[len(training_data)-1].year
                else:
                    y = training_data.Month[len(training_data)-1].year + 1
                    m = 1
                d = '{}-{}'.format(y, m)
                d = datetime.strptime(d, '%Y-%m')
                training_data = training_data.append(
                    {'Month': d, 'Quantity': s[x]}, ignore_index=True)
            training_data.set_index("Month", inplace=True)
            original_data_plot = training_data[:len_training]
            predicted_data = training_data[len_training:]
        fig = plt.figure(figsize=(4.5, 4), dpi=90)
        plt.title(title_print_first_word+" of "+called_item)
        if(graph_to_print == "Line Graph"):
            plt.plot(original_data_plot, color='black', label="Training Data")
            plt.plot(test_data, color='blue', label="Actual Data")
            plt.plot(predicted_data, color='red', label="Predicted Data")
        elif(graph_to_print == "Scatter Graph"):
            plt.scatter(original_data_plot.index.values, original_data_plot['Quantity'], s=10, color='black', label="Training Data")
            plt.scatter(test_data.index.values, test_data['Quantity'], s=10, color='blue', label="Actual Data")
            plt.scatter(predicted_data.index.values, predicted_data['Quantity'], s=10, color='red', label="Predicted Data")
    #            plt.show()
        plt.xlabel("Date")
        plt.ylabel("Quantity in Kg")
        plt.legend(loc='best')
        # You can make your x axis labels vertical using the rotation

        # specify the window as master
        canvas = FigureCanvasTkAgg(fig, master=window)
        canvas.draw()
        canvas.get_tk_widget().grid(row=1, column=0, columnspan=4,
                                    padx=7, pady=5, ipadx=20)
        called_status = 1
        return called_status
def predictRun(p, d, q):

    X = validate.getdataset().values.astype('float32')
    history = [x for x in X]
    months_in_year = 12
    y = validate.getvalidation().values.astype('float32')
    # load model
    model_fit = ARIMAResults.load(variables.model)
    bias = numpy.load(variables.bias)
    # make first prediction
    predictions = list()
    yhat = float(model_fit.forecast()[0])
    yhat = bias + inverse_difference(history, yhat, months_in_year)
    predictions.append(yhat)
    history.append(y[0])
    print('>Predicted=%.3f, Expected=%3.f, accuracy=%3.f' %
          (yhat, y[0], yhat / y[0] * 100))
    # rolling forecasts
    for i in range(1, len(y)):
        # difference data
        months_in_year = 12
        diff = difference(history, months_in_year)
        # predict
        model = ARIMA(diff, order=(p, d, q))
        model_fit = model.fit(trend='nc', disp=0)
        yhat = model_fit.forecast()[0]
        yhat = bias + inverse_difference(history, yhat, months_in_year)
        predictions.append(yhat)
        # observation
        obs = y[i]
        history.append(obs)
        print('>Predicted=%.3f, Expected=%3.f, accuracy=%3.f' %
              (yhat, obs, yhat / obs * 100))
        # report performance
    mse = mean_squared_error(y, predictions)
    rmse = sqrt(mse)
    print('RMSE: %.3f' % rmse)
    pyplot.plot(y)
    pyplot.plot(predictions, color='red')

    red_line = mlines.Line2D([], [], color='red', label='Voorspelling')
    blue_line = mlines.Line2D([], [], color='blue', label='Werkelijkheid')
    pyplot.legend(handles=[red_line, blue_line])

    pyplot.title('Voorspelling verkoop wijn 1972')
    pyplot.ylabel('Hoeveelheid')
    pyplot.xlabel('Maand')
    pyplot.show()
    def __init__(self, model_folder_abs_path: str):
        self._model_folder_abs_path: str = model_folder_abs_path

        if not os.path.exists(self._model_folder_abs_path):
            os.makedirs(self._model_folder_abs_path)

        model_file_abs_path = self._get_model_abs_file_path()
        model_meta_data_file_abs_path = self._get_model_meta_data_abs_file_path(
        )
        if os.path.exists(model_file_abs_path) and os.path.exists(
                model_meta_data_file_abs_path):
            self._arima_model: ARIMAResults = ARIMAResults.load(
                model_file_abs_path)
            with open(model_meta_data_file_abs_path) as file_meta_data:
                self._meta_data = json.load(file_meta_data)
        else:
            self._arima_model: ARIMAResults = None
            self._meta_data = None
def predictFuture():
    series = validate.getdatafile()
    months_in_year = 12
    model_fit = ARIMAResults.load(variables.model)
    bias = numpy.load(variables.bias)
    yhat = float(model_fit.forecast()[0])
    yhat = bias + inverse_difference(series.values, yhat, months_in_year)
    print('Predicted: %.3f' % yhat)
    pyplot.plot(series)
    pyplot.title('Lijn diagram tot aan voorspelling')
    pyplot.show()

    prediction = pd.DataFrame(yhat)
    prediction.to_csv(variables.predictionSave,
                      mode='a',
                      sep=',',
                      header=False)
    prediction.to_csv(variables.datafileloc, mode='a', header=False)
def validate_model(X, Y, arima_Order):
    X = X.values.astype('float32')
    Y = Y.values.astype('float32')
    history = [x for x in X]

    #load model
    model_fit = ARIMAResults.load('model.pkl')
    bias = numpy.load('model_bias.npy')

    #Make first prediction
    predictions = list()
    yhat = bias + float(model_fit.forecast()[0])
    predictions.append(yhat)
    history.append(Y[0])
    print('\nPrediction: ', yhat, ' Expected: ', Y[0])

    #Rolling forecasts
    for i in range(1, len(Y)):
        #Make predictions
        model = ARIMA(history, arima_Order)
        model_fit = model.fit(trend='nc', disp=0)
        yhat = bias + float(model_fit.forecast()[0])
        predictions.append(yhat)

        #Observations
        obs = Y[i]
        history.append(obs)
        print('Prediction: ', yhat, ' Expected: ', obs)

    #Reporting Performance
    mse = mean_squared_error(Y, predictions)
    rmse = sqrt(mse)
    """
    print("\n Validation X: ",X)
    print("\n Validation Y: ",Y)
    print("\n Validation History: ",history)
    print("\n Validation Predictions: ",predictions)
    """

    pyplot.plot(Y)
    pyplot.plot(predictions, color='red')
    pyplot.show()
    print('\nRMSE: ', rmse)
    return
def predict():
    try:
        months = int(ery_monts.get())
        if months <= 0:
            tkMessageBox.showinfo("Error",
                                  "Months should be greater than zero")
        else:
            dataset = Series.from_csv('dataset_training.csv')
            X = dataset.values.astype('float32')
            history = [x for x in X]
            months_in_year = 12

            model_fit = ARIMAResults.load('sales_model.pkl')
            bias = numpy.load('model_bias.npy')
            predictions = list()
            yhat = float(model_fit.forecast()[0])
            yhat = bias + inverse_difference(history, yhat, months_in_year)
            predictions.append(yhat)
            history.append(X[0])

            for i in range(0, months):
                iteration = months
                diff = difference(history, iteration)

                # predict here
                model = ARIMA(diff, order=(0, 0, 1))
                model_fit = model.fit(trend='nc', disp=0)
                yhat = model_fit.forecast()[0]
                yhat = bias + inverse_difference(history, yhat, iteration)

                #add predicted data in predictions list to plot graph
                predictions.append(yhat)

                #add predicted data to history
                #which helps to improve accuracy while predict future
                history.append(yhat)
                print("Prediction> " + str(yhat))

            pyplot.figure(num='Predicted Results')
            pyplot.plot(predictions, color='red')
            pyplot.show()
    except Exception as e:
        tkMessageBox.showinfo("Error", "invalid month")
Example #21
0
def main():

    data = pd.read_csv("./query_large.csv", sep=",")
    np_date = ["" for i in range(len(data))]

    data = data[296:]
    data = data.iloc[::-1]
    for i, item in enumerate(data["time"]):
        np_date[i] = datetime.strptime(item[:-5], '%Y-%m-%dT%H:%M:%S')
    X = np.array(data["mag"])
    for i in range(len(X)):
        if X[i] >= 5:
            X = X[i:]
            break
    count = 1
    ans = []
    new_data = []
    for i in range(len(X)):
        if X[i] < 5:
            #print('in if ' + str( X[i]))
            count = count + 1
        else:
            # print('in else ' + str( X[i]))
            # print('appended '+ str(count))
            ans.append(count)
            count = 0
            #new_data.append(data["time"][i])
    data_f = pd.DataFrame(data=ans, columns=['timeSinceLast'])

    dataset = data_f
    model = sm.tsa.statespace.SARIMAX(dataset, order=(5, 0, 1))
    model_fit = model.fit(disp=0)

    model_fit.save('arima_nowcasting.pkl')

    model = ARIMAResults.load('arima_nowcasting.pkl')

    start_index = int(sys.argv[1])
    end_index = int(sys.argv[2])
    forecast = model.predict(start=start_index, end=end_index)
    print(forecast)
    plt.scatter(forecast.index.tolist(), forecast.values)
def recursive_test(data, arima_Order, numOfPredictions):
    def make_predictions(xList, yList, model, bias, numOfPredictions):
        count = 0

        while count < numOfPredictions:
            model = ARIMA(xList, arima_Order)
            model_fit = model.fit(trend='nc', disp=0)

            prdct = bias + float(model_fit.forecast()[0])

            xList.append(prdct[0])
            print("Prediction ", count, " : ", prdct[0])
            yList.append(prdct[0])

            count = count + 1

        return yList

    X = data.values.astype('float32')
    orig = X.tolist()
    xList = X.tolist()
    yList = []
    model_fit = ARIMAResults.load('model.pkl')
    bias = numpy.load('model_bias.npy')

    predictions = make_predictions(xList, yList, model_fit, bias,
                                   numOfPredictions)
    """
    print("\n xList:")
    print(xList)
    print("\n yList:")
    print(yList)
    print("\n orig:")
    print(orig)
    """

    pyplot.plot(orig, color='blue')
    pyplot.plot(xList, color='red')
    #pyplot.plot(predictions, color = 'green')
    pyplot.show()

    return
Example #23
0
def test_ARIMA():
    fited_model = ARIMAResults.load('ARIMA_best')
    ar_coef, ma_coef = fited_model.arparams, fited_model.maparams
    resid = fited_model.resid
    ar_num, ma_num = ar_coef.size, ma_coef.size
    with open('test_data.json', 'r') as f:
        test_data = json.load(f)
    rmse_sum, mae_sum, mape_sum = 0, 0, 0
    skip_mape_count = 0
    for data in tqdm(test_data):
        history = []
        label = []
        pred = []
        for index, d in enumerate(data['data']):
            if index > ar_num:
                label.append(d[1])
                cur_resid = np.random.choice(resid, ma_num)
                diff = difference(history)
                yhat = history[-1] + predict(ar_coef, diff) + predict(
                    ma_coef, cur_resid)
                pred.append(yhat)
            history.append(d[1])
        label = np.array(label)
        pred = np.array(pred)
        rmse = np.sqrt(np.mean(np.square(label - pred)))
        mae = np.mean(np.abs(label - pred))
        mape_mask = label > 1
        mape = np.sum(np.abs(label - pred) / (label + 0.001) * mape_mask)
        mape_count = np.sum(mape_mask)
        if mape_count != 0:
            mape = mape / np.sum(mape_mask)
            mape_sum += mape
        else:
            skip_mape_count += 1
        rmse_sum += rmse
        mae_sum += mae
    print('RMSE is {:.3f}'.format(rmse_sum / len(test_data)))
    print('MAE is {:.3f}'.format(mae_sum / len(test_data)))
    print('MAPE is {:.5f}'.format(mape_sum /
                                  (len(test_data) - skip_mape_count)))
Example #24
0
 def validate_model(self,
                    validation_timeseries_series,
                    timeseries_series,
                    fitted_model_name,
                    differencing_interval=1):
     
     timeseries_values = timeseries_series.values.astype('float32')
     history = [x for x in timeseries_values]
     validation_timeseries_values = validation_timeseries_series.values.astype('float32')
     model_fit = ARIMAResults.load(fitted_model_name + '.pkl')
     bias = np.load(fitted_model_name + '_bias.npy')
     predictions = list()
     yhat = float(model_fit.forecast()[0])
     yhat = bias + self.reverse_difference_decomposition(history, yhat, differencing_interval)
     predictions.append(yhat)
     history.append(validation_timeseries_values[0])
     
     print ('>Predicted=%.6f, Expected=%6.f' % (yhat, validation_timeseries_values[0]))
     
     for i in range(1, len(validation_timeseries_values)):
         diff = self.difference_composition(history, differencing_interval)
         # predict
         model = ARIMA(diff, order=self.get_arima_order() )
         model_fit = model.fit(trend='nc', disp=0)
         yhat = model_fit.forecast()[0]
         yhat = bias + self.reverse_difference_decomposition(history, yhat, differencing_interval)
         predictions.append(yhat)
         # observation
         obs = validation_timeseries_values[i]
         history.append(obs)
         print ('>Predicted=%.6f, Expected=%6.f' % (yhat, obs))
         
     self.validation_dataset_predictions = predictions
     self.validation_dataset_history = history
     
     mse = mean_squared_error(validation_timeseries_values, predictions)
     rmse = sqrt(mse)
     return rmse
def cities():
    start = request.args.get('start')
    end = request.args.get('end')
    rows = CityQuantity.query.join(City).with_entities(
        City.name, CityQuantity.date_occurrence, CityQuantity.theft).filter(
            CityQuantity.date_occurrence >= start).filter(
                CityQuantity.date_occurrence <= end).all()
    values = __transform(rows)
    if datetime.strptime(start, '%Y-%m-%d') >= datetime.strptime(
            '2018-01-01', '%Y-%m-%d'):
        loaded = ARIMAResults.load(ROOT + 'ssp/v1/cities.pkl')
        d = pandas.date_range(start='1/1/2018', end='12/1/2018', freq='MS')
        d = d.format(formatter=lambda current: current.strftime('%Y-%m'))
        x = [int(round(x)) for x in loaded.forecast(steps=12)[0]]
        total = sum(x)
        values["labels"] = d
        values["total"] = values["total"] + total
        values["data"].append({
            "values": x,
            "label": "Goiânia Previsão",
            "total": total
        })
    return jsonify(values)
Example #26
0
def main():

    data = pd.read_csv("./query_large.csv",
                       sep=",",
                       parse_dates=['time'],
                       index_col='time',
                       squeeze=True)  #, date_parser=parser)

    fit = data['mag']
    model = sm.tsa.statespace.SARIMAX(fit, order=(1, 0, 1))
    model_fit = model.fit(disp=0)

    residuals = DataFrame(model_fit.resid)

    model_fit.save('arima_normal.pkl')
    timestamp = lambda s: datetime.strptime(s, "%d/%m/%Y")

    model = ARIMAResults.load('arima_normal.pkl')

    start_index = int(sys.argv[1])

    end_index = start_index + 6  #datetime(1925, 12, 26)
    forecast = model.predict(start=start_index, end=end_index)
    print(forecast)
	for i in range(interval, len(dataset)):
		value = dataset[i] - dataset[i - interval]
		diff.append(value)
	return diff
# invert differenced value
def inverse_difference(history, yhat, interval=1):
	return yhat + history[-interval]
# load and prepare datasets
dataset = Series.from_csv('trainsetar1.csv')
X = dataset.values.astype('float32')
history = [x for x in X]
months_in_year = 12
validation = Series.from_csv('testsetar1.csv')
y = validation.values.astype('float32')
# load model
model_fit = ARIMAResults.load('model1.pkl')
bias = numpy.load('model_bias1.npy')
# make first prediction
predictions = list()
yhat = float(model_fit.forecast()[0])
yhat = bias + inverse_difference(history, yhat, months_in_year)
predictions.append(yhat)
history.append(y[0])
print('>Predicted=%.d, Expected=%.d' % (yhat, y[0]))
# rolling forecasts
for i in range(1, len(y)):
	# difference data
	months_in_year = 12
	diff = difference(history, months_in_year)
	# predict
	model = ARIMA(diff, order=(0,0,1))
Example #28
0
def test():
    # load model
    model_fit = ARIMAResults.load('model.pkl')
    forecast = model_fit.forecast(steps=7)[0]
    print(forecast)
Example #29
0
def prediction():
    model_fit = ARIMAResults.load('model.pkl')
    bias = np.load('model_bias.npy')
    yhat = bias + float(model_fit.forecast()[0])
    print('Predicted: %.3f' % yhat)
Example #30
0
 def load_models(self):
     for i in self.describe:
         self.describe[i]['model'] = ARIMAResults.load(
             f'{self.model_path}/model_{i}.pkl')