# ---
# 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
# ---

# <pre style='font-size : 25px; text-align : center '><b>ASSIGNMENT 2</b></pre>
# <pre style='font-size : 20px;'><b>PREDICTING HOUSING PRICES WITH POLYNOMIAL REGRESSION</b></pre>
# <pre style='font-size : 15px'>Today we will continue working with boston housing data. 
# Recap from the previous lesson that we managed to train the <i>linear regression</i> 
# classifier on two features and as the result obtained mse score of 44 on train 
# and 38 on test data. This time we will use <i>linear regression</i> 
# on all the features and visualize the correlation between them and our target value. 
# We will also try using <i>polynomial regression</i> to solve the problem. 
# Finnaly, we will compare the results with the one from the previous assignment. </pre>
#  
# <i>Note : that's a test version of a notebook</i>

# <pre style='font-size : 20px'><b>PART 1</b></pre>
# <pre style='font-size : 18px'><b>Visualizing correlations</b></pre>
# <pre style='font-size : 15px;'>Firstly let's import necessary packages. </pre>

import pandas as pd
from pandas.plotting import scatter_matrix
import matplotlib.pyplot as  plt 
import seaborn as sns

#just for conviniece we will set a grid background of figures using seaborn
sns.set()


# <pre style='font-size : 15px'>We created a function <b>load_boston_df</b> that loads data and transforms it to pandas DataFrame, 
# thus you don't need replicate the code from the first assignment. </pre>

def load_boston_df():
    from sklearn import datasets
    boston_data = datasets.load_boston()
    columns_to_use = ['data','target','feature_names']
    boston_data = dict([(i,boston_data[i]) for i in columns_to_use])
    df = pd.DataFrame(columns=boston_data['feature_names'], data=boston_data['data'])
    df['target'] = boston_data['target']
    return df


df = load_boston_df()

df.head()

correlation  = df.corr()

correlation

# <pre style='font-size : 15px'>Let's see only the correlation between target and features.</pre>

correlation['target'].sort_values(ascending=False)[1:]

# <pre style='font-size :15px'>Some features have positive correlation with our target, 
# meaning that when the feature increases/dicreases - the target tends to also increase/dicrease, 
# while others have negative correlation which implies that when the feature decreases/increases 
# - target behaves the opposite. 
# Now let's plot the correlation matrix to better understand it. </pre>

# +
#uncomment to see the docs of the function
# #?scatter_matrix
# -

scatter_matrix(df, figsize=(15,10), alpha=0.3);
plt.plot();

# <pre style='font-size :15px'>Let's also plot the dependecy between the target value 
# and the feature with the biggest negative correlation score.</pre>

plt.figure(figsize=(15,10))
plt.scatter(df['target'],df['LSTAT'])
plt.xlabel('target (price)')
plt.ylabel('LSTAT (lower status of population)');
plt.title('Correlation between price of the house and lower status of population');

# <pre style='font-size :15px'>From the chart above it's obvious why the 
# correlation between LSTAT and target is the negative one.</pre>

# <pre style='font-size : 20px'><b>PART 2</b></pre>
# <pre style='font-size : 18px'><b>Using linear regression on all the features</b></pre>
# <pre style='font-size : 15px;'>First let's import necessary packages. </pre>

#you can see some warning messages below, but don't worry they won't effect your code
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

train_columns = list(df.columns)

target_column = train_columns.pop(-1)

train_columns

target_column

#we will set a random seed for the same results
seed = 5

train_set, test_set = train_test_split(df,test_size=0.20, random_state=seed)

X_train, y_train, X_test, y_test = train_set[train_columns], train_set[target_column],\
test_set[train_columns], test_set[target_column]

# <pre style='font_size : 15px'>It's time to create, train and evaluate our classifier</pre>

clf = LinearRegression()

clf.fit(X_train,y_train)

y_predicted_train = clf.predict(X_train)

print("Mean squared error on train data : {0}".format(mean_squared_error(y_predicted_train,y_train)))

y_predicted_test = clf.predict(X_test)

print("Mean squared error on test data : {0}".format(mean_squared_error(y_predicted_test,y_test)))

# <pre style='font-size : 15px'>Using all the features we got much better results. 
# One thing to notice is that the mse score on train data is lower then on test one which 
# is the sign of <i>overfitting</i> about which we will speak more concrete in the next lab, 
# but for now let's move on with this one.</pre>

# <pre style='font-size : 20px'><b>PART 3</b></pre>
# <pre style='font-size : 18px'><b>Using polynomial regression on all the features</b></pre>

from sklearn.preprocessing import PolynomialFeatures

clf = LinearRegression()

train_set, test_set = train_test_split(df,test_size=0.20, random_state=seed)

X_train, y_train, X_test, y_test = train_set[train_columns], train_set[target_column],\
test_set[train_columns], test_set[target_column]

# <pre style='font-size : 15px'>To use polynomial regression we firstly need to process them via PolynomialFeatures class</pre>

#C- degree of polynom. Feel free to play around with number and see the perfomance of the model depending on it
C = 2

poly = PolynomialFeatures(C)

poly_features_train = poly.fit_transform(X_train)

clf.fit(poly_features_train,y_train)

poly_features_test = poly.transform(X_test)

y_predicted_train = clf.predict(poly_features_train)

print("Mean squared error on train data : {0}".format(mean_squared_error(y_predicted_train,y_train)))

y_predicted_test = clf.predict(poly_features_test)

print("Mean squared error on test data : {0}".format(mean_squared_error(y_predicted_test,y_test)))

# <pre style='font-size : 15px'>Using polynomial regression with degree of 2 we got much better 
# results on both train and test data, but we see that there is a gap between the model's perfomance. 
# Model does much better job on train data which is obviously an example of overfitting. 
# In the next lesson we will try to fix this situation using <i>regularization</i> 
# and <i>by scaling</i> our data.</pre>



StarcoderdataJupyterScriptsDedupFiltered