<jupyter_start><jupyter_text>Question 12 From the dataset, fit a linear model on the relationship betwen the temperature in the living room in Celsius (x =T2) and the temperature outside the building (y=T6). What is the R^2 value in two D.P?<jupyter_code>from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
x = df[["T2"]]
y = df[["T6"]]

#Now, we split our dataset into the training and testing dataset. Recall that we had earlier segmented the features and target variables.
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)

#we create a Linear Regression Object
lre = LinearRegression()

#fit the model to the training dataset
lre.fit(x_train, y_train)

#obtain predictions
y_hat = lre.predict(x_test)

#R squared
from sklearn.metrics import r2_score
r2_score = r2_score(y_test, y_hat)
print('The Rsquared value to two decimal places is given as',round(r2_score, 2) )<jupyter_output>The Rsquared value to two decimal places is given as 0.64<jupyter_text>Preprocessing<jupyter_code>#First, we normalise our dataset to a common scale using the min max scaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression

# create a MinMaxScaler Object
scaler = MinMaxScaler()
norm_df = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)

#next we divide our Energy_data into feature and Target Variables
feature = norm_df.drop(columns=['Appliances'])
target = norm_df['Appliances']
#Now, we split our dataset into the training and testing dataset. Recall that we had earlier segmented the features and target variables.
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(feature, target, test_size=0.3, random_state=42)
#we create a Linear Regression Object
lim = LinearRegression()

#fit the model to the training dataset
lim.fit(x_train, y_train)

#obtain predictions
predicted_values = lim.predict(x_test)<jupyter_output><empty_output><jupyter_text>Question 13 What is the Mean Absolute Error (in two decimal places)?<jupyter_code>from sklearn.metrics import mean_absolute_error
mae = mean_absolute_error(y_test, predicted_values)
print('The Mean Absolute Error in two decimal places is', round(mae, 2))<jupyter_output>The Mean Absolute Error in two decimal places is 0.05<jupyter_text>Question 14 What is the Residual Sum of Squares (in two decimal places)?<jupyter_code>rss = np.sum(np.square(y_test - predicted_values))
print('The Residual Sum of Squares in two decimal places is', round(rss, 2))<jupyter_output>The Residual Sum of Squares in two decimal places is 45.35<jupyter_text>Question 15 What is the Root Mean Squared Error (in three decimal places)?<jupyter_code>from sklearn.metrics import  mean_squared_error
rmse = np.sqrt(mean_squared_error(y_test, predicted_values))
print('The RMSE in three decimal places is',round(rmse, 3))<jupyter_output>The RMSE in three decimal places is 0.088<jupyter_text>Question 16 What is the Coefficient of Determination (in two decimal places)?<jupyter_code>from sklearn.metrics import r2_score
r2_score = r2_score(y_test, predicted_values)
print('The Coefficient of Determination in two decimal places is',round(r2_score, 2))<jupyter_output>The Coefficient of Determination in two decimal places is 0.15<jupyter_text>Question 17 Obtain the feature weights from your linear model above. Which features have the lowest and highest weights respectively?<jupyter_code>#this function returns the weight of every feature
def get_weights_df(model, feat, col_name):

  weights = pd.Series(model.coef_, feat.columns).sort_values()
  weights_df = pd.DataFrame(weights).reset_index()
  weights_df.columns = ['Features', col_name]
  weights_df[col_name] = weights_df[col_name].round(3)
  return weights_df

linear_model_weights = get_weights_df(lim, x_train, 'Linear_Model_Weight')
linear_model_weights<jupyter_output><empty_output><jupyter_text>From the above, RH_2 and RH_1 are the features with lowest and highest weights respectively  Question 18 Train a ridge regression model with an alpha value of 0.4. Is there any change to the root mean squared error (RMSE) when evaluated on the test set?<jupyter_code>#Now lets apply Ridge Regression
from sklearn.linear_model import Ridge
#we create a Ridge Regression Object
ridge_reg = Ridge(alpha=0.4)

#fit the model to the training dataset
ridge_reg.fit(x_train, y_train)

#obtain predictions
y_hat = ridge_reg.predict(x_test)
# next we calculate RMSE after applying Ridge Regression
from sklearn.metrics import  mean_squared_error
rmse = np.sqrt(mean_squared_error(y_test, y_hat))
print('The RMSE in three decimal places after applying Ridge regression is',round(rmse, 3))<jupyter_output>The RMSE in three decimal places after applying Ridge regression is 0.088<jupyter_text>from both regressions (Linear Regression and Ridge Regression), there is no difference in the RMSE value  Question 19 Train a lasso regression model with an alpha value of 0.001 and obtain the new feature weights with it. How many of the features have non-zero feature weights?<jupyter_code># lets train a lasso regression model
from sklearn.linear_model import Lasso

#we create a Ridge Regression Object
las_reg = Lasso(alpha=0.001)

#fit the model to the training dataset
las_reg.fit(x_train, y_train)

#obtain predictions
y_hat = las_reg.predict(x_test)
#this function returns the weight of every feature
def get_weights_df(model, feat, col_name):
  weights = pd.Series(model.coef_, feat.columns).sort_values()
  weights_df = pd.DataFrame(weights).reset_index()
  weights_df.columns = ['Features', col_name]
  weights_df[col_name] = weights_df[col_name]
  return weights_df

lasso_weights_df = get_weights_df(las_reg, x_train, 'Lasso_weight')
(lasso_weights_df['Lasso_weight']==0).value_counts()<jupyter_output><empty_output><jupyter_text>from the above, there are four (5) non-zero features  Question 20 What is the new RMSE with the Lasso Regression(in 3 decimal places)?<jupyter_code>from sklearn.metrics import  mean_squared_error
rmse = np.sqrt(mean_squared_error(y_test, y_hat))
print('The RMSE in three decimal places with Lasso regression is',round(rmse, 3))<jupyter_output>The RMSE in three decimal places with Lasso regression is 0.094
{'fraction_non_alphanumeric': 0.07438540840602696, 'fraction_numerical': 0.014115781126090405, 'mean_word_length': 6.561151079136691, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '181898', 'license': 'Apache-2.0', 'n_tokens_mistral': 2060, 'n_tokens_neox': 1947, 'n_words': 684, 'path': 'Hamoye-Regression-Quiz.ipynb', 'repo_name': 'Behordeun/Hamoye-Internship', 'size': '6305'}