0
0
Data Analysis Pythondata~30 mins

Linear regression basics in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Linear regression basics
📖 Scenario: You are a data analyst at a small company. You want to understand how advertising spending affects sales. You have data for advertising spending and sales for 5 months.
🎯 Goal: Build a simple linear regression model to find the relationship between advertising spending and sales.
📋 What You'll Learn
Create a dictionary with advertising spending and sales data
Create a variable for the number of data points
Calculate the slope and intercept of the best fit line using the linear regression formula
Print the slope and intercept values
💡 Why This Matters
🌍 Real World
Linear regression is used in business to predict sales based on advertising budgets, helping companies plan their spending.
💼 Career
Data analysts and data scientists use linear regression to find relationships between variables and make predictions.
Progress0 / 4 steps
1
Create the data dictionary
Create a dictionary called data with two keys: 'advertising' and 'sales'. The value for 'advertising' should be the list [5, 10, 15, 20, 25]. The value for 'sales' should be the list [7, 12, 17, 22, 27].
Data Analysis Python
Hint

Use a dictionary with keys 'advertising' and 'sales'. Each key should have a list of numbers as its value.

2
Set the number of data points
Create a variable called n and set it to the length of the data['advertising'] list.
Data Analysis Python
Hint

Use the len() function on data['advertising'] to get the number of points.

3
Calculate slope and intercept
Calculate the slope m and intercept b of the best fit line using the formulas:
m = (n * sum(x*y) - sum(x) * sum(y)) / (n * sum(x*x) - (sum(x))**2)
b = (sum(y) - m * sum(x)) / n
Use x for data['advertising'] and y for data['sales']. Use sum() and list comprehensions to calculate sums.
Data Analysis Python
Hint

Use zip(x, y) to multiply pairs of x and y values. Calculate sums first, then apply the formulas.

4
Print the slope and intercept
Print the slope m and intercept b values using two separate print() statements. Print "Slope:" followed by m, and "Intercept:" followed by b.
Data Analysis Python
Hint

Use print("Slope:", m) and print("Intercept:", b) to show the results.