Linear Regression: Practice Problems
Problem-1: Consider a small sample of the dataset as shown below and perform linear regression analysis.
Solution:
1. Importing Libraries:
Import the necessary libraries. You will be using pandas for data manipulation and sklearn for building the
linear regression model.
2. Preparing the Data:
Load your data into a DataFrame and split it into features (X) and target variable (y).
3. Splitting the Data:
Split the data into training and testing sets to evaluate the model’s performance.
4. Training the Model:
Now, fit a simple linear regression model to the training data used.
5. Making Predictions:
Use the model to predict the test scores for the test data.
6. Visualizing the Results:
Visualize the training data along with the regression line.
7. Evaluating the Model:
Check the model’s performance using metrics such as Mean Absolute Error (MAE).
Problem-2: Consider the data set given below and predict the house prices based on their square footage.
Multiple Linear Regression Practice Problem
Problem-1: Consider the data shown below, which is commonly used in marketing analytics and predict the sales
based on advertisement across different media channels - TV, radio, and newspaper.
Solution:
1. Importing Libraries:
2. Preparing the Data:
Load the data into a DataFrame and separate the features and target variable.
3. Splitting the Data:
Split the data into training and testing sets.
4. Training the Model:
Fit a multiple linear regression model to the training data.
5. Making Predictions:
Use the model to predict sales for the test data.
6. Evaluating the Model:
Evaluate the model’s performance using Mean Absolute Error (MAE).
7. Interpreting the Results:
The coefficients of the model will tell us how much each advertising medium contributes to sales. We can
get the details using the function as follows:
*****