<jupyter_start><jupyter_text>Do the predictions<jupyter_code>## Evaluate model for predictions (quantitative)
## prediction function 
# if time, make it autoregressive 
## lit review for HVAC consumption 
evaluate_metrics={}

for bldg in buildings:
    if bldg == "avenal-recreation-center":
        # no data for this one to have previously trained on
        continue
        
    zones = xbos_services_getter.get_zones(building_zone_names_stub,bldg)
    action_df=pd.DataFrame()
    
    for zone in zones:
        s = xbos_services_getter.get_indoor_actions_historic(indoor_data_historical_stub,bldg,zone,start,end,window,aggregate)
        action_df = pd.concat([action_df, s], axis =1)

    if bldg == "csu-dominguez-hills":
        print("no meter or greenbutton data for csu-dominguez-hills")
        continue
        
    if bldg == "jesse-turner-center":
        continue

    electric_meter_data = xbos_services_getter.get_meter_data_historical(meter_data_historical_stub,bldg,start,end,electric_point_type,aggregate,window)
    
    data = pd.concat([action_df, electric_meter_data], axis =1)
    data["X"] = data.index
    data.rename(columns={"power":"y"}, inplace =True)
    
    scalerY = MinMaxScaler(feature_range=(0, 1))
    scalerX = MinMaxScaler(feature_range=(0, 1))
    scalerS = MinMaxScaler(feature_range=(0, 1))

    data["is_weekday"] = [is_weekday(x.weekday()) for x in data["X"]]
    data["is_workday"] = [is_workday(x.hour) for x in data["X"]]
    data["is_spring"]= [is_Spring(x.month) for x in data["X"]]
    data["is_summer"]= [is_Summer(x.month) for x in data["X"]]
    data["is_autumn"]= [is_Autumn(x.month) for x in data["X"]]
    data["is_winter"]= [is_Winter(x.month) for x in data["X"]]

    data.dropna(inplace=True)
    
    if data.empty:
        print(bldg+" is empty")
        continue
    
    data["X"] = scalerX.fit_transform(data["X"].values.reshape(-1,1))
    data["y"] = scalerY.fit_transform(data["y"].values.reshape(-1,1))

    prediction_window=16
    training_data=data[:-prediction_window]
    test_data = data[-prediction_window:]

    train_labels = training_data["y"]
    train_features = training_data.drop("y", axis = 1)
    test_labels = test_data["y"]
    test_features = test_data.drop("y", axis = 1)
    
    params = best_params_dict[bldg]
        
    rf = RandomForestRegressor(max_depth = params["max_depth"],
                              min_impurity_decrease= params["min_impurity_decrease"],
                              min_samples_leaf = params["min_samples_leaf"],
                              min_samples_split = params["min_samples_split"],
                              n_estimators = params["n_estimators"])
    rf.fit(train_features, train_labels)
    
    # printing evaluation criteria
   
    evaluate_metrics[bldg] = evaluate(rf, train_features,train_labels)
evaluate_metrics
def evaluate(model, train_features, train_labels):
    tscv = TimeSeriesSplit(n_splits=5)
    e = []
    a = []
    for train_index, test_index in tscv.split(train_features):
        rf.fit(train_features.iloc[train_index],train_labels.iloc[train_index])
        predictions = model.predict(train_features.iloc[test_index])
        errors = abs(predictions - train_labels.iloc[test_index])
        mape = 100 * np.mean(errors / train_labels.iloc[test_index])
        accuracy = 100 - mape
        e.append(np.mean(errors))
        a.append(accuracy)
    return(np.mean(e), np.mean(a))

def predict(model, test_features):
    return(model.predict(test_features))<jupyter_output><empty_output><jupyter_text>Utils<jupyter_code>def is_Spring(month):
    if month >3 and month<7:
        return 1
    else: 
        return 0

def is_Summer(month):
    if month> 6 and month < 10:
        return 1
    else: 
        return 0 

def is_Autumn(month): 
    if month > 9:
        return 1 
    else:
        return 0 

def is_Winter(month):
    if month ==12 or month <3:
        return 1 
    else:
        return 0

def timeseries_to_supervised(data, lag=1, steps = 10, dropnan= True, prefix= ""):
    n_vars = 1 if type(data) is list else data.shape[1]
    df = pd.DataFrame(data)
    cols, names = list(), list()
    for i in range(lag, 0, -1):
        cols.append(df.shift(i))
        names += [(prefix + 'var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
    for i in range(0, steps):
        cols.append(df.shift(-i))
        if i == 0:
            names += [(prefix + 'var%d(t)' % (j+1)) for j in range(n_vars)]
        else:
            names += [(prefix + 'var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
    agg = pd.concat(cols, axis=1)
    agg.columns = names
    if dropnan: 
        agg.dropna(inplace=True)
    return agg

def is_weekday(day):
    if day in range(0,5):
        return 1
    else:
        return 0


def is_workday(hour):
    if hour in range(8,18):
        return 1
    else:
        return 0

def is_holiday(day):
    holidays = pd.DatetimeIndex(['2016-11-11'])
    for d in ('2016-11-11', '2016-11-24', '2016-11-25', '2016-12-23', '2016-12-26', '2016-12-30', '2017-01-16', '2017-02-20', 
              '2017-02-20', '2017-03-27', '2017-03-28', '2017-03-29', '2017-03-30', '2017-03-31', '2017-07-04', '2017-09-04',
              '2017-11-10', '2017-11-23', '2017-11-24', '2017-12-22', '2017-12-25', '2017-12-29', '2018-01-01', '2018-01-15',
              '2018-02-19', '2018-03-26', '2018-03-27', '2018-03-28', '2018-03-30', '2018-03-30', '2018-04-28', '2018-07-04',
              '2018-09-03'
             ):
         holidays = holidays.append(pd.DatetimeIndex([d]))
    
    dateOnly = pd.DataFrame(pd.DatetimeIndex((data.index).date))

    data['Holiday'] = pd.DatetimeIndex(dateOnly[0]).isin(holidays)
    data['Holiday'] = data['Holiday'].replace({ True : 1, False : 0 })
    

def interpolate_uncontrolled(data):

    data.ix[data["s0"]>.2, "y"] = np.nan
    data.ix[data["s1"]>.2, "y"] = np.nan
    data.ix[data["s2"]>.2, "y"] = np.nan
    data.ix[data["s3"]>.2, "y"] = np.nan
    data.ix[data["s4"]>.2, "y"] = np.nan
    data.ix[data["s5"]>.2, "y"] = np.nan
    data.ix[data["s6"]>.2, "y"] = np.nan
    data.ix[data["s7"]>.2, "y"] = np.nan
    data.ix[data["s8"]>.2, "y"] = np.nan
    data.ix[data["s9"]>.2, "y"] = np.nan
    data.ix[data["s10"]>.2, "y"] = np.nan
    data.ix[data["s11"]>.2, "y"] = np.nan
    data.ix[data["s12"]>.2, "y"] = np.nan
    data.ix[data["s13"]>.2, "y"] = np.nan

    data.ix[data["y"].isna(), "interpolated"]=1
    data.ix[data["interpolated"].isna(), "interpolated"]=0

    data["y"].interpolate(method = "piecewise_polynomial", inplace=True)

    data.drop(["s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "s11", "s12", "s13"], axis = 1, inplace = True)
    data.dropna(inplace=True)

    return data

def add_features(data):
    data["is_weekday"] = [is_weekday(x.weekday()) for x in data["X"]]
    data["is_workday"] = [is_workday(x.hour) for x in data["X"]]
    data["is_spring"]= [is_Spring(x.month) for x in data["X"]]
    data["is_summer"]= [is_Summer(x.month) for x in data["X"]]
    data["is_autumn"]= [is_Autumn(x.month) for x in data["X"]]
    data["is_winter"]= [is_Winter(x.month) for x in data["X"]]
    return data<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1711786755518534, 'fraction_numerical': 0.054144106622240736, 'mean_word_length': 3.017847183491355, 'pattern_counts': {'":': 4, '<': 12, '<?xml version=': 0, '>': 26, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '637096', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2694, 'n_words': 516, 'path': 'services/energy_consumption_forecast/random_forest/rf_parameters_microservices-2.ipynb', 'repo_name': 'phgupta/XBOS', 'size': '7203'}