Python Forum
Different results of code with local account and technical account
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Different results of code with local account and technical account
#5
(Mar-02-2020, 07:03 PM)micseydel Wrote: You really need to provide more details. If your question is about Python code, we need to see the code that causes the problem. (If that code more than about ten lines, I highly recommend you come up with example code which is minimized to reproduce your problem, hard-code as much as possible.) As for the most recent results you posted, I'm not especially familiar with MS SQL but you'll need to provide your queries if you want people to comment on the results.

hi Micseydel,

here is the code:

from datetime import datetime, timedelta
import pandas as pd
import numpy as np
import ast
from warnings import catch_warnings
from warnings import filterwarnings
from statsmodels.tsa.holtwinters import ExponentialSmoothing
import time
import sqlite3
import pyodbc as msql
from dateutil.relativedelta import relativedelta

def lit_eval(strg):
    if type(strg) == str:
        return ast.literal_eval(strg)
    else:
        return strg

def pd_dict2list(row):
    return [value for key, value in row.items()][0:-1]

def frcst_cot(y, model, setup, fcst_hrz):
    result = None
    try:
        # never show warnings when grid searching, too noisy
        with catch_warnings():
            filterwarnings("ignore")

            t, d, s, p, b, r = setup
            # fit model
            model = ExponentialSmoothing(y, seasonal_periods=p, trend=t, seasonal=s, damped=d).fit(use_boxcox=b,
                                                                                                   remove_bias=r)
            yhat_v = model.predict(len(y) - fcst_hrz)
            yhat = model.forecast(fcst_hrz)
            result = pd.concat([yhat_v, yhat], axis=0)
    except:
        error = None
        result = None

    # if no forecasting was done applying generic model    
    if result is None:
        print('result is none, trying generic model...')
        try:
            # never show warnings when grid searching, too noisy
            with catch_warnings():
                filterwarnings("ignore")

                t, d, s, p, b, r = setup
                # fit model
                model = ExponentialSmoothing(y).fit()
                yhat_v = model.predict(len(y) - fcst_hrz)
                yhat = model.forecast(fcst_hrz)
                result = pd.concat([yhat_v, yhat], axis=0)
        except:
            error = None
            result = None

    # if results are still None using Naive model prediction 
    if result is None:
        print('result is still none, applying Naive model...')
        try:
            # never show warnings when grid searching, too noisy
            with catch_warnings():
                filterwarnings("ignore")

                forecst = y.copy()
                idx = pd.date_range(start=y.tail(1).index.min(), periods=fcst_hrz, freq='MS')
                forecst = forecst.reindex(idx, fill_value=0)
                forecst['Volume'] = y.tail(1).values[0][0]
                forecst = forecst.squeeze()
        except:
            error = None
    return result

# TODO: Add path to pickle file BEST_MODELS FILE
best_models = pd.read_pickle(r'C:\Users\XXX\Desktop\..\best_models.pkl')

starttime = time.time()

for itm in range(len(best_models)):
    row = best_models[itm:itm + 1]

    fcst_horizon = 12

    cols = (row.T != '(All)')
    cols.columns = ['chek']
    cols = cols[cols.chek == True].T

    needed_cols = list(set(cols.columns) - {'DATE', 'MODEL', 'SETUP', 'VERTEX'})

    qry_str = '\tand '.join([(c + ' == "' + row[c].values[0] + '" ') for c in needed_cols])

    # TODO: ADD PICKLE PATH COT_VERTEX
    CoT_vertex = pd.read_pickle(r'C:\Users\XXX\Desktop\..CoT_vertex.pkl')
    CoT_vertex = CoT_vertex.copy()

    CoT_vertex = CoT_vertex.pivot_table(index='Date', values=['Volume'], aggfunc=np.sum).reset_index()

    if CoT_vertex.Date.min() != CoT_vertex.Date.min():
        print('no COT data for:' + qry_str)
    else:
        if datetime.strptime(CoT_vertex.Date.max(), '%Y-%m-%d').date() >= (
                datetime.now().replace(day=1) + relativedelta(months=-2)).date():  # Added
            idx = pd.date_range(start=CoT_vertex.Date.min(), end=CoT_vertex.Date.max(), freq='MS')
            CoT_vertex.set_index('Date', inplace=True)
            CoT_vertex = CoT_vertex.reindex(idx, fill_value=0)
            CoT_vertex = CoT_vertex.squeeze()

            frcst = frcst_cot(CoT_vertex, row['MODEL'].values[0], row['SETUP'].values[0], fcst_horizon)

            print(frcst)

        else:
            print('Last COT older than 3m')
I tried to hardcode as much as I can but I am very new at Python so still struggling.
I am now looking to see how to send you the 2 pickle files so that you can run the code...
Reply


Messages In This Thread
RE: Different results of code with local account and technical account - by dreyz64 - Mar-03-2020, 03:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Modify an Energy Model to account for year dependent interest rate rather than only t giovanniandrean 0 1,036 Oct-10-2023, 07:00 AM
Last Post: giovanniandrean
  Code works but doesn't give the right results colin_dent 2 1,395 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Microsoft text phone verifying account cito 2 1,719 Jul-21-2022, 12:16 PM
Last Post: cito
  Output prints Account.id at the end? LastStopDEVS 5 3,868 Dec-19-2020, 05:59 AM
Last Post: buran
  Compiling Python 3.8.5 source code results in build error Deepan 0 2,719 Sep-14-2020, 04:11 AM
Last Post: Deepan
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,965 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  How to append one function1 results to function2 results SriRajesh 5 4,348 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  Error while Logging on to outlook email account using Python inside VDI Shilton 0 5,104 Sep-09-2018, 06:53 AM
Last Post: Shilton
  I need to MODIFY this to list all of the s3 buckets in my account , right now it will quantum_1 0 2,059 Jun-04-2018, 03:48 PM
Last Post: quantum_1
  financial technical indicators ian 2 3,830 Apr-23-2018, 12:25 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020