<jupyter_start><jupyter_text>Build the Linear Regression Model<jupyter_code># Read in the data
X = data['Marketing Spend( Million $)'].values.reshape(-1,1)
y = data['Sales (Million $)']
reg = LinearRegression() 
reg.fit(X,y)
# Predictions on the basis of the model
y_pred = reg.predict(X)
y_pred
r2_score(y, y_pred) # Marketing spends account for 96.5% of the variation present in the Sales
#Residual Sum of Squares = Mean_Squared_Error * Total number of datapoints
rss = np.sum(np.square(y - y_pred))
print(rss)
mse = mean_squared_error(y, y_pred)
print(mse)
# Root Mean Squared Error
rmse = mse**0.5
print(rmse)
# Plot for Predicted sales vs Marketing Spend
plt.scatter( X , y , color = 'blue') # original data shown as blue points
plt.plot(X , y_pred , color = 'red' , linewidth = 3) # Fitted model in red
plt.xlabel("Marketing Spend (Million $)")
plt.ylabel("Predicted Sales (Million $)")
plt.show()<jupyter_output><empty_output><jupyter_text>Let us say that we want a better fit and hence we use higher degree polynomials as predictors.<jupyter_code>#help(PolynomialFeatures)
X = data['Marketing Spend( Million $)'].values.reshape(-1,1)
print(X)
poly = PolynomialFeatures(3) # Want to generate features with degree less than or equal to
                             # the specified degree
Y = poly.fit_transform(X) # Transform the variable X to 1, X, X^2, X^3
print(Y)
# Building the polynomial regression model with degree 5
degree=5 # got this number through trial and this is the lowest value which fit the data perfectly
polyreg5 = PolynomialFeatures(degree)
X_poly5 = polyreg5.fit_transform(X) # Transform the variable X to 1, X, X^2, X^3, X^4, X^5
linreg5 = LinearRegression()
linreg5.fit(X_poly5, y)
# Plotting the polynomial regression(degree-5) and simple linear regression
X_seq = np.linspace(X.min(),X.max(),300).reshape(-1,1) # Between 0 and 1 we get 300 equally spaced values
# print(X_seq)
plt.figure()
plt.scatter(X,y) 
plt.plot(X_seq,linreg5.predict(polyreg5.fit_transform(X_seq)),color="black") # model fit with polynomial regression
plt.plot(X_seq,reg.predict(X_seq),color="red") # model fit with linear regression
plt.title("Polynomial regression with degree "+str(degree))
plt.xlabel("Marketing Spend (Million $)")
plt.ylabel("Predicted Sales (Million $)")
plt.show()
y_pred5 = linreg5.predict(polyreg5.fit_transform(X)) # store predictions from the polynomial regression in the variable y_pred5
print(r2_score(y, y_pred5))
# Metrics to assess model performance
rss = np.sum(np.square(y - y_pred5))
print(rss)
mse = mean_squared_error(y, y_pred5)
print(mse)
rmse = mse**0.5
# Root Mean Squared Error
print(rmse)<jupyter_output>1.6288128800067282e-28
2.714688133344547e-29
5.2102669157582964e-15<jupyter_text>Ridge Regression<jupyter_code># Applying Ridge Regression with varying the hyperparameter 'lambda'

X_seq = np.linspace(X.min(),X.max(),300).reshape(-1,1) # values to be considered for predictor variable
lambdas = [0, 0.001, 0.01, 0.1, 1, 10, 100, 1000] # Higher the value of lambda, 
                                                  # more the regularization
for i in lambdas: # for each lambda we get different model coefficients
    degree = 5 # Degree for polynomial regression - chose 5 since this is the lowest number that gave a perfect fit
    # Creating degree 5 features
    ridgecoef = PolynomialFeatures(degree)
    # Transforming input features to polynomial features (1, x1, x2, x3, x4, x5)    
    X_poly = ridgecoef.fit_transform(X)
    ridgereg = Ridge(alpha = i) # Initialize the Ridge Regression model with a specific lambda
    ridgereg.fit(X_poly, y) # fit the model on the polynomial features
    
    # Plotting the ridge regression model for each lambda
    plt.figure()
    plt.scatter(X,y)
    plt.plot(X_seq,ridgereg.predict(ridgecoef.fit_transform(X_seq)),color="black") # Polynomial Regression
    plt.plot(X_seq,reg.predict(X_seq),color="red") # Linear Regression
    plt.title("Polynomial regression with degree "+str(degree) + " and lambda = " + str(i))
    plt.show()
    
    #Computing the r2 score
    y_pred = ridgereg.predict(ridgecoef.fit_transform(X))
    print("r2 score = " + str(r2_score(y, y_pred))) 
    print(ridgereg.coef_) # model coefficients<jupyter_output><empty_output><jupyter_text>Lasso Regression<jupyter_code># Applying Lasso Regression with varying the hyperparameter 'lambda'

lambdas = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
for i in lambdas:
    degree = 5
    # Creating degree 5 features
    lassocoef = PolynomialFeatures(degree)
    # Transforming input features to polynomial features (1, x1, x2, x3, x4, x5)
    X_poly = lassocoef.fit_transform(X)
    lassoreg = Lasso(alpha = i)
    lassoreg.fit(X_poly, y)
    # Plotting the ridge model
    plt.figure()
    plt.scatter(X,y)
    plt.plot(X_seq,lassoreg.predict(lassocoef.fit_transform(X_seq)),color="black")
    plt.plot(X_seq,reg.predict(X_seq),color="red")
    plt.title("Polynomial regression with degree "+str(degree) + " and lambda = " + str(i))
    plt.show()
    # Compute R^2 
    y_pred = lassoreg.predict(lassocoef.fit_transform(X))
    print("r2 score = " + str(r2_score(y, y_pred)))
    print(lassoreg.coef_)<jupyter_output><empty_output><jupyter_text>Ridge and Lasso Regression for specific lambda value<jupyter_code># Get the model coefficients for specific lambda say 0.001

# Ridge Regression

ridgecoef = PolynomialFeatures(degree, include_bias = True) # Creating degree 5 features
# Transforming input features to polynomial features (1, x1, x2, x3, x4, x5)    
X_poly = ridgecoef.fit_transform(X)
ridgereg = Ridge(alpha = 0.001) # Initialize the Ridge Regression model with a specific lambda
ridgereg.fit(X_poly, y) # fit the model on the polynomial features
print(ridgereg.coef_)    
y_pred = ridgereg.predict(ridgecoef.fit_transform(X))
print("r2 score = " + str(r2_score(y, y_pred)))

# Lasso Regression
lassocoef = PolynomialFeatures(degree) # Creating degree 5 features
# Transforming input features to polynomial features (1, x1, x2, x3, x4, x5)
X_poly = lassocoef.fit_transform(X)
lassoreg = Lasso(alpha = 0.001)
lassoreg.fit(X_poly, y)
print(lassoreg.coef_)
y_pred = lassoreg.predict(lassocoef.fit_transform(X))
print("r2 score = " + str(r2_score(y, y_pred)))
betas = pd.DataFrame(index=['1', 'x', 'x2', 'x3', 'x4', 'x5'], 
                     columns = ['Polynomial', 'Ridge', 'Lasso'])
betas['Polynomial'] = linreg5.coef_ # Polynomial Regression
betas['Ridge'] = ridgereg.coef_ # Ridge Regression
betas['Lasso'] = lassoreg.coef_ # Lasso Regression
betas<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1151912235258266, 'fraction_numerical': 0.03199756209050739, 'mean_word_length': 4.7177700348432055, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '437681', 'license': 'Apache-2.0', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2244, 'n_words': 626, 'path': '8. Machine Learning-2/1. Advanced Regression/3. Regularization/Ridge and Lasso Regression.ipynb', 'repo_name': 'vshubh24/machineLearning', 'size': '6563'}