-
Notifications
You must be signed in to change notification settings - Fork 2
/
recommender.py
48 lines (36 loc) · 1.9 KB
/
recommender.py
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
from dataclasses import dataclass
import numpy as np
import pandas as pd
@dataclass
class Customer:
def __init__(self, funds: int = None, possible_discount: float = None, profession: str = None, power: int = None):
self.funds = funds
self.possible_discount = possible_discount
self.profession = profession
self.power = power
def recommend_products(customer: Customer, *args) -> list[pd.DataFrame]:
if not isinstance(customer, Customer):
raise ValueError("Input must be a Customer object")
def filter_products(products):
st_dev = np.std(products["Normalized"]) * customer.power
pred_products = products[products["Profession"].str.contains(customer.profession)]
pred_products = pred_products[(pred_products["Normalized"] > customer.funds - st_dev) & (pred_products["Normalized"] < customer.funds + st_dev)]
return pred_products
def calculate_discount(products):
prod_mean = np.mean(products["Price"])
products["Discounted"] = products["Price"]-prod_mean
for index, discounted_price in products["Discounted"].items():
# Calculate discounted price for the current product
new_discounted_price = products.at[index, "Price"] - max(discounted_price, 0) * customer.possible_discount
# Update the value in the DataFrame
products.at[index, "Discounted"] = new_discounted_price
products["Discounted"] = products["Discounted"].apply(lambda x: round(x, 2))
return products
"""pred_laptops = filter_products(laptops)
pred_keyboards = filter_products(keyboards)"""
predictions = [filter_products(arg) for arg in args]
predictions = [calculate_discount(p) for p in predictions]
"""pred_laptops = filter_products(laptops)
pred_keyboards = filter_products(keyboards)"""
#predictions = [filter_products(arg) for arg in args]
return predictions