<jupyter_start><jupyter_text><jupyter_code>import numpy as np # linear algebra
import pandas as pd #data manipulation library
import seaborn as sns
import matplotlib.pyplot as plt #data visuaizition
from matplotlib import style
from matplotlib import pyplot as plt
from sklearn.svm import LinearSVC
from sklearn.metrics import confusion_matrix
from sklearn.datasets.samples_generator import make_blobs
from sklearn import svm
from sklearn import tree
from sklearn.linear_model import LinearRegression # for linear Regression Algorithm
from sklearn.model_selection import train_test_split # to split the dataset for training and testing
from sklearn import metrics # for checking the model accuracy
from pandas import Series, DataFrame
import statsmodels.formula.api as smf
from sklearn.preprocessing import StandardScaler


from google.colab import files
uploaded = files.upload()
df=pd.read_csv('recs2015_public_v4-1.csv') 
df.head()
plt.figure(figsize=(8, 12))
heatmap = sns.heatmap(df.corr()[['KWH']].sort_values(by='KWH', ascending=False), vmin=-1, vmax=1, annot=True, cmap='BrBG')
heatmap.set_title('Features Correlating with total electricity usage', fontdict={'fontsize':18}, pad=16);
df.drop(df.columns.difference(['KWH','TOTSQFT_EN','NCOMBATH','TOTROOMS','FUELPOOL','RECBATH','ROOFTYPE','OTHROOMS','WALLTYPE','HIGHCEIL','UGASHERE','POOL','SOLAR']), 1, inplace=True)
df.to_csv('electricity.csv', index=False)
dataframe = pd.read_csv("electricity.csv") 
dataframe.head()
dataframe.info()
dataframe.describe()
print(dataframe.shape)
# Increase the size of the heatmap.
plt.figure(figsize=(16, 6))
# Store heatmap object in a variable to easily access it when you want to include more features (such as title).
# Set the range of values to be displayed on the colormap from -1 to 1, and set the annotation to True to display the correlation values on the heatmap.
heatmap = sns.heatmap(dataframe.corr(), vmin=-1, vmax=1, annot=True)
# Give a title to the heatmap. Pad defines the distance of the title from the top of the heatmap.
heatmap.set_title('Correlation Heatmap', fontdict={'fontsize':12}, pad=12);
dataframe.corr()
plt.figure(figsize=(8, 12))
heatmap = sns.heatmap(dataframe.corr()[['KWH']].sort_values(by='KWH', ascending=False), vmin=-1, vmax=1, annot=True, cmap='BrBG')
heatmap.set_title('Features Correlating with total electricity usage', fontdict={'fontsize':18}, pad=16);
 model = smf.ols('KWH ~ TOTSQFT_EN + NCOMBATH + TOTROOMS + OTHROOMS + WALLTYPE + ROOFTYPE + HIGHCEIL + UGASHERE + POOL + FUELPOOL + RECBATH + SOLAR',data = dataframe )
results = model.fit()
print(results.summary())
y = dataframe['KWH']
X = dataframe[['TOTSQFT_EN','NCOMBATH','TOTROOMS','OTHROOMS', 'WALLTYPE',  'ROOFTYPE', 'HIGHCEIL', 'UGASHERE', 'POOL', 'FUELPOOL', 'RECBATH', 'SOLAR']]
#Splitting The Data into Training And Testing Dataset Set a variable X equal to the numerical features of the dataframe and a variable y equal to the "Total kilowatts per hour" column

train_X, test_X,train_y,test_y = train_test_split(X, y, test_size=0.3) # our main data split into train and test
# the attribute test_size=0.3 splits the data into 70% and 30% ratio. train=70% and test=30%

train_X.head()
test_X.head()
# Training the Model
#Linear Regression Create an instance of a LinearRegression() model named lm.

lm = LinearRegression() #Create an instance of a LinearRegression() model named lm.

model = lm.fit(train_X,train_y)
predictions = lm.predict(train_X)
print(predictions)
lm.score(train_X,train_y)
# print coefficients
lm.coef_
lm.intercept_
#  Use lm.predict() to predict off the X_test set of the data.
predictions = lm.predict(test_X)
# Evaluating the Model Calculating the residual sum of squares and the explained variance score (R^2)
# Calculate the Mean Absolute Error, Mean Squared Error, and the Root Mean Squared Error.

from sklearn import metrics

print('MAE:', metrics.mean_absolute_error(test_y, predictions))
print('MSE:', metrics.mean_squared_error(test_y, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(test_y, predictions)))

# Residuals
# Plot a histogram of the residuals and make sure it looks normally distributed. Use either seaborn distplot, or just plt.hist().

sns.displot((test_y-predictions),bins=50);
import pandas as pd
import numpy as np
from sklearn import model_selection
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.linear_model import ElasticNet
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from math import sqrt
target_column = ['KWH'] ## create an object of the target variabble called 'target_column'
predictors = list(set(list(df.columns))-set(target_column))## give the lis of alll features excluding KWH
df[predictors] = df[predictors]/df[predictors].max()## normalizes the predictors
df.describe()## displays the summary
X = df[predictors].values ## creates array of the indepedent variables
y = df[target_column].values ## creates array of the depedent variables which is KWH

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=40)
print(X_train.shape); print(X_test.shape)
lr = LinearRegression()
lr.fit(X_train, y_train)
# linear regression 
pred_train_lr= lr.predict(X_train)
print(np.sqrt(mean_squared_error(y_train,pred_train_lr)))
print(r2_score(y_train, pred_train_lr))

pred_test_lr= lr.predict(X_test)
print(np.sqrt(mean_squared_error(y_test,pred_test_lr))) 
print(r2_score(y_test, pred_test_lr))
## ridge regression
rr = Ridge(alpha=0.01)
rr.fit(X_train, y_train) 
pred_train_rr= rr.predict(X_train)
print(np.sqrt(mean_squared_error(y_train,pred_train_rr)))
print(r2_score(y_train, pred_train_rr))

pred_test_rr= rr.predict(X_test)
print(np.sqrt(mean_squared_error(y_test,pred_test_rr))) 
print(r2_score(y_test, pred_test_rr))
## lasso regression
model_lasso = Lasso(alpha=0.01)
model_lasso.fit(X_train, y_train) 
pred_train_lasso= model_lasso.predict(X_train)
print(np.sqrt(mean_squared_error(y_train,pred_train_lasso)))
print(r2_score(y_train, pred_train_lasso))

pred_test_lasso= model_lasso.predict(X_test)
print(np.sqrt(mean_squared_error(y_test,pred_test_lasso))) 
print(r2_score(y_test, pred_test_lasso))
#Elastic Net
model_enet = ElasticNet(alpha = 0.01)
model_enet.fit(X_train, y_train) 
pred_train_enet= model_enet.predict(X_train)
print(np.sqrt(mean_squared_error(y_train,pred_train_enet)))
print(r2_score(y_train, pred_train_enet))

pred_test_enet= model_enet.predict(X_test)
print(np.sqrt(mean_squared_error(y_test,pred_test_enet)))
print(r2_score(y_test, pred_test_enet))
from sklearn.linear_model import Lasso, LassoCV
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt

pd = Lasso()
x = dataframe[['TOTSQFT_EN','NCOMBATH','TOTROOMS','OTHROOMS', 'WALLTYPE',  'ROOFTYPE', 'HIGHCEIL', 'UGASHERE', 'POOL', 'FUELPOOL', 'RECBATH', 'SOLAR']]
y = dataframe['KWH']
xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3)

model = Lasso().fit(x, y) 
print(model)
score = model.score(x, y)
ypred = model.predict(xtest)
mse = mean_squared_error(ytest,ypred)
print("Alpha:{0:.2f}, R2:{1:.2f}, MSE:{2:.2f}, RMSE:{3:.2f}"
    .format(model.alpha, score, mse, np.sqrt(mse)))

x_ax = range(len(ypred))
plt.scatter(x_ax, ytest, s=5, color="blue", label="original")
plt.plot(x_ax, ypred, lw=0.8, color="red", label="predicted")
plt.legend()
plt.show()

alphas = [0.1,0.3, 0.5, 0.8, 1]
lassocv = LassoCV(alphas=alphas, cv=5).fit(x,y)
print(lassocv)
score = lassocv.score(x,y)
ypred = lassocv.predict(xtest)
mse = mean_squared_error(ytest,ypred)
print("Alpha:{0:.2f}, R2:{1:.3f}, MSE:{2:.2f}, RMSE:{3:.2f}"
    .format(lassocv.alpha_, score, mse, np.sqrt(mse)))

x_ax = range(len(xtest))
plt.scatter(x_ax, ytest, s=5, color="blue", label="original")
plt.plot(x_ax, ypred, lw=0.8, color="red", label="predicted")
plt.legend()
plt.show() 
from sklearn.linear_model import Ridge, RidgeCV
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np

bq = Ridge
x = dataframe[['TOTSQFT_EN','NCOMBATH','TOTROOMS','OTHROOMS', 'WALLTYPE',  'ROOFTYPE', 'HIGHCEIL', 'UGASHERE', 'POOL', 'FUELPOOL', 'RECBATH', 'SOLAR']]
y = dataframe['KWH']
xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3)

alphas = [0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1,0.5, 1]
for a in alphas:
 model = Ridge(alpha=a, normalize=True).fit(x,y) 
 score = model.score(x, y)
 pred_y = model.predict(x)
 mse = mean_squared_error(y, pred_y) 
 print("Alpha:{0:.6f}, R2:{1:.3f}, MSE:{2:.2f}, RMSE:{3:.2f}"
    .format(a, score, mse, np.sqrt(mse)))

ridge_mod=Ridge(alpha=0.01, normalize=True).fit(xtrain,ytrain)
ypred = ridge_mod.predict(xtest)
score = model.score(xtest,ytest)
mse = mean_squared_error(ytest,ypred)
print("R2:{0:.3f}, MSE:{1:.2f}, RMSE:{2:.2f}"
   .format(score, mse,np.sqrt(mse)))

x_ax = range(len(xtest))
plt.scatter(x_ax, ytest, s=5, color="blue", label="original")
plt.plot(x_ax, ypred, lw=0.8, color="red", label="predicted")
plt.legend()
plt.show()

# RidgeCV method
ridge_cv=RidgeCV(alphas=alphas, store_cv_values=True)
ridge_mod = ridge_cv.fit(xtrain,ytrain)
print(ridge_mod.alpha_)
print(np.mean(ridge_mod.cv_values_, axis=0))

ypred = ridge_mod.predict(xtest)
score = ridge_mod.score(xtest,ytest)
mse = mean_squared_error(ytest,ypred)
print("R2:{0:.3f}, MSE:{1:.2f}, RMSE:{2:.2f}"
   .format(score, mse, np.sqrt(mse)))

x_ax = range(len(xtest))
plt.scatter(x_ax, ytest, s=5, color="blue", label="original")
plt.plot(x_ax, ypred, lw=0.8, color="red", label="predicted")
plt.legend()
plt.show()
from sklearn.linear_model import ElasticNet,ElasticNetCV
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np

vf = ElasticNet()
x = dataframe[['TOTSQFT_EN','NCOMBATH','TOTROOMS','OTHROOMS', 'WALLTYPE',  'ROOFTYPE', 'HIGHCEIL', 'UGASHERE', 'POOL', 'FUELPOOL', 'RECBATH', 'SOLAR']]
y = dataframe['KWH']
xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3)

alphas = [0.0001, 0.001, 0.01, 0.1, 0.3, 0.5, 0.7, 1]

for a in alphas:
    model = ElasticNet(alpha=a).fit(x,y)   
    score = model.score(x, y)
    pred_y = model.predict(x)
    mse = mean_squared_error(y, pred_y)   
    print("Alpha:{0:.4f}, R2:{1:.2f}, MSE:{2:.2f}, RMSE:{3:.2f}"
       .format(a, score, mse, np.sqrt(mse)))

elastic=ElasticNet(alpha=0.01).fit(xtrain, ytrain)
ypred = elastic.predict(xtest)
score = elastic.score(xtest, ytest)
mse = mean_squared_error(ytest, ypred)
print("R2:{0:.3f}, MSE:{1:.2f}, RMSE:{2:.2f}"
      .format(score, mse, np.sqrt(mse)))

x_ax = range(len(xtest))
plt.scatter(x_ax, ytest, s=5, color="blue", label="original")
plt.plot(x_ax, ypred, lw=0.8, color="red", label="predicted")
plt.legend()
plt.show()


# --- ElasticNetCV ----
elastic_cv=ElasticNetCV(alphas=alphas, cv=5)
model = elastic_cv.fit(xtrain, ytrain)
print(model.alpha_)
print(model.intercept_)

ypred = model.predict(xtest)
score = model.score(xtest, ytest)
mse = mean_squared_error(ytest, ypred)
print("R2:{0:.3f}, MSE:{1:.2f}, RMSE:{2:.2f}"
      .format(score, mse, np.sqrt(mse)))

x_ax = range(len(xtest))
plt.scatter(x_ax, ytest, s=5, color="blue", label="original")
plt.plot(x_ax, ypred, lw=0.8, color="red", label="predicted")
plt.legend()
plt.show()<jupyter_output>Alpha:0.0001, R2:0.35, MSE:32158769.62, RMSE:5670.87
Alpha:0.0010, R2:0.35, MSE:32158806.37, RMSE:5670.87
Alpha:0.0100, R2:0.35, MSE:32162289.78, RMSE:5671.18
Alpha:0.1000, R2:0.35, MSE:32388996.84, RMSE:5691.13
Alpha:0.3000, R2:0.33, MSE:33239546.22, RMSE:5765.37
Alpha:0.5000, R2:0.32, MSE:34032830.46, RMSE:5833.77
Alpha:0.7000, R2:0.30, MSE:34686961.75, RMSE:5889.56
Alpha:1.0000, R2:0.29, MSE:35455231.54, RMSE:5954.43
R2:0.345, MSE:34489222.70, RMSE:5872.75
{'fraction_non_alphanumeric': 0.14320193081255028, 'fraction_numerical': 0.03636363636363636, 'mean_word_length': 5.778080697928027, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '484959', 'license': 'Apache-2.0', 'n_tokens_mistral': 5225, 'n_tokens_neox': 4874, 'n_words': 1040, 'path': 'Final_Demo.ipynb', 'repo_name': 'Amruta5199/handson-ml', 'size': '12430'}