<jupyter_start><jupyter_text>Feedback time series models<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
sns.set_context('talk')
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
#untie local dependency
import os
cwd = os.getcwd().split('\\')
local_repo_path = '\\'.join(cwd[:-1])
raw_data_path = local_repo_path + r'\data\raw'
processed_data_path = local_repo_path + '\data\processed'
feature_path = local_repo_path + r'\\data\\features'
output_feature_fname = 'Lookback2'
n_lookback = 2
sj_feature_train = pd.read_csv(feature_path + r'\\sj_train_Lookback_w_cases' + str(n_lookback) + '.csv', index_col=0).drop('var6(t)', axis = 1)
sj_feature_test = pd.read_csv(feature_path + r'\\sj_test_' + output_feature_fname + '.csv', index_col=0)
iq_feature_train = pd.read_csv(feature_path + r'\\iq_train_Lookback_w_cases' + str(n_lookback) + '.csv', index_col=0).drop('var6(t)', axis = 1)
iq_feature_test = pd.read_csv(feature_path + r'\\iq_test_' + output_feature_fname + '.csv', index_col=0)
sj_feature_train.head()
from sklearn.preprocessing import MinMaxScaler
from sklearn.ensemble import (ExtraTreesRegressor, RandomForestRegressor, 
                              AdaBoostRegressor, GradientBoostingRegressor)
from sklearn import neighbors
from sklearn.svm import SVR
from sklearn import linear_model
from sklearn.metrics import mean_absolute_error
from sklearn.neural_network import MLPRegressor
SVR_mod = SVR(kernel = 'linear')
lin_reg = linear_model.LinearRegression()
MLP_NN = MLPRegressor((100), max_iter = 100,learning_rate = 'adaptive', activation = 'relu')
def feedback_model(train, test, model, n_lookback = 10):
    for i in np.arange(len(test)):
        X = train.drop('total_cases', axis = 1)
        y = train['total_cases']
        scaler = MinMaxScaler(feature_range = (-1,1))
        scalerfit = scaler.fit(X)
        X = scaler.transform(X)
        model.fit(X,y)
        
        previous_timestep = pd.DataFrame(train.iloc[len(train)-1, :]).transpose()
        next_timestep = pd.DataFrame(test.drop(['city', 'year', 'weekofyear'], axis = 1).iloc[i,:]).transpose()
        for j in np.arange(1,n_lookback):
            #print(previous_timestep['var6(t-' + str(i) + ')'])
            next_timestep['var6(t-' + str(j+1) + ')'] = previous_timestep['var6(t-' + str(j) + ')'].values

            next_timestep['var6(t-1)'] = previous_timestep['total_cases'].values
            next_timestep['total_cases'] = -1
            
        X_next_timestep = scalerfit.transform(next_timestep.drop('total_cases', axis = 1))
       
        next_timestep['total_cases'] = model.predict(X_next_timestep)
        
            
        train = train.append(next_timestep).reset_index(drop = True)

        
        
        
    return(train)
feedbacks = feedback_model(sj_feature_train, sj_feature_test, MLP_NN, n_lookback = 2)
fig, ax = plt.subplots(figsize = (20,10))
feedbacks['total_cases'].plot(ax = ax)
RF_mod.predict()
scaler_iq = scaler.fit(iq_feature_train.drop('total_cases', axis = 1))
X_tr_iq = scaler_iq.transform(iq_feature_train.drop('total_cases', axis = 1))
Y_tr_iq = iq_feature_train['total_cases']
    mod.fit(train.drop('total_cases', axis = 1), train['total_cases'])
    test_preds = mod.predict(test.drop(['city', 'year', 'weekofyear'], axis = 1))
    train_preds = mod.predict(train.drop('total_cases', axis = 1))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13956204379562043, 'fraction_numerical': 0.012554744525547445, 'mean_word_length': 4.278890600924499, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '370078', 'license': 'MIT', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1223, 'n_words': 212, 'path': 'archive/Feedback_Models.ipynb', 'repo_name': 'chrisgschon/DengAI', 'size': '3425'}