<jupyter_start><jupyter_text>Boston Housing DatasetWe will try implementing a Linear Regression Model on the Housing Dataset by finding the appropriate suitable related parameters in the data.  Importing dependenciesImporting dependencies like numpy, pandas, sklearn, matplotlib that we are going to use in the future<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error<jupyter_output>/usr/local/lib/python3.6/dist-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
  import pandas.util.testing as tm<jupyter_text>Loading our datasetWe load the Boston dataset and print it's description to see about what the DataFrame object is.<jupyter_code>ds = datasets.load_boston()
print(ds.DESCR)<jupyter_output>.. _boston_dataset:

Boston house prices dataset
---------------------------

**Data Set Characteristics:**  

    :Number of Instances: 506 

    :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target.

    :Attribute Information (in order):
        - CRIM     per capita crime rate by town
        - ZN       proportion of residential land zoned for lots over 25,000 sq.ft.
        - INDUS    proportion of non-retail business acres per town
        - CHAS     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
        - NOX      nitric oxides concentration (parts per 10 million)
        - RM       average number of rooms per dwelling
        - AGE      proportion of owner-occupied units built prior to 1940
        - DIS      weighted distances to five Boston employment centres
        - RAD      index of accessibility to radial highways
        - TAX      full-value property-tax rate per $10,000
        - PTRATIO  pu[...]<jupyter_text>Converting dataset to DataFrame ObjectWe load our Boston dataset into a Pandas DataFrame object.<jupyter_code>df = pd.DataFrame(data=np.c_[ds['data'], ds['target']], columns=np.append(ds['feature_names'], ['target']))<jupyter_output><empty_output><jupyter_text>Describing our datasetWe display the first 10 entries of the DataFrame object and display the summary of the dataset.<jupyter_code>df.head(10)
df.describe()<jupyter_output><empty_output><jupyter_text>Finding relation between the target and featuresWe plot a graph to see how the target feature vary with different features.<jupyter_code>depends_upon = ds.feature_names
for i in depends_upon:
  x = df[i]
  y = df['target']
  plt.xlabel(i)
  plt.ylabel('target')
  plt.scatter(x, y)
  plt.show()<jupyter_output><empty_output><jupyter_text>Using heatmapsGraphs can give a pretty fair picture about the relationship between the targetted data and the feature. But using a heatmap shows a more accurate picture about the correlation between different features and the target variable.<jupyter_code>plt.figure(figsize=(14,14))
corr_matrix = df.corr().round(2)
sns.heatmap(data=corr_matrix, annot=True)<jupyter_output><empty_output><jupyter_text>Conclusion from the graphs and heatmapsFrom both, the graphs and the heatmap, we can infer that LSTAT and RM could provide us with a better understanding of the data with Regression.We now shape the X and Y variables we are gonna use.<jupyter_code>X = df[['RM', 'LSTAT']]
Y = df[['target']]<jupyter_output><empty_output><jupyter_text>Splitting the datasetWe use ```train_test_split``` to test our dataset into training and testing variables.<jupyter_code>X_train, x_test, Y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=4)<jupyter_output><empty_output><jupyter_text>We create a linear regression model.<jupyter_code>model = LinearRegression()<jupyter_output><empty_output><jupyter_text>We train the Linear Regression model using the fit method.<jupyter_code>model.fit(X_train, Y_train)<jupyter_output><empty_output><jupyter_text>We make predictions for the X values in the testing dataset and store it in the y_hat variable. Now we can compare the y_hat variable with the y_test variable to compare the accuracy of our model.<jupyter_code>y_hat = model.predict(x_test)<jupyter_output><empty_output><jupyter_text>Using the in-built score method we calculate the performance score of our model.<jupyter_code>model.score(x_test, y_test)<jupyter_output><empty_output><jupyter_text>We calculate the Root Mean Squared Error for our model by calculating the Mean Squared Error and then taking it's square root to calculate the RMSE value.<jupyter_code>rmse = np.sqrt(mean_squared_error(y_test, y_hat))
print(rmse)<jupyter_output>5.766369317319989<jupyter_text>Plotting the LSTAT versus price graphThe grey dots highlight the data fed in the testing set for variation of price with LSTAT. The red dots are what the model predicted.<jupyter_code>plt.scatter(x_test['LSTAT'], y_test, color='grey')
plt.scatter(x_test['LSTAT'], y_hat, c='red')
plt.xlabel("LSTAT", fontsize=18)
plt.ylabel("Price", fontsize=18)
plt.show()<jupyter_output><empty_output><jupyter_text>Plotting the RM versus price graphThe grey dots highlight the data fed in the testing set for variation of price with RM. The red dots are what the model predicted.<jupyter_code>plt.scatter(x_test['RM'], y_test, color='grey')
plt.scatter(x_test['RM'], y_hat, c='red')
plt.xlabel("RM", fontsize=18)
plt.ylabel("Price", fontsize=18)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08563782337198929, 'fraction_numerical': 0.01159678858162355, 'mean_word_length': 5.263687150837989, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 58, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '580282', 'license': 'MIT', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1650, 'n_words': 572, 'path': 'Boston Housing Price Prediction.ipynb', 'repo_name': 'dewanshrawat15/ML-basics', 'size': '5605'}