# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, Lasso, ElasticNet
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import make_pipeline
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.feature_selection import SelectFromModel

import matplotlib.pyplot as plt
import numpy as np
# -

df = pd.read_csv('Resources/sales.csv')
df.head()

df=df.drop(columns=['Date', 'year', 'day', 'month','weekly_sales'])
df.head()

df=df[['Store','Product','week_of_year','Base Price','Price','promotion','Is_Holiday','Weekly_Units_Sold']]
df.head()

df['Temp']='_'
df['Store'] = df['Temp'].str.cat(df['Store'].values.astype(str))
df.head()

del df['Temp']
df

df['Temp']='_'
df['Product'] = df['Temp'].str.cat(df['Product'].values.astype(str))
df.head()

del df['Temp']
df

df['Temp']='_'
df['week_of_year'] = df['Temp'].str.cat(df['week_of_year'].values.astype(str))
del df['Temp']
df.head()

#  LabelEncoding Is_Holiday column
df['Is_Holiday']=LabelEncoder().fit_transform(df['Is_Holiday'])
df.head()

# +
# Create features
X=df.drop(columns=['Weekly_Units_Sold'], axis = 1)

# One Hot Encode. 
X=pd.get_dummies(X)

# Create target. 
y = df['Weekly_Units_Sold']
# -

X

X.describe()

# ## Linear Regression

# +
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

# Scale the data
scaler = StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)

# +
# Create and train the model
model = LinearRegression().fit(X_train_scaled, y_train)

# Generate predictions
y_pred = model.predict(X_test_scaled)


# +
#  Score Data
training_score = model.score(X_train_scaled, y_train)
testing_score = model.score(X_test_scaled, y_test)

score = r2_score(y_test, y_pred)

print(f"Training Score: {training_score}")
print(f"Testing Score: {testing_score}")
print('---------------------')
print(f"R2 Score: {score}")

# +
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import GradientBoostingRegressor

GBR = GradientBoostingRegressor()

parameters = {'learning_rate': [0.045, 0.05, 0.055, 0.06, 0.065],
              'subsample'    : [0.09, 0.085, 0.08, 0.075, 0.07],
              'n_estimators' : [900, 950, 1000, 1050, 1100],
              'max_depth'    : [6, 7, 8, 9, 10]
             }

grid_GBR = GridSearchCV(estimator=GBR, param_grid = parameters, cv = 2, n_jobs=-1)
grid_GBR.fit(X_train_scaled, y_train)

# Generate predictions
y_pred = grid_GBR.predict(X_test_scaled)

print(" Results from Grid Search " )
print("\n The best estimator across ALL searched params:\n",grid_GBR.best_estimator_)
print("\n The best score across ALL searched params:\n",grid_GBR.best_score_)
print("\n The best parameters across ALL searched params:\n",grid_GBR.best_params_)
score = r2_score(y_test, y_pred)

score

# +
# # Apply trees in the ensemble to X, return leaf indices.
# apply(X)

# # Fit the gradient boosting model.
# fit(X, y[, sample_weight, monitor])

# # Get parameters for this estimator.
# get_params([deep])

X = []

predict(X)

# # Return the coefficient of determination of the prediction.
# score(X, y[, sample_weight])


# # Set the parameters of this estimator.
# set_params(**params)

# # Predict regression target at each stage for X.
# staged_predict(X)
# -

# Visualizing the regression coefficients. 
plt.rcParams["figure.figsize"] = (20,13)
plt.xticks(rotation=90)
plt.bar(X_train.columns, model.coef_,)
plt.show()

# +
# filter for product 1
Prod1_df = df[df['Product'] == '_1']

# Create features
X = Prod1_df.drop(columns=['Weekly_Units_Sold'], axis = 1)

# One Hot Encode. 
X=pd.get_dummies(X)

# Create target. 
y = Prod1_df['Weekly_Units_Sold']

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

# Scale the data
scaler = StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Create and train the model
model = LinearRegression().fit(X_train_scaled, y_train)

# Generate predictions
y_pred = model.predict(X_test_scaled)

#  Score Data
training_score = model.score(X_train_scaled, y_train)
testing_score = model.score(X_test_scaled, y_test)

score = r2_score(y_test, y_pred)

print(f"Training Score for Product 1: {training_score}")
print(f"Testing Score for Product 1: {testing_score}")
print('---------------------')
print(f"R2 Score for Product 1: {score}")

# +
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import GradientBoostingRegressor

GBR = GradientBoostingRegressor()

parameters = {'learning_rate': [0.05, 0.055, 0.06, 0.065, 0.07],
              'subsample'    : [0.08, 0.075, 0.07, 0.065, 0.06],
              'n_estimators' : [900, 950, 1000, 1050, 1100],
              'max_depth'    : [5, 6, 7, 8, 9]
             }

grid_GBR = GridSearchCV(estimator=GBR, param_grid = parameters, cv = 2, n_jobs=-1)
grid_GBR.fit(X_train_scaled, y_train)

# Generate predictions
y_pred = grid_GBR.predict(X_test_scaled)

print(" Results from Grid Search " )
print("\n The best estimator across ALL searched params:\n",grid_GBR.best_estimator_)
print("\n The best score across ALL searched params:\n",grid_GBR.best_score_)
print("\n The best parameters across ALL searched params:\n",grid_GBR.best_params_)
score = r2_score(y_test, y_pred)

score

# +
# filter for product 2
Prod2_df = df[df['Product'] == '_2']

# Create features
X = Prod2_df.drop(columns=['Weekly_Units_Sold'], axis = 1)

# One Hot Encode. 
X=pd.get_dummies(X)

# Create target. 
y = Prod1_df['Weekly_Units_Sold']

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

# Scale the data
scaler = StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Create and train the model
model = LinearRegression().fit(X_train_scaled, y_train)

# Generate predictions
y_pred = model.predict(X_test_scaled)

#  Score Data
training_score = model.score(X_train_scaled, y_train)
testing_score = model.score(X_test_scaled, y_test)

score = r2_score(y_test, y_pred)

print(f"Training Score for Product 2: {training_score}")
print(f"Testing Score for Product 2: {testing_score}")
print('---------------------')
print(f"R2 Score for Product 2: {score}")

# +
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import GradientBoostingRegressor

GBR = GradientBoostingRegressor()

parameters = {'learning_rate': [0.025, 0.03, 0.035, 0.04, 0.045],
              'subsample'    : [0.6, 0.055, 0.05, 0.045, 0.04],
              'n_estimators' : [1400, 1450, 1500, 1550, 1650],
              'max_depth'    : [8, 9, 10, 11, 12]
             }

grid_GBR = GridSearchCV(estimator=GBR, param_grid = parameters, cv = 2, n_jobs=-1)
grid_GBR.fit(X_train_scaled, y_train)

# Generate predictions
y_pred = grid_GBR.predict(X_test_scaled)

score = r2_score(y_test, y_pred)

print(" Results from Grid Search " )
print("\n The best estimator across ALL searched params:\n",grid_GBR.best_estimator_)
print("\n The best score across ALL searched params:\n",grid_GBR.best_score_)
print("\n The best parameters across ALL searched params:\n",grid_GBR.best_params_)
score

# +
# filter for product 3
Prod3_df = df[df['Product'] == '_3']

# Create features
X = Prod1_df.drop(columns=['Weekly_Units_Sold'], axis = 1)

# One Hot Encode. 
X=pd.get_dummies(X)

# Create target. 
y = Prod3_df['Weekly_Units_Sold']

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

# Scale the data
scaler = StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Create and train the model
model = LinearRegression().fit(X_train_scaled, y_train)

# Generate predictions
y_pred = model.predict(X_test_scaled)

#  Score Data
training_score = model.score(X_train_scaled, y_train)
testing_score = model.score(X_test_scaled, y_test)

score = r2_score(y_test, y_pred)

print(f"Training Score for Product 3: {training_score}")
print(f"Testing Score for Product 3: {testing_score}")
print('---------------------')
print(f"R2 Score for Product 3: {score}")

# +
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import GradientBoostingRegressor

GBR = GradientBoostingRegressor()

parameters = {'learning_rate': [0.04, 0.0425, 0.0475, 0.05],
              'subsample'    : [0.7, 0.6, 0.5, 0.4],
              'n_estimators' : [1600, 1650,1700, 1750],
              'max_depth'    : [1, 2, 3, 4]
             }

grid_GBR = GridSearchCV(estimator=GBR, param_grid = parameters, cv = 2, n_jobs=-1)
grid_GBR.fit(X_train_scaled, y_train)

# Generate predictions
y_pred = grid_GBR.predict(X_test_scaled)

print(" Results from Grid Search " )
print("\n The best estimator across ALL searched params:\n",grid_GBR.best_estimator_)
print("\n The best score across ALL searched params:\n",grid_GBR.best_score_)
print("\n The best parameters across ALL searched params:\n",grid_GBR.best_params_)
score = r2_score(y_test, y_pred)

score
# -

# ## Lasso Regression 

# +
# Create and train the model
model = Lasso(max_iter=10000).fit(X_train_scaled, y_train)

# Generate predictions
y_pred = model.predict(X_test_scaled)


# +
#  Score Data
training_score = model.score(X_train_scaled, y_train)
testing_score = model.score(X_test_scaled, y_test)

score = r2_score(y_test, y_pred)

print(f"Training Score: {training_score}")
print(f"Testing Score: {testing_score}")
print('---------------------')
print(f"R2 Score: {score}")
# -

# Visualizing the regression coefficients. 
plt.rcParams["figure.figsize"] = (20,13)
plt.xticks(rotation=90)
plt.bar(X_train.columns, model.coef_)
plt.show()



# ## Ridge

# +
# Create and train the model
model = Ridge(alpha=100).fit(X_train_scaled, y_train)

# Generate predictions
y_pred = model.predict(X_test_scaled)

# +
#  Score Data
training_score = model.score(X_train_scaled, y_train)
testing_score = model.score(X_test_scaled, y_test)

score = r2_score(y_test, y_pred)

print(f"Training Score: {training_score}")
print(f"Testing Score: {testing_score}")
print('---------------------')
print(f"R2 Score: {score}")
# -

# Visualizing the regression coefficients. 
plt.rcParams["figure.figsize"] = (20,13)
plt.xticks(rotation=90)
plt.bar(X_train.columns, model.coef_)
plt.show()



# ## ElasticNet

# +
# Create and train the model
model = ElasticNet(alpha=10).fit(X_train_scaled, y_train)

# Generate predictions
y_pred = model.predict(X_test_scaled)

# +
#  Score Data
training_score = model.score(X_train_scaled, y_train)
testing_score = model.score(X_test_scaled, y_test)

score = r2_score(y_test, y_pred)

print(f"Training Score: {training_score}")
print(f"Testing Score: {testing_score}")
print('---------------------')
print(f"R2 Score: {score}")
# -

# Visualizing the regression coefficients. 
plt.rcParams["figure.figsize"] = (20,13)
plt.xticks(rotation=90)
plt.bar(X_train.columns, model.coef_)
plt.show()







dnn_model = build_and_compile_model(normalizer)
dnn_model.summary()













# Select the columns that have a nonzero value from the LASSO regression.
reg = Lasso(max_iter=5000).fit(X_train_scaled, y_train)

sel = SelectFromModel(reg)
sel.fit(X_train_scaled, y_train)

X_selected_train, X_selected_test, y_train, y_test = train_test_split(sel.transform(X), y, random_state=1)
scaler = StandardScaler().fit(X_selected_train)
X_selected_train_scaled = scaler.transform(X_selected_train)
X_selected_test_scaled = scaler.transform(X_selected_test)

reg = LinearRegression().fit(X_selected_train_scaled, y_train)
reg.score(X_selected_test_scaled, y_test)

reg = LinearRegression().fit(X_train, y_train)
print(reg.coef_)
plt.bar(X.columns, reg.coef_)
plt.show()















def rmse(X, predictions):
    return np.sqrt(np.mean(np.square(X - predictions)))


# +
# Create and train the model
model = LinearRegression().fit(X_train, y_train)

# Generate predictions
predictions_test = model.predict(X_test)

# Compute loss to evalute the model
loss = rmse(y_test, predictions_test)
print('Test Loss for LinearRegression:', loss)

# Generate predictions for train set
predictions_train = model.predict(X_train)

# Compute loss on train set to evalute the model
loss = rmse(y_train, predictions_train)
print('Training Loss for LinearRegression:', loss)
# -







# +
# # Scale the data
# scaler = StandardScaler().fit(X_train)

# +
# model=LinearRegression()

# +
# model.fit(X_train, y_train)

# +
training_score = model.score(X_train, y_train)
testing_score = model.score(X_test, y_test)

### END SOLUTION 

print(f"Training Score: {training_score}")
print(f"Testing Score: {testing_score}")
# -

score = model.score(X, y)
print(f"R2 Score: {score}")

predictions = model.predict(X)
# Plot Residuals
plt.scatter(predictions, predictions - y)
plt.hlines(y=0, xmin=predictions.min(), xmax=predictions.max())
plt.show()

















from sklearn.svm import SVR
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
import numpy as np

# +
# USE WEIRD MAGICAL FUNCTIONS TO FIT THE CASE DATA TO A SECORD ORDER POLYNOMIAL
poly_model = make_pipeline(PolynomialFeatures(4), LinearRegression())
poly_model.fit(X[:, np.newaxis], y)
Xfit = np.linspace(0, len(X), 1000)
yfit = poly_model.predict(Xfit[:, np.newaxis])

plt.plot(X, y)
plt.plot(Xfit, yfit)
plt.legend(loc="best")
# -



reg = make_pipeline(StandardScaler(), SGDRegressor(max_iter=1000, tol=1e-3))
reg.fit(X,y)

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

# Create and train the model
model = LinearRegression().fit(X_train, y_train)



StarcoderdataJupyterScriptsDedupFiltered