<jupyter_start><jupyter_text>**From the dataset, fit a linear model on the relationship between 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>import pandas as pd
import numpy as np
from sklearn.preprocessing  import MinMaxScaler
np.random.seed(42)

df = pd.read_csv("/energydata_complete.csv")
df.head()
df.shape
df.isna().sum()
df = df.drop(columns=["date","lights"])
df.shape
df.head()
scaler = MinMaxScaler()
norm_df = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)
norm_df.head()

x = df.T2
y = df. T6

array = np.array(x)
x_reshaped = array.reshape(-1,1)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x_reshaped,y, test_size=0.3, random_state=42)
from sklearn.linear_model import LinearRegression
classifier = LinearRegression()
classifier.fit(x_train, y_train)
predictions = classifier.predict(x_test)
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
r2 = r2_score(y_test, predictions)
r2.round(2)<jupyter_output><empty_output><jupyter_text>The r2 value is 0.64 **What is the Mean Absolute Error (in two decimal places)?** of the whole dataset<jupyter_code>new_x = norm_df.iloc[:,1:]
new_y = norm_df.iloc[:,0]

x_train, x_test, y_train, y_test = train_test_split(new_x,new_y, test_size=0.3, random_state=42)
Model = LinearRegression()
Model.fit(x_train,y_train)
new_pred = Model.predict(x_test)
mae = mean_absolute_error(y_test, new_pred)
mae.round(2)<jupyter_output><empty_output><jupyter_text>Mean aboslute Error is 0.05 What is the Residual Sum of Squares (in two decimal places)?<jupyter_code>rss = np.sum(np.square(y_test - new_pred))
rss.round(2)<jupyter_output><empty_output><jupyter_text>What is the Root Mean Squared Error (in three decimal places)?<jupyter_code>rmse = np.sqrt(mean_squared_error(y_test, new_pred))
rmse.round(3)
What is the Coefficient of Determination (in two decimal places)?
r2_value = r2_score(y_test, new_pred)
r2_value.round(2)<jupyter_output><empty_output><jupyter_text>Obtain the feature weights from your linear model above. Which features have the lowest and highest weights respectively?<jupyter_code>def determine_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].round(3)
    return weights_df
model_weights = determine_weights_df(Model, x_train, 'Linear_Model_Weight')
model_weights<jupyter_output><empty_output><jupyter_text>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>from sklearn.linear_model import  Ridge, Lasso
R_model = Ridge(alpha = 0.4)
R_model.fit(x_train, y_train)
R_preds = R_model.predict(x_test)
R_rmse = np.sqrt(mean_squared_error(y_test, R_preds))
round(R_rmse, 3)<jupyter_output><empty_output><jupyter_text>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>lasso_model = Lasso(alpha= 0.001)
lasso_model.fit(x_train, y_train)
lasso_preds = lasso_model.predict(x_test)
lasso_weights = determine_weights_df(lasso_model, x_train, 'Lasso_weight')
lasso_weights
non_zero_feature_weight = np.sum(lasso_weights != 0)
non_zero_feature_weight<jupyter_output><empty_output><jupyter_text>What is the new RMSE with the Lasso Regression (in 3 decimal places)?<jupyter_code>L_rmse = np.sqrt(mean_squared_error(y_test, lasso_preds))

round(L_rmse, 3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10388918486947256, 'fraction_numerical': 0.014651038891848695, 'mean_word_length': 7.198689956331878, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '592647', 'license': 'Apache-2.0', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1351, 'n_words': 323, 'path': 'HAMOYE_STAGEB.ipynb', 'repo_name': 'Favour-Aiyegbeni/hamoyetest', 'size': '3754'}