<jupyter_start><jupyter_text>Title**Exercise: 1 - Bias Variance Tradeoff** DescriptionThe aim of this exercise is to understand **bias variance tradeoff**. For this, you will fit different degree polynomial regression on the same data and plot them as given below.   Instructions:- Read the file noisypopulation.csv as a pandas dataframe.- Assign the response and predictor variables appropriately.- Perform bootstrap operation on the dataset. - For each bootstrap:    - For degree of the chosen degree value:        - Compute the polynomial features        - Fit the model on the given data        - Select a set of random points in the data to predict the model        - Store the predicted values as a list- Plot the predicted values along with the random data points and true function as given above. Hints:sklearn.PolynomialFeatures() : Generates polynomial and interaction featuressklearn.LinearRegression() : LinearRegression fits a linear modelsklearn.fit() : Fits the linear model to the training datasklearn.predict() : Predict using the linear model.Note: This exercise is **auto-graded and you can try multiple attempts.**<jupyter_code>#Importing libraries
%matplotlib inline
import numpy as np
import scipy as sp
import matplotlib as mpl
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
#Helper function to set plot characteristics

def make_plot():
    fig, axes=plt.subplots(figsize=(20,8), nrows=1, ncols=2);
    axes[0].set_ylabel("$p_R$", fontsize=18)
    axes[0].set_xlabel("$x$", fontsize=18)
    axes[1].set_xlabel("$x$", fontsize=18)
    axes[1].set_yticklabels([])
    axes[0].set_ylim([0,1])
    axes[1].set_ylim([0,1])
    axes[0].set_xlim([0,1])
    axes[1].set_xlim([0,1])
    plt.tight_layout();
    return axes
# Reading the file into a dataframe

df = pd.read_csv("noisypopulation.csv")
df.head()
### edTest(test_data) ###

# Set the predictor and response variable
# Column x is the predictor and column y is the response variable.
# Column f is the true function of the given data
# Select the values of the columns

x = df.___
y = df.___
f = df.___
### edTest(test_poly) ###
# Function to compute the Polynomial Features for the data x for the given degree d
def polyshape(d, x):
    return PolynomialFeatures(___).fit_transform(___.reshape(-1,1))
### edTest(test_linear) ###
#Function to fit a Linear Regression model 
def make_predict_with_model(x, y, x_pred):
    
    #Create a Linear Regression model with fit_intercept as False
    lreg = ___
    
    #Fit the model to the data x and y
    lreg.fit(___, ___)
    
    #Predict on the x_pred data
    y_pred = lreg.predict(___)
    return lreg, y_pred
#Function to perform bootstrap and fit the data

# degree is the maximum degree of the model
# num_boot is the number of bootstraps
# size is the number of random points selected from the data for each bootstrap
# x is the predictor variable
# y is the response variable

def gen(degree, num_boot, size, x, y):
    
    #Create 2 lists to store the prediction and model
    predicted_values, linear_models =[], []
    
    #Run the loop for the number of bootstrap value
    for i in range(num_boot):
        
        #Helper code to call the make_predict_with_model function to fit the data
        indexes=np.sort(np.random.choice(x.shape[0], size=size, replace=False))
        
        #lreg and y_pred hold the model and predicted values of the current bootstrap
        lreg, y_pred = make_predict_with_model(polyshape(degree, x[indexes]), y[indexes], polyshape(degree, x))
        
        #Append the model and predicted values into the appropriate lists
        predicted_values.append(___)
        linear_models.append(___)
    
    #Return the 2 lists
    return predicted_values, linear_models
### edTest(test_gen) ###
# Call the function gen twice with x and y as the predictor and response variable respectively
# The number of bootstraps should be 200 and the number of samples from the dataset should be 30
# Store the return values in appropriate variables

# Get results for degree 1
predicted_1, model_1 = gen(___);

# Get results for degree 100
predicted_100, model_100 = gen(___);
#Helper code to plot the data

indexes=np.sort(np.random.choice(x.shape[0], size=30, replace=False))

plt.figure(figsize = (12,8))
axes=make_plot()

#Plot for Degree 1
axes[0].plot(x,f,label="f", color='darkblue',linewidth=4)
axes[0].plot(x, y, '.', label="Population y", color='#009193',markersize=8)
axes[0].plot(x[indexes], y[indexes], 's', color='black', label="Data y")

for i,p in enumerate(predicted_1[:-1]):
    axes[0].plot(x,p,alpha=0.03,color='#FF9300')
axes[0].plot(x, predicted_1[-1], alpha=0.3,color='#FF9300',label="Degree 1 from different samples")


#Plot for Degree 100
axes[1].plot(x,f,label="f", color='darkblue',linewidth=4)
axes[1].plot(x, y, '.', label="Population y", color='#009193',markersize=8)
axes[1].plot(x[indexes], y[indexes], 's', color='black', label="Data y")


for i,p in enumerate(predicted_100[:-1]):
    axes[1].plot(x,p,alpha=0.03,color='#FF9300')
axes[1].plot(x,predicted_100[-1],alpha=0.2,color='#FF9300',label="Degree 100 from different samples")

axes[0].legend(loc='best')
axes[1].legend(loc='best')

#edgecolor='black', linewidth=3, facecolor='green',
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11010530205061889, 'fraction_numerical': 0.024200997598374283, 'mean_word_length': 4.457661290322581, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '540880', 'license': 'MIT', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1681, 'n_words': 583, 'path': 'docs/lectures/lecture11/notebook/s4-exa2-challenge.ipynb', 'repo_name': 'r34g4n/2020-CS109A', 'size': '5413'}