Repository navigation
Expand file tree
/
Copy pathMarketing Costs
More file actions
57 lines (41 loc) · 1.97 KB
/
Copy pathMarketing Costs
File metadata and controls
57 lines (41 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
from sklearn.linear_model import LinearRegression
def desired_marketing_expenditure(marketing_expenditure, units_sold, desired_units_sold):
# Convert the lists to numpy arrays
marketing_expenditure = np.array(marketing_expenditure).reshape(-1, 1)
units_sold = np.array(units_sold).reshape(-1, 1)
# Create a linear regression model
model = LinearRegression()
# Fit the model on the units_sold as a predictor of marketing_expenditure
model.fit(units_sold, marketing_expenditure)
# Predict the required marketing expenditure for the desired_units_sold
predicted_expenditure = model.predict(np.array([[desired_units_sold]]))
return predicted_expenditure[0][0]
# Test the function with the provided example values
corrected_predicted_expenditure = desired_marketing_expenditure(
[300000, 200000, 400000, 300000, 100000],
[60000, 50000, 90000, 80000, 30000],
60000
)
corrected_predicted_expenditure
def desired_marketing_expenditure(marketing_expenditure, units_sold, desired_units_sold):
# Convert the lists to numpy arrays
marketing_expenditure = np.array(marketing_expenditure).reshape(-1, 1)
units_sold = np.array(units_sold).reshape(-1, 1)
# Create and fit the linear regression model
model = LinearRegression()
model.fit(marketing_expenditure, units_sold)
# Calculate the coefficient and intercept
coef = model.coef_[0][0]
intercept = model.intercept_[0]
# Given the desired number of units sold, calculate the marketing expenditure
# using the formula: expenditure = (desired_units_sold - intercept) / coefficient
required_expenditure = (desired_units_sold - intercept) / coef
return required_expenditure
# Using the provided data to calculate the required marketing expenditure
required_marketing_expenditure = desired_marketing_expenditure(
[300000, 200000, 400000, 300000, 100000],
[60000, 50000, 90000, 80000, 30000],
60000
)
required_marketing_expenditure