<jupyter_start><jupyter_text>Prediction Models<jupyter_code>import pandas as pd
import datetime
import numpy as np
import sklearn
from sklearn.cross_validation import train_test_split 
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import RandomizedSearchCV
from sklearn.grid_search import GridSearchCV
from sklearn import linear_model
from sklearn.metrics import *
from sklearn.neural_network import MLPRegressor
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
def mean_absolute_percentage_error(y_true, y_pred): 
        y_true, y_pred = np.array(y_true), np.array(y_pred)
        return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
weekend = ['Saturday','Sunday']
def week_day_type(x):
    if x in weekend:
        return 'weekends'
    else:
        return 'weekdays'
def time_slot(x):
    if x in morning:
        return 'morning'
    elif x in afternoon:
        return 'afternoon'
    elif x in evening:
        return 'evening'
    else:
        return 'night'  
df=pd.read_csv("C:/Users/Akash/Documents/ADS/Assignment2/Appliances-energy-prediction-data-master/energydata_complete.csv")
df['date']=pd.to_datetime(df['date'])
df['year']=df['date'].dt.year
df['month']=df['date'].dt.month
df['day']=df['date'].dt.day
df['day_of_week']=df['date'].dt.weekday_name
df['time_hr_24']=df['date'].dt.hour
df['time_min']=df['date'].dt.minute
df['week_day_type']=df['day_of_week'].map(week_day_type)
morning=range(6,12)
afternoon=range(12,17)
evening=range(17,22)  
df['time_slot']=df['time_hr_24'].map(time_slot)
df.drop(['date'],axis=1,inplace=True)
df=pd.get_dummies(df,prefix=['DOW','TS','WDT'],columns=['day_of_week','time_slot','week_day_type'])
print(df.shape)<jupyter_output>(19735, 46)<jupyter_text>Spliting data and normalization<jupyter_code>df_train,df_test = train_test_split(df,train_size=0.7,random_state=42)
x_train=df_train.iloc[:,1:]
y_train=df_train['Appliances']
scaler.fit(x_train)
x_train_sc=scaler.transform(x_train)
x_test=df_test.iloc[:,1:]
y_test=df_test['Appliances']
x_test_sc=scaler.transform(x_test)<jupyter_output><empty_output><jupyter_text>Linear Regression Model<jupyter_code>lm=linear_model.LinearRegression()
lm.fit(x_train_sc,y_train)<jupyter_output><empty_output><jupyter_text>Linear Regression on Training dataset<jupyter_code>y_train_pred=lm.predict(x_train_sc)
print("R2   :",r2_score(y_train,y_train_pred))
print("MAE  :",mean_absolute_error(y_train,y_train_pred))
print("RMSE :",np.sqrt(mean_squared_error(y_train,y_train_pred)))
print("MAPE :",mean_absolute_percentage_error(y_train,y_train_pred))<jupyter_output>R2   : 0.20492495517
MAE  : 52.04917031
RMSE : 91.8032672546
MAPE : 59.5590692172<jupyter_text>Linear Regression on Testing dataset<jupyter_code>y_test_pred=lm.predict(x_test_sc)
print("R2   :",r2_score(y_test,y_test_pred))
print("MAE  :",mean_absolute_error(y_test,y_test_pred))
print("RMSE :",np.sqrt(mean_squared_error(y_test,y_test_pred)))
print("MAPE :",mean_absolute_percentage_error(y_test,y_test_pred))<jupyter_output>R2   : 0.202353758845
MAE  : 52.4473890861
RMSE : 90.6523438651
MAPE : 61.7795970369<jupyter_text>Random Forest Model<jupyter_code>rf=RandomForestRegressor()
rf.fit(x_train_sc, y_train)<jupyter_output><empty_output><jupyter_text>Random Forest on Training dataset<jupyter_code>y_train_pred=rf.predict(x_train_sc)
print("R2   :",r2_score(y_train,y_train_pred))
print("MAE  :",mean_absolute_error(y_train,y_train_pred))
print("RMSE :",np.sqrt(mean_squared_error(y_train,y_train_pred)))
print("MAPE :",mean_absolute_percentage_error(y_train,y_train_pred))<jupyter_output>R2   : 0.901313591729
MAE  : 14.3792529318
RMSE : 32.3431822664
MAPE : 14.2605523459<jupyter_text>Random Forest on Testing dataset<jupyter_code>y_test_pred=rf.predict(x_test_sc)
print("R2   :",r2_score(y_test,y_test_pred))
print("MAE  :",mean_absolute_error(y_test,y_test_pred))
print("RMSE :",np.sqrt(mean_squared_error(y_test,y_test_pred)))
print("MAPE :",mean_absolute_percentage_error(y_test,y_test_pred))<jupyter_output>R2   : 0.47441832924
MAE  : 35.5865563249
RMSE : 73.5857942129
MAPE : 35.4129570787<jupyter_text>Building Models Based on Selected Features<jupyter_code>drop_col_list=['year','DOW_Monday','DOW_Saturday','DOW_Sunday','DOW_Thursday','DOW_Tuesday','DOW_Wednesday','TS_afternoon','TS_morning',
              'WDT_weekdays','WDT_weekends','month','time_min','DOW_Friday','TS_evening','day','rv1','rv2','Visibility'
               ,'T9','T7','lights']
x_train=df_train.iloc[:,1:]
y_train=df_train['Appliances']
x_train.drop(drop_col_list,axis=1,inplace=True)
scaler.fit(x_train)
x_train_sc=scaler.transform(x_train)
x_test=df_test.iloc[:,1:]
x_test.drop(drop_col_list,axis=1,inplace=True)
y_test=df_test['Appliances']
x_test_sc=scaler.transform(x_test)<jupyter_output><empty_output><jupyter_text>Random Forest Model After using tuned  hyperparameters<jupyter_code>rf=RandomForestRegressor(bootstrap=False, criterion='mse', max_depth=25,
           max_features='sqrt', max_leaf_nodes=None,
           min_impurity_decrease=0.0, min_impurity_split=None,
           min_samples_leaf=1, min_samples_split=2,
           min_weight_fraction_leaf=0.0, n_estimators=350, n_jobs=1,
           oob_score=False, random_state=42, verbose=0, warm_start=False)
rf.fit(x_train_sc, y_train)<jupyter_output><empty_output><jupyter_text>Random Forest on Training dataset<jupyter_code>y_train_pred=rf.predict(x_train_sc)
print("R2   :",r2_score(y_train,y_train_pred))
print("MAE  :",mean_absolute_error(y_train,y_train_pred))
print("RMSE :",np.sqrt(mean_squared_error(y_train,y_train_pred)))
print("MAPE :",mean_absolute_percentage_error(y_train,y_train_pred))<jupyter_output>R2   : 0.999360449368
MAE  : 1.36231948386
RMSE : 2.60370359672
MAPE : 1.80088335883<jupyter_text>Random Forest on Testing dataset<jupyter_code>y_test_pred=rf.predict(x_test_sc)
print("R2   :",r2_score(y_test,y_test_pred))
print("MAE  :",mean_absolute_error(y_test,y_test_pred))
print("RMSE :",np.sqrt(mean_squared_error(y_test,y_test_pred)))
print("MAPE :",mean_absolute_percentage_error(y_test,y_test_pred))<jupyter_output>R2   : 0.631187334288
MAE  : 28.1314719464
RMSE : 61.6420097136
MAPE : 27.1339805346<jupyter_text>Linear Regression Model<jupyter_code>lm=linear_model.LinearRegression()
lm.fit(x_train_sc,y_train)<jupyter_output><empty_output><jupyter_text>Linear Regression on training dataset<jupyter_code>y_train_pred=lm.predict(x_train_sc)
print("R2   :",r2_score(y_train,y_train_pred))
print("MAE  :",mean_absolute_error(y_train,y_train_pred))
print("RMSE :",np.sqrt(mean_squared_error(y_train,y_train_pred)))
print("MAPE :",mean_absolute_percentage_error(y_train,y_train_pred))<jupyter_output>R2   : 0.171048338471
MAE  : 53.2887700492
RMSE : 93.7386468196
MAPE : 62.3960338305<jupyter_text>Linear Regression on Testing dataset<jupyter_code>y_test_pred=lm.predict(x_test_sc)
print("R2   :",r2_score(y_test,y_test_pred))
print("MAE  :",mean_absolute_error(y_test,y_test_pred))
print("RMSE :",np.sqrt(mean_squared_error(y_test,y_test_pred)))
print("MAPE :",mean_absolute_percentage_error(y_test,y_test_pred))<jupyter_output>R2   : 0.171646282313
MAE  : 53.3113370386
RMSE : 92.3808148987
MAPE : 64.0116106896<jupyter_text>Neural Network<jupyter_code>x_train=df_train.iloc[:,1:]
print(x_train.shape)
y_train=df_train['Appliances']
scaler.fit(x_train)
x_train_sc=scaler.transform(x_train)
x_test=df_test.iloc[:,1:]
y_test=df_test['Appliances']
x_test_sc=scaler.transform(x_test)<jupyter_output>(13814, 45)<jupyter_text>Neural Network Model<jupyter_code>mlp = MLPRegressor(hidden_layer_sizes=(365,365,365),max_iter=500,alpha=1.00000000e-06,random_state=42)
mlp.fit(x_train_sc,y_train)<jupyter_output><empty_output><jupyter_text>Neural Network on Training Dataset<jupyter_code>y_train_pred=mlp.predict(x_train_sc)
print("R2   :",r2_score(y_train,y_train_pred))
print("MAE  :",mean_absolute_error(y_train,y_train_pred))
print("RMSE :",np.sqrt(mean_squared_error(y_train,y_train_pred)))
print("MAPE :",mean_absolute_percentage_error(y_train,y_train_pred))<jupyter_output>R2   : 0.75920061787
MAE  : 27.9396749876
RMSE : 50.5221426581
MAPE : 30.2356429216<jupyter_text>Neural Network on Testing Dataset<jupyter_code>y_test_pred=mlp.predict(x_test_sc)
print("R2   :",r2_score(y_test,y_test_pred))
print("MAE  :",mean_absolute_error(y_test,y_test_pred))
print("RMSE :",np.sqrt(mean_squared_error(y_test,y_test_pred)))
print("MAPE :",mean_absolute_percentage_error(y_test,y_test_pred))<jupyter_output>R2   : 0.407856186557
MAE  : 39.4717490931
RMSE : 78.1065533965
MAPE : 39.3081527053
{'fraction_non_alphanumeric': 0.13690962099125364, 'fraction_numerical': 0.07055393586005831, 'mean_word_length': 9.773869346733669, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '443076', 'license': 'CNRI-Python', 'n_tokens_mistral': 3941, 'n_tokens_neox': 3496, 'n_words': 374, 'path': 'Part 4 - Prediction algorithms/Part4_withOutliersInData.ipynb', 'repo_name': 'NortheasternUniversityADS/Assignment-2', 'size': '8575'}