Files

2.7 KiB

A4 - Data Analytics-1

Tested and working as intended.


Pre-requisites

  • Install required libraries: pandas, numpy, seaborn, matplotlib & scikit-learn
pip install pandas numpy seaborn matplotlib
pip install -U scikit-learn

Code blocks

  1. Import libraries:
import pandas as pd
import numpy as np
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 mean_squared_error, r2_score

Tip

Hit Tab key while typing library names (or anything else) to activate auto-complete in Jupyter notebook.

  1. Load the dataset from a CSV file into a pandas DataFrame:
df= pd.read_csv("Assignment-A3-BostonHousing.csv")
df.head() # Prints first 5 rows
  1. Printing information about the DataFrame:
print("Columns:\n", df.columns)
print("Info:\n", df.info())
print("Description:\n", df.describe())
  1. Check for missing values:
print(df.isnull().sum())
  1. Correlation matrix:
plt.figure(figsize=(12, 10))
sns.heatmap(df.corr(), annot=True, cmap='coolwarm')
plt.title("Correlation Matrix")
plt.show()
  1. Splitting training and testing data:
X = df.drop('medv', axis=1)  # Deleted/Dropped "medv" (median value) column from dataset
y = df['medv']               # Target (Median value of owner-occupied homes)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Split data into 80% training and 20% testing
# X is independent variable; y is dependent variable
  1. Linear regression and evaulation:
lr = LinearRegression() # Create linear regression model object "lr"
lr.fit(X_train, y_train) # Train linear regression model using "X_train" and "y_train"
y_pred = lr.predict(X_test) # Make prediction on test case (X_train); predicated value stored in variable (y_pred)

# Evaluation
print("Mean Squared Error (MSE):", mean_squared_error(y_test, y_pred))
print("R-squared (R²):", r2_score(y_test, y_pred))
  1. Plotting graph:
plt.figure(figsize=(6,6))
plt.scatter(y_test, y_pred, color='blue')
plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], color='red')
plt.xlabel('Actual Prices')
plt.ylabel('Predicted Prices')
plt.title('Actual vs Predicted Prices')
plt.grid(True)
plt.show()

References

  1. Dataset source