<jupyter_start><jupyter_text>Multiple Regression for Sklearn Datasets - Boston Housing<jupyter_code>from sklearn import datasets
import pandas as pd
# Lets load the boston dataset from sklearn that would return the bunch
boston = datasets.load_boston()
# The first step would be to convert the dataset into DataFrame
df = pd.DataFrame(data=boston.data, columns=boston.feature_names)
df.head()
# You'll notice that the above dataframe does not contain the target element MEDV. That is because that is stored in target
boston.target
# lets make this target a part of the dataframe as we want to find correlation
df['MEDV'] = boston.target
df.head()
# As we have all the features and target in one df now, lets find more about this df
df.info()
# Lets use describe to get some statistics about the df
df.describe()
# Dataset looks good as there are no null values and rest of the values look good as well.
# Lets find the correlation to select the most appropriate features
correlation_matrix = df.corr()
correlation_matrix
# Lets convert this correlation_matrix into heatmap for visual understanding of the correlation.
# For that lets use seaborn
import seaborn as sns 
import matplotlib.pyplot as plt
plt.subplots(figsize=(20,15))
sns.heatmap(correlation_matrix, annot=True)<jupyter_output><empty_output><jupyter_text>Modelling using 2 features<jupyter_code># From the above heatmap, it is evident, that two of the features are having a high corr with MEDV
# RM with 0.7 corr and LSTAT with -.74 corr
# For the purpose of this notebook, lets take only these 2 features
df_corr = df[['RM', 'LSTAT']]
# In order to work further with the data, lets first split the data into train and test
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(df_corr, df['MEDV'], test_size=0.2)
# Lets see the summary of the data for each of module of test and train
print("X_train: ", X_train.shape)
print("X_test: ", X_test.shape)
print("y_train: ", y_train.shape)
print("y_test: ", y_test.shape)
# Lets fit the LinearRegression model to our train set data
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X_train,y_train)
# Lets predict the dependent values
y_train_predict = lr.predict(X_train)
y_train_predict.shape
# Lets now plot to see how it looks like
plt.scatter(y_train, y_train_predict)
# In order to find how the model is doing, lets find Mean Squared, Root Mean Squared Error and R-Squared
from sklearn.metrics import r2_score, mean_squared_error
import numpy as np
print("MSE: ", mean_squared_error(y_train, y_train_predict))
print("RMSE: ", np.sqrt(mean_squared_error(y_train, y_train_predict)))
print("R-Squared: ", r2_score(y_train, y_train_predict))
# The model looks pretty good based on above values, lets predict the values for test set now
y_test_predict = lr.predict(X_test)
plt.scatter(y_test, y_test_predict)
# In order to find how the model is doing, lets find Mean Squared, Root Mean Squared Error and R-Squared
from sklearn.metrics import r2_score, mean_squared_error
import numpy as np
print("MSE: ", mean_squared_error(y_test, y_test_predict))
print("RMSE: ", np.sqrt(mean_squared_error(y_test, y_test_predict)))
print("R-Squared: ", r2_score(y_test, y_test_predict))<jupyter_output>MSE:  36.8352183009584
RMSE:  6.0692024435636025
R-Squared:  0.6020513996264707<jupyter_text>Modelling using all the features<jupyter_code># Lets split test and train for the whole dataset
# Lets first get the dataframe for features except the MEDV which is the target
df_all = pd.DataFrame(data=boston.data, columns=boston.feature_names)
df_medv = pd.DataFrame(data=boston.target)
df_medv.columns = ['Price']
# lets check how our dataframes look like
df_all
df_medv
# Lets train and test split
X_train_all, X_test_all, y_train_all, y_test_all = train_test_split(df_all, df_medv, test_size=0.2)
# Lets see the summary of the data for each of module of test and train
print("X_train: ", X_train_all.shape)
print("X_test: ", X_test_all.shape)
print("y_train: ", y_train_all.shape)
print("y_test: ", y_test_all.shape)
# Lets fit LinearRegression model
lr2 = LinearRegression()
lr2.fit(X_train_all, y_train_all)
y_train_predict_all = lr2.predict(X_train_all)
# Lets plot
plt.scatter(y_train_all, y_train_predict_all)
# In order to find how the model is doing, lets find Mean Squared, Root Mean Squared Error and R-Squared
print("MSE: ", mean_squared_error(y_train_all, y_train_predict_all))
print("RMSE: ", np.sqrt(mean_squared_error(y_train_all, y_train_predict_all)))
print("R-Squared: ", r2_score(y_train_all, y_train_predict_all))
# Lets now predict the test set
y_test_predict_all = lr2.predict(X_test_all)
# Lets plot
plt.scatter(y_test_all, y_test_predict_all)
# In order to find how the model is doing, lets find Mean Squared, Root Mean Squared Error and R-Squared
print("MSE: ", mean_squared_error(y_test_all, y_test_predict_all))
print("RMSE: ", np.sqrt(mean_squared_error(y_test_all, y_test_predict_all)))
print("R-Squared: ", r2_score(y_test_all, y_test_predict_all))
results = {"All Features": {"MSE": mean_squared_error(y_test_all, y_test_predict_all), 
                            "RMSE": np.sqrt(mean_squared_error(y_test_all, y_test_predict_all)), 
                            "R-Squared": r2_score(y_test_all, y_test_predict_all)},
          
          "2 Features": {"MSE": 36.8352183009584, 
                            "RMSE": 6.0692024435636025, 
                            "R-Squared": 0.6020513996264707}
          }
df_results = pd.DataFrame(results)
df_results<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08635794743429287, 'fraction_numerical': 0.022170570355801895, 'mean_word_length': 5.328054298642534, 'pattern_counts': {'":': 8, '<': 12, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '624171', 'license': 'MIT', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1864, 'n_words': 588, 'path': 'MLR_Sklearn_Datasets.ipynb', 'repo_name': 'raziiq/python-mlr-boston-housing', 'size': '5593'}