generated from amosproj/amos202Xss0Y-projname
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a small skeleton on how the EVP might be interacted with. Add…
…ed some dummy data for example purposes. #22 Signed-off-by: Felix Zailskas <[email protected]>
- Loading branch information
1 parent
2a82242
commit 33448e4
Showing
9 changed files
with
330 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# SPDX-License-Identifier: MIT | ||
# SPDX-FileCopyrightText: 2023 Felix Zailskas <[email protected]> | ||
|
||
from .database_dummy import DatabaseDummy | ||
|
||
_database = None | ||
|
||
|
||
def get_database() -> DatabaseDummy: | ||
global _database | ||
if _database is None: | ||
_database = DatabaseDummy() | ||
return _database |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# SPDX-License-Identifier: MIT | ||
# SPDX-FileCopyrightText: 2023 Felix Zailskas <[email protected]> | ||
|
||
import json | ||
|
||
|
||
class DatabaseDummy: | ||
def __init__(self) -> None: | ||
with open("src/database/dummy_leads.json") as f: | ||
json_data = json.load(f)["training_leads"] | ||
self.data = {d["lead_id"]: d for d in json_data} | ||
|
||
def get_entry_by_id(self, id_: int) -> dict: | ||
return self.data[id_] | ||
|
||
def get_all_entries(self): | ||
return self.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"training_leads": [ | ||
{ | ||
"lead_id": 0, | ||
"company_name": "test_company", | ||
"first_name": "test_first", | ||
"last_name": "test_last", | ||
"country_code": "DE", | ||
"phone_number": 176123123, | ||
"email_address": "[email protected]", | ||
"customer_probability": 0.1, | ||
"life_time_value": 400000 | ||
}, | ||
{ | ||
"lead_id": 1, | ||
"company_name": "test_company", | ||
"first_name": "test_first", | ||
"last_name": "test_last", | ||
"country_code": "DE", | ||
"phone_number": 176123123, | ||
"email_address": "[email protected]", | ||
"customer_probability": 0.9, | ||
"life_time_value": 1000 | ||
}, | ||
{ | ||
"lead_id": 2, | ||
"company_name": "test_company", | ||
"first_name": "test_first", | ||
"last_name": "test_last", | ||
"country_code": "DE", | ||
"phone_number": 176123123, | ||
"email_address": "[email protected]", | ||
"customer_probability": 0.7, | ||
"life_time_value": 3500 | ||
}, | ||
{ | ||
"lead_id": 3, | ||
"company_name": "test_company", | ||
"first_name": "test_first", | ||
"last_name": "test_last", | ||
"country_code": "DE", | ||
"phone_number": 176123123, | ||
"email_address": "[email protected]", | ||
"customer_probability": 0.4, | ||
"life_time_value": 10000 | ||
}, | ||
{ | ||
"lead_id": 4, | ||
"company_name": "test_company", | ||
"first_name": "test_first", | ||
"last_name": "test_last", | ||
"country_code": "DE", | ||
"phone_number": 176123123, | ||
"email_address": "[email protected]", | ||
"customer_probability": 0.32, | ||
"life_time_value": 20000 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SPDX-License-Identifier: CC-BY-4.0 | ||
SPDX-FileCopyrightText: 2023 Felix Zailskas <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# SPDX-License-Identifier: MIT | ||
# SPDX-FileCopyrightText: 2023 Felix Zailskas <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# SPDX-License-Identifier: MIT | ||
# SPDX-FileCopyrightText: 2023 Felix Zailskas <[email protected]> | ||
|
||
import numpy as np | ||
from sklearn.linear_model import LinearRegression | ||
|
||
from database import get_database | ||
|
||
|
||
class LeadValue: | ||
def __init__( | ||
self, lifetime_value: float = 0, customer_probability: float = 0 | ||
) -> None: | ||
assert ( | ||
0.0 <= customer_probability <= 1.0 | ||
), "Probability of becoming a customer must be between 0.0 and 1.0" | ||
self.life_time_value = lifetime_value | ||
self.customer_probability = customer_probability | ||
|
||
def get_lead_value(self) -> float: | ||
return self.life_time_value * self.customer_probability | ||
|
||
|
||
class EstimatedValuePredictor: | ||
def __init__(self) -> None: | ||
self.probability_predictor = LinearRegression() | ||
self.life_time_value_predictor = LinearRegression() | ||
|
||
data = get_database().get_all_entries() | ||
X = np.random.random((len(data), len(data))) | ||
y_probability = np.array( | ||
[item["customer_probability"] for item in data.values()] | ||
) | ||
y_value = np.array([item["customer_probability"] for item in data.values()]) | ||
|
||
self.probability_predictor.fit(X, y_probability) | ||
self.life_time_value_predictor.fit(X, y_value) | ||
|
||
def estimate_value(self, lead_id) -> LeadValue: | ||
# make call to data base to retrieve relevant fields for this lead | ||
lead_data = get_database().get_entry_by_id(lead_id) | ||
|
||
# preprocess lead_data to get feature vector for our ML model | ||
feature_vector = np.random.random((1, 5)) | ||
|
||
# use the models to predict required values | ||
lead_value_pred = self.life_time_value_predictor.predict(feature_vector) | ||
# manually applying sigmoid to ensure value in range 0, 1 | ||
cust_prob_pred = 1 / ( | ||
1 + np.exp(-self.probability_predictor.predict(feature_vector)) | ||
) | ||
|
||
return LeadValue(lead_value_pred, cust_prob_pred) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# SPDX-License-Identifier: MIT | ||
# SPDX-FileCopyrightText: 2023 Felix Zailskas <[email protected]> | ||
|
||
from database import get_database | ||
from evp.evp import EstimatedValuePredictor | ||
|
||
lead_id = 0 | ||
|
||
lead_data = get_database().get_entry_by_id(lead_id) | ||
|
||
evp = EstimatedValuePredictor() | ||
lead_value = evp.estimate_value(lead_id) | ||
|
||
print( | ||
f""" | ||
Dummy prediction for {lead_id=}: | ||
Data: | ||
{lead_data} | ||
This lead has a predicted probability of {lead_value.customer_probability} to become a customer. | ||
This lead has a predicted life time value of {lead_value.life_time_value}. | ||
This results in a total lead value of {lead_value.get_lead_value()}. | ||
""" | ||
) |