NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import copy
import pandas as pd
import numpy as np
pd.set_option("display.max_rows",None)
pd.set_option("display.max_columns",None)
#from scripts.explore import hier_cluster
#from scripts.plot import plot_line
#from scripts.utils import filter_new_sample_points
import warnings
warnings.filterwarnings("ignore")
import seaborn as sns
import datetime
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
from sklearn.ensemble import VotingRegressor

from sklearn.metrics import mean_absolute_error,mean_squared_error,mean_absolute_percentage_error
from sklearn.linear_model import ElasticNetCV
from sklearn.linear_model import ElasticNet
from sklearn import linear_model
import math
from sklearn.linear_model import Ridge
import statsmodels.api as sm
import matplotlib.pyplot as plt
from sklearn.model_selection import TimeSeriesSplit
import copy
import pandas as pd
import numpy as np
pd.set_option("display.max_rows",None)
pd.set_option("display.max_columns",None)
#from scripts.explore import hier_cluster
#from scripts.plot import plot_line
#from scripts.utils import filter_new_sample_points
import warnings
warnings.filterwarnings("ignore")
import seaborn as sns

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt
from sklearn.preprocessing import scale
from sklearn.preprocessing import StandardScaler
from sklearn import model_selection
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn import neighbors
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error
import math
from sklearn.metrics import mean_absolute_error
import seaborn as sns
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
import shap
import numpy as np
from scipy.signal import butter,filtfilt
from sklearn.preprocessing import *



X_val_forward = new_data_forward[:-15]
X_hol_forward = new_data_forward[-15:]


### RANDOM FOREST

rf_model = RandomForestRegressor()

rf_params = {"max_depth": [5,8,10],
"max_features": [2,5,7,10],
"n_estimators": [200,300,500],
"min_samples_split": [2,8,10,20],
"min_samples_leaf":[3,4,5,6]}


cv = KFold(n_splits=5,shuffle=False)
gsearch = GridSearchCV(estimator=rf_model, cv=cv,
param_grid=rf_params)
gsearch.fit(X_val_forward, y_validation)

y_pred_rf_forward = gsearch.predict(X_hol_forward)

a = mean_absolute_error(y_pred_rf_forward,y_holdout)

mse = mean_squared_error(y_pred_rf_forward, y_holdout)

rmse = math.sqrt(mse)


mape = np.mean(np.abs((y_holdout - y_pred_rf_forward)/y_holdout))*100

r2 = r2_score(y_holdout, y_pred_rf_forward)

adjusted_r_squared = 1 - (1-r2)*(len(y_validation)-1)/(len(y_validation)-X_val_forward.shape[1]-1)

diff_r2 = abs(r2-adjusted_r_squared)



print("Algorithm:","RF")
print("Score:",gsearch.best_score_)
print("Params:",gsearch.best_params_)
print("Holdout score MAE",a)
print("Holdout score RMSE",rmse)
print("MAPE:",mape)


print("R2_Score:", r2)
print("Adjusted R2:", adjusted_r_squared)
print("Diff btw Adjusted R2 and R2_score: ",diff_r2)

print("------------------------------------")

pred_situation_0301 = pd.DataFrame()
pred_situation_0301['HOLDOUT'] = y_holdout
pred_situation_0301['RF_pred_forward'] = y_pred_rf_forward


import seaborn as sns
line_width = 1.5
fig, axs = plt.subplots(figsize=(12,8))

text_size = 10
plt.rc('font', size=text_size) # controls default text sizes
plt.rc('axes', titlesize=text_size) # fontsize of the axes title
plt.rc('axes', labelsize=text_size) # fontsize of the x and y labels
plt.rc('xtick', labelsize=text_size) # fontsize of the tick labels
plt.rc('ytick', labelsize=text_size) # fontsize of the tick labels
plt.rc('legend', fontsize=text_size) # legend fontsize
plt.rc('figure', titlesize=text_size) # fontsize of the figure title
plt.xlabel('xlabel', fontsize=text_size)
plt.ylabel('ylabel', fontsize=text_size)

sns.lineplot(data=pred_situation_0301['HOLDOUT'],
linewidth = line_width, ax=axs, label="Holdout-AralıkBası"
, color="green"
)


sns.lineplot(data=pred_situation_0301['RF_pred_forward'],
linewidth = line_width, ax=axs, label="RF_pred_forward"
, color="orange"
)

'''
sns.lineplot(data=df1['U150 Feed N2 (150SCI0004_AZOT_D5762)_1'],
linewidth = line_width, ax=axs, label="Target Variable Lag1 - 150SCI0004_AZOT_D5762)_1"
, color="blue"
)

'''

axs.set_title(label = "Prediction vs SampleValue",
fontweight="bold")

axs.set(xlabel="Date", ylabel="Distribution of Pred and Sample")
# axs.legend(loc = "upper left")
# axs.legend(loc='center left', bbox_to_anchor=(1.1, 0.5))
axs.legend(loc='upper center', bbox_to_anchor=(0.5, -0.08),
fancybox=True, shadow=True, ncol=6)


plt.savefig('forward_pred_with_rf_0301.png')

plt.show()

### XGBOOST

model_xgb = XGBRegressor()

xgb_params = {"learning_rate": [0.3,0.1,0.01],
"min_child_weight": [0.8,1],
"max_depth": [6,8,10],
"n_estimators": [100,200,500],
"colsample_bytree": [0.4,0.8,1],
"lambda": [0.5,0.8,1]}


cv = KFold(n_splits=5,shuffle=False)

gsearch = GridSearchCV(estimator=model_xgb, cv=cv,
param_grid=xgb_params, verbose = 2)

gsearch.fit(X_val_forward, y_validation)

y_pred_xgb_forward = gsearch.predict(X_hol_forward)

a = mean_absolute_error(y_pred_xgb_forward,y_holdout)

mse = mean_squared_error(y_pred_xgb_forward, y_holdout)

rmse = math.sqrt(mse)


mape = np.mean(np.abs((y_holdout - y_pred_xgb_forward)/y_holdout))*100

r2 = r2_score(y_holdout, y_pred_xgb_forward)

adjusted_r_squared = 1 - (1-r2)*(len(y_validation)-1)/(len(y_validation)-X_val_forward.shape[1]-1)

diff_r2 = abs(r2-adjusted_r_squared)


print("Algorithm:","XGBOOST")
print("Score:",gsearch.best_score_)
print("Params:",gsearch.best_params_)
print("Holdout score MAE",a)
print("Holdout score RMSE",rmse)
print("MAPE:",mape)


print("R2_Score:", r2)
print("Adjusted R2:", adjusted_r_squared)
print("Diff btw Adjusted R2 and R2_score: ",diff_r2)

print("------------------------------------")


pred_situation_0301['XGB_pred_forward'] = y_pred_xgb_forward


import seaborn as sns
line_width = 1.5
fig, axs = plt.subplots(figsize=(12,8))

text_size = 10
plt.rc('font', size=text_size) # controls default text sizes
plt.rc('axes', titlesize=text_size) # fontsize of the axes title
plt.rc('axes', labelsize=text_size) # fontsize of the x and y labels
plt.rc('xtick', labelsize=text_size) # fontsize of the tick labels
plt.rc('ytick', labelsize=text_size) # fontsize of the tick labels
plt.rc('legend', fontsize=text_size) # legend fontsize
plt.rc('figure', titlesize=text_size) # fontsize of the figure title
plt.xlabel('xlabel', fontsize=text_size)
plt.ylabel('ylabel', fontsize=text_size)

sns.lineplot(data=pred_situation_0301['HOLDOUT'],
linewidth = line_width, ax=axs, label="Holdout-AralıkBası"
, color="green"
)


sns.lineplot(data=pred_situation_0301['XGB_pred_forward'],
linewidth = line_width, ax=axs, label="XGB_pred_forward"
, color="orange"
)

'''
sns.lineplot(data=df1['U150 Feed N2 (150SCI0004_AZOT_D5762)_1'],
linewidth = line_width, ax=axs, label="Target Variable Lag1 - 150SCI0004_AZOT_D5762)_1"
, color="blue"
)

'''

axs.set_title(label = "Prediction vs SampleValue",
fontweight="bold")

axs.set(xlabel="Date", ylabel="Distribution of Pred and Sample")
# axs.legend(loc = "upper left")
# axs.legend(loc='center left', bbox_to_anchor=(1.1, 0.5))
axs.legend(loc='upper center', bbox_to_anchor=(0.5, -0.08),
fancybox=True, shadow=True, ncol=6)


plt.savefig('forward_pred_with_XGB_0301.png')

plt.show()

### LINEAR REGRESSION

linreg_model = LinearRegression()

linreg_params = {'fit_intercept':[True,False],
'normalize':[True,False]}



cv = KFold(n_splits=5,shuffle=False)

gsearch = GridSearchCV(estimator=linreg_model, cv=cv,
param_grid=linreg_params, verbose = 2)

gsearch.fit(X_val_forward, y_validation)

y_pred_linreg_forward = gsearch.predict(X_hol_forward)

a = mean_absolute_error(y_pred_linreg_forward,y_holdout)

mse = mean_squared_error(y_pred_linreg_forward, y_holdout)

rmse = math.sqrt(mse)


mape = np.mean(np.abs((y_holdout - y_pred_linreg_forward)/y_holdout))*100

r2 = r2_score(y_holdout, y_pred_linreg_forward)

adjusted_r_squared = 1 - (1-r2)*(len(y_validation)-1)/(len(y_validation)-X_val_forward.shape[1]-1)

diff_r2 = abs(r2-adjusted_r_squared)


print("Algorithm:","LINEAR-REGRESSOR")
print("Score:",gsearch.best_score_)
print("Params:",gsearch.best_params_)
print("Holdout score MAE",a)
print("Holdout score RMSE",rmse)
print("MAPE:",mape)


print("R2_Score:", r2)
print("Adjusted R2:", adjusted_r_squared)
print("Diff btw Adjusted R2 and R2_score: ",diff_r2)

print("------------------------------------")

pred_situation_0301['LINEAR-REGRESSOR_pred_forward'] = y_pred_linreg_forward

import seaborn as sns
line_width = 1.5
fig, axs = plt.subplots(figsize=(12,8))

text_size = 10
plt.rc('font', size=text_size) # controls default text sizes
plt.rc('axes', titlesize=text_size) # fontsize of the axes title
plt.rc('axes', labelsize=text_size) # fontsize of the x and y labels
plt.rc('xtick', labelsize=text_size) # fontsize of the tick labels
plt.rc('ytick', labelsize=text_size) # fontsize of the tick labels
plt.rc('legend', fontsize=text_size) # legend fontsize
plt.rc('figure', titlesize=text_size) # fontsize of the figure title
plt.xlabel('xlabel', fontsize=text_size)
plt.ylabel('ylabel', fontsize=text_size)

sns.lineplot(data=pred_situation_0301['HOLDOUT'],
linewidth = line_width, ax=axs, label="Holdout-AralıkBası"
, color="green"
)


sns.lineplot(data=pred_situation_0301['LINEAR-REGRESSOR_pred_forward'],
linewidth = line_width, ax=axs, label="LINEAR-REGRESSOR_pred_forward"
, color="orange"
)

'''
sns.lineplot(data=df1['U150 Feed N2 (150SCI0004_AZOT_D5762)_1'],
linewidth = line_width, ax=axs, label="Target Variable Lag1 - 150SCI0004_AZOT_D5762)_1"
, color="blue"
)

'''

axs.set_title(label = "Prediction vs SampleValue",
fontweight="bold")

axs.set(xlabel="Date", ylabel="Distribution of Pred and Sample")
# axs.legend(loc = "upper left")
# axs.legend(loc='center left', bbox_to_anchor=(1.1, 0.5))
axs.legend(loc='upper center', bbox_to_anchor=(0.5, -0.08),
fancybox=True, shadow=True, ncol=6)


plt.savefig('forward_pred_with_linearregressor_0301.png')

plt.show()




     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.