# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ### Importing Libraries

import numpy as np
import pandas as pd
import scipy.stats as stats
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline
import sklearn
from sklearn import datasets
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import mean_squared_error
# to visualise al the columns in the dataframe
pd.pandas.set_option('display.max_columns', None)

# ### Importing data set

boston = datasets.load_boston()
bos = pd.DataFrame(boston.data, columns = boston.feature_names)
bos['PRICE'] =boston.target

# ### Exploratory Data Analysis

bos.head()

bos.info()

bos.describe()

# +
## Lets analyse the continuous values by creating histograms to understand the distribution

for feature in bos.columns:
    data=bos.copy()
    data[feature].hist(bins=25)
    plt.xlabel(feature)
    plt.ylabel("Count")
    plt.title(feature)
    plt.show()
# -

# #### Clearly seen as feature 3 is a categorical bariable having value  0 and 1, we have already saw there is no missing values hence we have to chek for any outliers as well as do any transformation for making the data more normal distributed
#
#

# +
## Plotting scatter plots wrt to price with different features


for feature in bos.columns:
    data=bos.copy()
    if 0 in data[feature].unique():
        pass
    else:
        plt.scatter(data[feature],data['PRICE'])
        plt.xlabel(feature)
        plt.ylabel('PRICE')
        plt.title(feature)
        plt.show()
        
# -

## checking correlations
correlation_matrix = bos.copy().corr().round(2)
plt.figure(figsize=(12,8))
sns.heatmap(data=correlation_matrix, annot=True)

# +
## Plotting scatter plots wrt to price with logtransformingdifferent features


for feature in bos.columns:
    data=bos.copy()
    if 0 in data[feature].unique():
        pass
    else:
        data[feature] =np.log10(data[feature])
        data['PRICE'] =np.log10(data['PRICE'])
        plt.scatter(data[feature],data['PRICE'])
        plt.xlabel(feature)
        plt.ylabel('PRICE')
        plt.title(feature)
        plt.show()
        
# -

### Detecting Outliers
for feature in bos.columns:
    data=bos.copy()
    if 0 in data[feature].unique():
        pass
    else:
        data[feature]=data[feature]
        data.boxplot(column=feature)
        plt.ylabel(feature)
        plt.title(feature)
        plt.show()
        

# +
### Given that CRIM, RM, B, PRICE has significant outliers.
### Removing outliers using IQR methods
Q1 = bos.quantile(0.25)
Q3 = bos.quantile(0.75)
IQR = Q3 - Q1

bos_filter_IQR = bos[~((bos< (Q1 - 10* IQR)) |(bos> (Q3 + 10* IQR))).any(axis=1)]
# -

bos.shape

bos_filter_IQR.shape

# ### Model building & Performance

# #### Without removing outliers

# +
X = bos.drop('PRICE', axis = 1)
y = bos['PRICE']

X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=42)

reg_all = LinearRegression()
reg_all.fit(X_train, y_train)

# model evaluation for training set

y_train_predict = reg_all.predict(X_train)
rmse = (np.sqrt(mean_squared_error(y_train, y_train_predict)))
r2 = round(reg_all.score(X_train, y_train),2)

print("The model performance for training set")
print("--------------------------------------")
print('RMSE is {}'.format(rmse))
print('R2 score is {}'.format(r2))
print("\n")
# -

# #### After removing Outliers

# +
X = bos_filter_IQR.drop('PRICE', axis = 1)
y = bos_filter_IQR['PRICE']

X_iqr_train, X_iqr_test, y_iqr_train, y__iqr_test = train_test_split(X,y,test_size=0.2, random_state=42)

reg_all = LinearRegression()
reg_all.fit(X_iqr_train, y_iqr_train)

# model evaluation for training set

y_train_predict = reg_all.predict(X_iqr_train)
rmse = (np.sqrt(mean_squared_error(y_iqr_train, y_train_predict)))
r2 = round(reg_all.score(X_iqr_train, y_iqr_train),2)

print("The model performance for training set")
print("--------------------------------------")
print('RMSE is {}'.format(rmse))
print('R2 score is {}'.format(r2))
print("\n")

# +
### After standardization
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
# Standardize weight
X_iqr_train = scaler.fit_transform(X_iqr_train)

# -

X_iqr_train

y_iqr_train

# +
reg_all = LinearRegression()
reg_all.fit(X_iqr_train, y_iqr_train)

# model evaluation for training set

y_train_predict = reg_all.predict(X_iqr_train)
rmse = (np.sqrt(mean_squared_error(y_iqr_train, y_train_predict)))
r2 = round(reg_all.score(X_iqr_train, y_iqr_train),2)

print("The model performance for training set")
print("--------------------------------------")
print('RMSE is {}'.format(rmse))
print('R2 score is {}'.format(r2))
print("\n")

# -

# #### Performance on Testing set

X_iqr_test = scaler.transform(X_iqr_test)

y_test_predict = reg_all.predict(X_iqr_test)
rmse = (np.sqrt(mean_squared_error(y__iqr_test, y_test_predict)))
r2 = round(reg_all.score(X_iqr_test, y__iqr_test),2)
print("The model performance for testing set")
print("--------------------------------------")
print('RMSE is {}'.format(rmse))
print('R2 score is {}'.format(r2))
print("\n")




StarcoderdataJupyterScriptsDedupFiltered