<jupyter_start><jupyter_text>**Loading of Libraries**<jupyter_code>import pandas as pd
import numpy as np

import warnings
warnings.filterwarnings('ignore')

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
sns.set(style='white', color_codes=True)<jupyter_output><empty_output><jupyter_text>**Data Acquisition (Displaying of house dataset)**<jupyter_code>train = pd.read_csv("/content/drive/MyDrive/Colab Notebooks/train.csv")
test = pd.read_csv("/content/drive/MyDrive/home-data-for-ml-course/test.csv")
data = pd.concat([train,test],ignore_index = True)
#Printing Dimension of datasets
print("Dimension of train dataset", train.shape)
print("Dimension of test dataset", test.shape)
print("Dimension of data dataset", data.shape)
# Showing overview of the train dataset
train.head()
# Showing overview of the test dataset
test.head()
# Showing overview of the Final dataset
data.head()
# Extracting dataframe for only 'number' datatypes
df = data.select_dtypes(include=['float64','int64'])
df.head()
# Showing datatypes of the columns
df.info()
# getting discriptive statistcs on "number" datatypes
df.describe()<jupyter_output><empty_output><jupyter_text>**Missing Values**<jupyter_code>#Identifing missing Values
miss_val = df.isna().sum()
miss_val
# Identifing missing Values > 0 and sorting them by ascending order
miss_val[miss_val>0].sort_values(ascending = False)
columns = ['LotFrontage','GarageYrBlt','MasVnrArea','BsmtHalfBath','BsmtFullBath','GarageArea','GarageCars','TotalBsmtSF',\
                'BsmtUnfSF','BsmtFinSF2','BsmtFinSF1']
for column in columns:
    df[column] = df[column].fillna(df[column].mean())
df.isna().sum()
# Showing overview of the train dataset after imputing missing values
df.head()
#Spliting dataframe into train and test by manually as intially we have two dataframes train and test.
df_train = df[:1460]
df_test = df[1460:]
#Constructing correlation Matrix
data_corr = df_train.corr()
data_corr
#considering only the features which have absolute correlation value greater tha 0.60

data_corr = data_corr['SalePrice'][:-1]
data_corr[abs(data_corr)>=0.60].sort_values(ascending=False)
# df_test.drop(['SalePrice'],axis = 1, inplace = True)
df_train = df_train[['OverallQual','GrLivArea','GarageCars','GarageArea','TotalBsmtSF','1stFlrSF','SalePrice']]
df_test = df_test[['OverallQual','GrLivArea','GarageCars','GarageArea','TotalBsmtSF','1stFlrSF']]
corr = df_train.corr()
plt.figure(figsize=(5,5))
sns.heatmap(corr, annot=True, cmap='cubehelix_r',square=True) 
plt.show()
from sklearn.model_selection import train_test_split
train, val = train_test_split(df_train, test_size=0.25)
#considering only the highly correlated features for training and validation
train_x = train.drop(['SalePrice'], axis = 1)
train_y = train['SalePrice']             # output of the training data

val_x = val.drop(['SalePrice'], axis = 1)
val_y = val['SalePrice']
print('Dimension of train_x dataframe:',train_x.shape)
print('Dimension of train_y dataframe:',train_y.shape)
print('Dimension of val_x dataframe:',val_x.shape)
print('Dimension of val_y dataframe:',val_y.shape)
from sklearn.linear_model import LinearRegression

ln_reg = LinearRegression()
ln_reg.fit(train_x, train_y)
from sklearn.metrics import mean_squared_error,mean_absolute_error, r2_score

df_pred = ln_reg.predict(val_x)
print('Mean Absolute Error of linear regression:',mean_absolute_error(df_pred, val_y))
print('Mean Square Error of linear regression:', mean_squared_error(df_pred, val_y))
print('R_Squared Score of linear regression:', r2_score(df_pred, val_y))
predic_test = ln_reg.predict(df_test)
#final prediction on test data
predic_test = ln_reg.predict(df_test)

#Create Sample Submission file 
submission_data = pd.DataFrame({'Id':test.Id,'Pred_SalePrice':predic_test})
submission_data
val_y = pd.DataFrame(val_y)
val_y['Predicted'] = submission_data['Pred_SalePrice']

val_y
#val_y= val_y.drop("Predicted",axis=1)
val_y<jupyter_output><empty_output><jupyter_text>**Ridge(L2) Regression**<jupyter_code>from sklearn.linear_model import Ridge

ridge = Ridge()
ridge
ridge.fit(train_x,train_y)
ridge_score = ridge.score(val_x,val_y)
coeff_used = np.sum(ridge.coef_ != 0)
ridge.coef_
val_y.isna().sum()
print("training score",ridge_score)
print("number of feature used:",coeff_used)<jupyter_output>training score 0.775313116943729
number of feature used: 6<jupyter_text>**Lasso(L1) Regression**<jupyter_code>from sklearn.linear_model import Lasso 
lasso = Lasso()
lasso
lasso.fit(train_x,train_y)
lasso_score = lasso.score(val_x,val_y)
coeff_used = np.sum(lasso.coef_ != 0)
lasso.coef_
print("training score",lasso_score)
print("number of feature used:",coeff_used)<jupyter_output>training score 0.7753096131413246
number of feature used: 6
{'fraction_non_alphanumeric': 0.12649465072372562, 'fraction_numerical': 0.015733165512901194, 'mean_word_length': 7.780847145488029, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 22, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '91848', 'license': 'Unlicense', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1673, 'n_words': 362, 'path': 'Project/Linear Regression.ipynb', 'repo_name': 'foday1989/FODAY-DS.Portfolio.io', 'size': '4767'}