<jupyter_start><jupyter_text>Importing Libraries & getting Data<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.read_csv('dataset/train.csv')

# pd.set_option('display.max_columns', 500)
# pd.set_option('display.max_rows', 500)
# pd.set_option('display.width', 500)

data.head()
data.shape
data.describe()
data.columns<jupyter_output><empty_output><jupyter_text>Skewness (shape of distribution of values)<jupyter_code>print("Skew : ",data.SalePrice.skew())
plt.hist(data.SalePrice, color='green', ec='black')
plt.show()<jupyter_output>Skew :  1.8828757597682129<jupyter_text>--> Log-transforming the target variable when it is skewed, to improve the linearity of the data<jupyter_code>target = np.log(data.SalePrice)
print("Skew : ",target.skew())
plt.hist(target, color='red', ec='black')
plt.show()<jupyter_output>Skew :  0.12133506220520406<jupyter_text>Correlation<jupyter_code>plt.figure(figsize=(10,10))
sns.heatmap(data.corr(), yticklabels=True, cbar=True)
data.corr()['SalePrice'].sort_values(ascending=False)<jupyter_output><empty_output><jupyter_text>So, OverallQual & GrLivArea are the most positively correlated with SalePrice.  Analyzing OverallQual<jupyter_code>data.OverallQual.unique()
sns.scatterplot(x='OverallQual', y='SalePrice' , data=data)

plt.title('SalePrice vs Overall Quality')
plt.show()

quality_pivot = data.pivot_table(index='OverallQual', values='SalePrice' ,aggfunc=np.median)

quality_pivot.plot(kind='bar',color='lightblue' )
plt.xlabel('Overall-Quality')
plt.ylabel('Sale-Price')

plt.title('SalePrice vs Quality')
plt.xticks(rotation=0)
plt.show()<jupyter_output><empty_output><jupyter_text>Analyzing GrLivArea (Ground-Living-Area)<jupyter_code>sns.scatterplot(data=data, x="GrLivArea", y="SalePrice")

plt.title('SalePrice vs Ground-Living-Area')
plt.show()<jupyter_output><empty_output><jupyter_text>Analyzing Garage<jupyter_code>sns.scatterplot(data=data, x="GarageCars", y="SalePrice")

plt.title('SalePrice vs Garage-Cars')
plt.show()

sns.scatterplot(data=data, x="GarageArea", y="SalePrice")

plt.title('SalePrice vs Garage-Area')
plt.show()<jupyter_output><empty_output><jupyter_text>Handling Missing Values<jupyter_code>data.info()
def percent_missing_data(data):
    missing_count = data.isnull().sum().sort_values(ascending=False)
    missing_percent =100 * data.isnull().sum().sort_values(ascending=False) / len(data)

    missing_count = pd.DataFrame(missing_count[missing_count > 0])
    missing_percent = pd.DataFrame(missing_percent[missing_percent > 0])

    missing_values_table = pd.concat([missing_count ,missing_percent] ,axis=1)

    missing_values_table.columns = ["missing_count", "missing_percent"]

    print('The dataset consists of {0} columns , out of which {1} have missing values.'.format(data.shape[1],str(missing_values_table.shape[0])))

    return missing_values_table

missing_values = percent_missing_data(data)
missing_values<jupyter_output>The dataset consists of 81 columns , out of which 19 have missing values.<jupyter_text>--> Dropping the columns which have more than 80% of data as missing.<jupyter_code>threshhold = 80 

drop_cols = missing_values[missing_values["missing_percent"] > threshhold].index.tolist()
drop_cols
data = data.drop(columns=drop_cols )
data.shape
missing_values = percent_missing_data(data)
missing_values<jupyter_output>The dataset consists of 77 columns , out of which 15 have missing values.<jupyter_text>--> Dropping the columns which have less than 3% of data as missing , as that is considered to be negligible and it doesn't have a big impact on our model.<jupyter_code>threshhold = 3
drop_cols_less = missing_values[missing_values["missing_percent"] < threshhold].index.tolist()
drop_cols_less
data = data.drop(columns=drop_cols_less)
data.shape
missing_values = percent_missing_data(data)
missing_values<jupyter_output>The dataset consists of 69 columns , out of which 7 have missing values.<jupyter_text>Imputation  Garage Features<jupyter_code>garage_features = [feature for feature in missing_values.index if 'Garage' in feature]
print('Number of Garage Features are: {}'.format(len(garage_features)))
# Missing values can be filled using mean--> (for numerical) and mode--> (for categorical)

# for finding mode
data['GarageType'].value_counts(), data['GarageCond'].value_counts(), data['GarageFinish'].value_counts(), data['GarageQual'].value_counts()

# replacing with mode
data['GarageType'] = data['GarageType'].fillna('Attchd')
data['GarageCond'] = data['GarageCond'].fillna('TA')
data['GarageFinish'] = data['GarageFinish'].fillna('Unf')
data['GarageQual'] = data['GarageQual'].fillna('TA')

# replacing with mean
data['GarageYrBlt'] = data['GarageYrBlt'].fillna(data.GarageYrBlt.mean())

missing_values = percent_missing_data(data)
missing_values<jupyter_output>The dataset consists of 69 columns , out of which 2 have missing values.<jupyter_text>Other Features<jupyter_code>data['FireplaceQu'].value_counts()
data['LotFrontage'].value_counts()
# Fireplace is a categorical feature, so can be filled with None.
# And Lotfrontage is a numerical feature with outliers, so can be filled with median.

data['FireplaceQu'] = data['FireplaceQu'].fillna('None')
data['LotFrontage'] = data['LotFrontage'].fillna(data.LotFrontage.median())

missing_values = percent_missing_data(data)
missing_values<jupyter_output>The dataset consists of 69 columns , out of which 0 have missing values.<jupyter_text>Feature Selection   Numerical Features<jupyter_code>data_numeric = data.select_dtypes(include=[np.number])
data_categ = data.select_dtypes(include="object")
data_numeric
data_categ
data_numeric.shape, data_categ.shape
data_numeric_list = list(data_numeric.columns.values)
data_numeric_list

data_categ_list = list(data_categ.columns.values)
data_categ_list

# categorical to numeric 

for feature in data.select_dtypes(include="object"):
    labels_ordered = data.groupby([feature])['SalePrice'].mean().sort_values().index
    labels_ordered = {k: i for i, k in enumerate(labels_ordered, 0)}
    data[feature] = data[feature].map(labels_ordered)

data.head()<jupyter_output><empty_output><jupyter_text>Feature Scaling<jupyter_code>X = data.drop(['Id', 'SalePrice'] , axis=1)
y = np.log(data.SalePrice)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
from sklearn.preprocessing import StandardScaler 
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
X_train.shape , X_test.shape<jupyter_output><empty_output><jupyter_text>Model Buliding  Ridge Regression<jupyter_code>from sklearn.linear_model import Ridge 

ridge_reg = Ridge(alpha=100)
ridge_reg.fit(X_train ,y_train) 

y_pred_ridge =ridge_reg.predict(X_test)
# testing the model 
from sklearn.metrics import r2_score ,mean_absolute_error 

print("Mean Absolute Error : " ,mean_absolute_error(y_test ,y_pred_ridge))
print("R2 score :" ,r2_score(y_test ,y_pred_ridge))
# finding best value for alpha and train model again

alpha_list = []
mae_list = []
for alpha_val in np.arange(0.01, 200):
    ridge_ = Ridge(alpha=alpha_val)
    ridge_.fit(X_train, y_train)
    alpha_list.append(alpha_val)

    # testing the model
    y_pred_ridge = ridge_.predict(X_test)
    mae = mean_absolute_error(y_test, y_pred_ridge)
    mae_list.append(mae)

alpha_list = pd.DataFrame(alpha_list)
mae_list = pd.DataFrame(mae_list)
alpha_mae = pd.concat([alpha_list, mae_list], axis=1)
alpha_mae.columns = ["alpha_list", "mae_list"]

alpha_mae[alpha_mae["mae_list"] == alpha_mae["mae_list"].min()]

# training and testing once again with the best alpha value 

ridge_reg = Ridge(alpha=199.01)
ridge_reg.fit(X_train, y_train)

y_pred_ridge = ridge_reg.predict(X_test)

# testing the model

print("Mean Absolute Error : ", mean_absolute_error(y_test, y_pred_ridge))
print("R2 score :", r2_score(y_test, y_pred_ridge))

y_pred_ridge.min()
plt.figure(figsize=(10,10))
sns.regplot(x=y_pred_ridge ,y=y_test)<jupyter_output><empty_output><jupyter_text>Lasso Regression<jupyter_code>from sklearn.linear_model import Lasso 
lasso = Lasso(alpha=0.8)
lasso.fit(X_train ,y_train)
y_pred_lasso = lasso.predict(X_test)
# testing the model
from sklearn.metrics import r2_score, mean_absolute_error

print("Mean Absolute Error : ", mean_absolute_error(y_test, y_pred_lasso))
print("R2 score :", r2_score(y_test, y_pred_lasso))

# finding best value for alpha and train model again

alpha_list = []
mae_list = []
for alpha_val in np.arange(0.01, 200):
    lasso_ = Lasso(alpha=alpha_val)
    lasso_.fit(X_train, y_train)
    alpha_list.append(alpha_val)

    # testing the model
    y_pred_lasso = lasso_.predict(X_test)
    mae = mean_absolute_error(y_test, y_pred_lasso)
    mae_list.append(mae)

alpha_list = pd.DataFrame(alpha_list)
mae_list = pd.DataFrame(mae_list)
alpha_mae = pd.concat([alpha_list, mae_list], axis=1)
alpha_mae.columns = ["alpha_list", "mae_list"]

alpha_mae[alpha_mae["mae_list"] == alpha_mae["mae_list"].min()]

# training and testing once again with the best alpha value

lasso = Lasso(alpha=0.01)
lasso.fit(X_train, y_train)
y_pred_lasso = lasso.predict(X_test)

# testing the model

print("Mean Absolute Error : ", mean_absolute_error(y_test, y_pred_lasso))
print("R2 score :", r2_score(y_test, y_pred_lasso))

y_pred_lasso.min()
plt.figure(figsize=(10, 10))
sns.regplot(x=y_pred_lasso, y=y_test)<jupyter_output><empty_output><jupyter_text>Elastic Net<jupyter_code>from sklearn.preprocessing import PolynomialFeatures 

polynomial_converter = PolynomialFeatures(degree=2, include_bias=False)
poly_features_train = polynomial_converter.fit_transform(X_train)
poly_features_test = polynomial_converter.fit_transform(X_test)

from sklearn.linear_model import ElasticNetCV 

elastic = ElasticNetCV(l1_ratio= 1,tol=0.01)
elastic.fit(poly_features_train , y_train)
y_pred_elastic = elastic.predict(poly_features_test)

#testing model

print("Mean Absolute Error : ", mean_absolute_error(y_test, y_pred_elastic))
print("R2 score :", r2_score(y_test, y_pred_elastic))

y_pred_elastic.min()
plt.figure(figsize=(10, 10))
sns.regplot(x=y_pred_elastic, y=y_test)<jupyter_output><empty_output><jupyter_text>Model Evaluation<jupyter_code># creating Dataframe to check which regression technique was the best 

models = pd.DataFrame({
    'Regression Model': ['Ridge', 'Lasso', 'Elastic Net'],
    'MAE Score': [
        mean_absolute_error(y_test, y_pred_ridge),
        mean_absolute_error(y_test, y_pred_lasso),
        mean_absolute_error(y_test, y_pred_elastic)],
    'R2 Score': [
        r2_score(y_test, y_pred_ridge),
        r2_score(y_test, y_pred_lasso),
        r2_score(y_test, y_pred_elastic)
    ]})

models.sort_values(by='MAE Score', ascending=True)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup