We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 2
1071124, 9:50 AM
Lab_02_Group_08
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.1inear_model import Linearegression
from sklearn.metrics import mean_absolute_error
# Step 1: Read the CSV file into a pandas DataFrame
df = pd.read_csv("Table 02 sales_data.csv")
# Step 2: Define features (X) and target (y)
X = df[["Marketing Spend’, ‘Store Size’, ‘Staff_count’]]
y = df["Sales']
# Step 3: Split the data into training and testing sets
X_train, X_test, y_train, y test = train test_split(x, y, test_siz
4, random_sta
4# Step 4: Initialize and train the Linear regression model
model = LinearRegression()
model.fit(X_train, y_train)
|v LinearRegression
[LinearRegression()
# Step 5: Predict sales on the test set
y_pred = model.predict(x test)
# Step 6: Evaluate the model using Mean Absolute Error (MAE)
mae = mean_absolute_error(y test, y_pred)
print(f"Mean Absolute Error: {mae)')
Mean Absolute Error: 2471.2396859690684
# Step 7: Display the model's coefficients and intercept
print(f'Coefficients: {model.coef_}")
print(f'intercept: {nodel.intercept_}")
Coefficients: [2.96635904e+00 1.49904832e+02 5.10803828e+03)
Intercept: -509.5101740694372
Discuss how well the model performs, based on the
MAE values
print(df['Sales'].min())
print (df[ ‘Sales’ ].max())
itThe Mean Absolute Error (MAE) of the model is Mean Absolute Error: 2471.24
‘#This metric measures the average absolute difference between the actual sales figu
‘This value is smaller than the actual sales value, as it is out of its range( smal
72352
292642
Ai: UsersfarchvOneDrive!Documonts/BIA SEM Machine Learning and Algorthms 1/PracticalsfLab2iab_02_Group_0S.himl we1071124, 9:50 AM Lab_02_Group_05
Reflect on how business students can apply Linear
Regression to predict sales or other financial
outcomes using available data.
‘#Business students can apply Linear Regression as a powerful tool to predict outcom
#1. Sales Forecasting: By analyzing historical sales data and identifying patterns,
#2. Financial Forecasting: Linear regression can be used to forecast future financi
#3, Market Analysis: Linear regression can assist in assessing how external factors
Can we reduce the number of variables using LR?
#Yes, it is possible to reduce the number of variables using Linear regression by a
#1. Feature Selection: By examining the coefficients of the regression model, Less
#2. Regularization Methods: Techniques Like Lasso regression (L1 regularization) ca
#3. Dimensionality Reduction: Approaches Like Principal Component Analysis (PCA) ca
Ai:1C:UsersfarchvOneDrive!Documonts/BIA SEM Machine Learning and Algorthms 1/PracticalsfLab2iab_02_Group_0S.himl
22.