<jupyter_start><jupyter_text>Linear Regression<jupyter_code># 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<jupyter_output><empty_output><jupyter_text>Lasso Regression<jupyter_code># 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()<jupyter_output><empty_output><jupyter_text>Ridge<jupyter_code># 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()<jupyter_output><empty_output><jupyter_text>ElasticNet<jupyter_code># 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)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1311861743912019, 'fraction_numerical': 0.026158680282796543, 'mean_word_length': 5.318114143920596, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '457969', 'license': 'MIT', 'n_tokens_mistral': 4846, 'n_tokens_neox': 4578, 'n_words': 1052, 'path': 'static/Work Notebooks/sc_jfc_model.ipynb', 'repo_name': 'jackatopolis/black-friday-predictor', 'size': '12730'}