<jupyter_start><jupyter_text>Problem Statement Build the linear regression model using scikit learn in boston data to predict 'Price' based on other dependent variable.  1. Loading the Libraries and Dataset<jupyter_code>import pandas as pd
import numpy as np
import sklearn
from sklearn.datasets import load_boston
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score

# Loading dataset
boston = load_boston()
bos = pd.DataFrame(boston.data)
bos.head()
# Description of Dataset
print(boston.DESCR)
# Replacing column number with feature names
names = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT']
bos.columns = names
bos.head()
# Adding target or dependent variable to the dataset
bos["Price"] = boston.target
bos.head()<jupyter_output><empty_output><jupyter_text>2. Exploratory Data Analysis<jupyter_code># Checking for null values
bos.isna().sum()
# Summary of Boston Dataframe
bos.info()
# Basic Statistical Analysis of Boston Dataframe
bos.describe()
# Distribution of target varaible:Price
sns.set(rc={'figure.figsize':(10,10)})
sns.distplot(bos['Price'], bins=100)
plt.show()
# Correlation among the features
plt.figure(figsize=(10,10))
sns.heatmap(bos[names].corr().round(2),annot=True)<jupyter_output><empty_output><jupyter_text>3. Machine Learning Model: Linear Regression<jupyter_code># Creating feature and target Dataset
y = bos["Price"]
X = bos.drop(["Price"],axis=1)
 
# Splitting into train and test Dataset
X_train,X_test,y_train,y_test=train_test_split(X ,y, test_size = 0.3, random_state = 42)
print(f'Shape of train feature:{X_train.shape}')
print(f'Shape of test feature :{X_test.shape}')
print(f'Shape of train target :{y_train.shape}')
print(f'Shape of test target  :{y_test.shape}')
# Instantiatiate the linear regression model
model = LinearRegression(normalize= True)
# Fit the model 
model.fit(X_train,y_train)
# Predicting on test dataset
y_pred = model.predict(X_test)
#performance metrics: R2 Score
score = r2_score(y_test,y_pred)
print(f'The R2 score for the linear regression model is: {score.round(2)}')


# Checking Normality of errors
diff = y_test-y_pred
sns.distplot(diff)
plt.title("Histogram of Residuals")
plt.xlabel("Residuals")
plt.ylabel("Frequency")
plt.show()
# Visualizing the differences between actual prices and predicted values
plt.scatter(y_test, y_pred)
plt.xlabel("Prices")
plt.ylabel("Predicted prices")
plt.title("Predicted Price Vs Actual Price")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11878557874762809, 'fraction_numerical': 0.009487666034155597, 'mean_word_length': 6.86865671641791, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '626531', 'license': 'MIT', 'n_tokens_mistral': 941, 'n_tokens_neox': 880, 'n_words': 251, 'path': '6. Linear Regression Assignment.ipynb', 'repo_name': 'deepchatterjeevns/Ineuron.ai', 'size': '2635'}