In [1]:
# prompt: To teach linear regression to an absolute beginner using python can build a step by step process with example snippet using scikitlearn. Start with a simple easily relatable one independent variable and one dependant. Start with data preparation, dropping unwanted fields, filling missing, normalization, outlier cleaning etc. Start first with correlation graph using seaborn and panda dataframe stats, head, info etc. So that the student understands the data and correlation and what is being predicted. You also give me useful relatable data.csv with multiple features but for the first use we will use just one feature. After that move on to 3 or more features. Remember to explain and show the y intercept, coefficients and also test train split. Also use matplotlib to show the points and fitted line for each features. Make complete with error analysis and model evaluation. I will use Google colab notes for running this. Give me step by step headings subheadings explanation for each line.
# Import necessary libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import StandardScaler
In [2]:
# 1. Data Loading and Exploration
# Load the dataset (replace 'your_data.csv' with the actual file name)
# Example relatable data (replace with your own dataset or generate one)
data = {'advertising_expenditure': [10, 15, 20, 25, 30, 35, 40, 45, 50,55],
'sales': [25,30,38,40,50, 55, 60, 65,70, 75]}
df = pd.DataFrame(data)
# Display the first few rows of the dataframe
print("First few rows of data:\n", df.head())
# Get information about the dataset (data types, missing values, etc.)
print("\nDataset Info:\n", df.info())
# Descriptive statistics
print("\nDescriptive statistics:\n", df.describe())
First few rows of data:
advertising_expenditure sales
0 10 25
1 15 30
2 20 38
3 25 40
4 30 50
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 advertising_expenditure 10 non-null int64
1 sales 10 non-null int64
dtypes: int64(2)
memory usage: 288.0 bytes
Dataset Info:
None
Descriptive statistics:
advertising_expenditure sales
count 10.000000 10.00000
mean 32.500000 50.80000
std 15.138252 17.11919
min 10.000000 25.00000
25% 21.250000 38.50000
50% 32.500000 52.50000
75% 43.750000 63.75000
max 55.000000 75.00000
In [3]:
#2. Data Visualization and Correlation Analysis
# Create a scatter plot to visualize the relationship between 'advertising_expenditure' and 'sales'
plt.figure(figsize=(8, 6))
sns.scatterplot(x='advertising_expenditure', y='sales', data=df)
plt.title('Advertising Expenditure vs. Sales')
plt.xlabel('Advertising Expenditure')
plt.ylabel('Sales')
plt.show()
In [4]:
# Calculate the correlation between the variables
correlation = df['advertising_expenditure'].corr(df['sales'])
print(f"\nCorrelation between advertising expenditure and sales: {correlation}")
Correlation between advertising expenditure and sales: 0.9968309451267948
In [5]:
# 3. Data Preprocessing
# (Normalization and outlier cleaning might not be needed for this simple example but demonstrate how to do it)
# Normalize the 'advertising_expenditure' feature
scaler = StandardScaler()
df['advertising_expenditure_normalized'] = scaler.fit_transform(df[['advertising_expenditure']])
In [6]:
# Create a scatter plot to visualize the relationship between 'advertising_expenditure' and 'sales'
plt.figure(figsize=(8, 6))
sns.scatterplot(x='advertising_expenditure_normalized', y='sales', data=df)
plt.title('Advertising Expenditure Normalised vs. Sales')
plt.xlabel('Advertising Expenditure Normalised')
plt.ylabel('Sales')
plt.show()
In [7]:
# 4. Model Training (Single Feature)
# Prepare the features (X) and target variable (y)
X = df[['advertising_expenditure_normalized']] #Using only one feature
y = df['sales']
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 80% train 20% test
# Initialize and train the linear regression model
model = LinearRegression()
model.fit(X_train, y_train)
Out[7]:
LinearRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
LinearRegression()
In [8]:
# 5. Model Evaluation
# Make predictions on the test set
y_pred = model.predict(X_test)
# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"\nMean Squared Error: {mse}")
print(f"R-squared: {r2}")
print(f"\nIntercept: {model.intercept_}")
print(f"Coefficient: {model.coef_}")
Mean Squared Error: 1.1504904875148556 R-squared: 0.9971237737812129 Intercept: 51.0 Coefficient: [16.09467983]
In [9]:
# Visualize the fitted line
plt.figure(figsize=(8,6))
plt.scatter(X_test, y_test, color='blue', label = 'Actual Sales') # actual points
plt.plot(X_test, y_pred, color='red', linewidth=2, label='Predicted Sales') # predicted points
plt.xlabel('Advertising Expenditure (Normalized)')
plt.ylabel('Sales')
plt.title('Linear Regression Fit (Single Feature)')
plt.legend()
plt.show()
In [10]:
# 6. Model Training (Multiple Features)
# Example with multiple features
# (Assume there is another feature 'marketing_expenditure')
# Create dummy data. Replace with actual data loaded from file
df['marketing_expenditure'] = [5,8,10,12,15,18,20,22,25,28]
df['marketing_expenditure_normalized'] = scaler.fit_transform(df[['marketing_expenditure']])
X = df[['advertising_expenditure_normalized', 'marketing_expenditure_normalized']]
y = df['sales']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
# Evaluate model and visualize results (similar to above, but with multiple features)
mse = mean_squared_error(y_test,y_pred)
r2 = r2_score(y_test, y_pred)
print(f"\nMean Squared Error: {mse}")
print(f"R-squared: {r2}")
print(f"\nIntercept: {model.intercept_}")
print(f"Coefficient: {model.coef_}")
Mean Squared Error: 2.6300154320987645 R-squared: 0.9934249614197531 Intercept: 51.06944444444444 Coefficient: [5.98391942 9.99710606]