<jupyter_start><jupyter_text>Pricing<jupyter_code>purchased = df[df['event_type'] =='purchase'][['brand','product_id','event_time','promotion','discount','price','selling_price']]
purchased.head()
purchased['month'] = pd.DatetimeIndex(purchased.event_time).month_name(locale = 'English')
purchased.head()
grouped = purchased.groupby(['brand','product_id','month','discount'])
demand_df = grouped['selling_price'].count().reset_index()
price_df = grouped['selling_price'].sum().reset_index()
stock_df = demand_df.groupby(['brand','product_id',])['selling_price'].max().reset_index()
stock_df['selling_price'] = stock_df['selling_price'].apply(lambda x: x*2)
demand_df['month_disc'] = demand_df['month'].map(str) + '_' + demand_df['discount'].map(str)
price_df['month_disc'] = price_df['month'].map(str) + '_' + price_df['discount'].map(str)
column_names = ['month_disc']

mapper = {} 
for i, col_name in enumerate(column_names):
    for val in demand_df[col_name].unique():
        mapper[str(val) + str(i)] = len(mapper)
         
    
def one_hot(values):
    v = np.zeros( len(mapper) )
    for i, val in enumerate(values): 
        v[ mapper[str(val) + str(i)] ] = 1
    return v    
    
demand_df['one_hot'] = demand_df[column_names].values.tolist()
demand_df['one_hot'] = demand_df['one_hot'].map(one_hot)
column_names = ['month_disc']

mapper = {} 
for i, col_name in enumerate(column_names):
    for val in price_df[col_name].unique():
        mapper[str(val) + str(i)] = len(mapper)
         
    
def one_hot(values):
    v = np.zeros( len(mapper) )
    for i, val in enumerate(values): 
        v[ mapper[str(val) + str(i)] ] = 1
    return v    
    
price_df['one_hot'] = price_df[column_names].values.tolist()
price_df['one_hot'] = price_df['one_hot'].map(one_hot)
demand_df['one_hot'] = demand_df['one_hot']*demand_df['selling_price']
price_df['one_hot'] = price_df['one_hot']*price_df['selling_price']
demand_df.head()
price_df.head()
def count_by_md(df):
    counters = np.zeros(len(demand_df['month_disc'].unique()))
    for one_hot in df['one_hot'].values:
        month_dis_id = np.argmax(one_hot)
        counters[month_dis_id]  =  one_hot[month_dis_id]
    return counters
result_pricing = []

for prod in demand_df.product_id.unique():
    
    pid = prod
    brand = demand_df[demand_df['product_id'] == pid]['brand'].unique()[0]
    pid_demand_df = demand_df[demand_df['product_id'] == pid].reset_index(drop = True)
    D = count_by_md(pid_demand_df)
    pid_price_df = price_df[price_df['product_id']== pid].reset_index(drop = True)
    P = count_by_md(pid_price_df)
    C = stock_df[stock_df['product_id']==pid]['selling_price'].reset_index(drop = True)
    
    
    L = len(demand_df['month_disc'].unique())

    M = np.array([[
        1 if i >= 4*j and i < 4*(j+1) else 0
        for i in range(L)
    ] for j in range(5)])
    
    B = [1 for i in range(5)]

    # Second, we ensure that the sum of all demands is less than the availbale stock level,
    # that is z.Df <= C
    Df = np.array(D).reshape(1, L)
    
    try:

        res = linprog(-np.array(P).flatten(), 
                      A_eq=M, 
                      b_eq=B, 
                      A_ub=Df, 
                      b_ub=np.array([C]), 
                      bounds=(0, None))
    
        if(res.success):
            result = np.array(res.x).tolist()
            result.insert(0,pid)
            result.insert(0,brand)
            result.append(-res.fun)
        result_pricing.append(result)
    except:
        continue
md = demand_df['month_disc'].unique().tolist()
md.insert(0,'Producr_id')
md.insert(0,'Brand')
md.append('net_revenue')
pricing_df=pd.DataFrame(result_pricing,columns=md)
len(pricing_df)
pricing_df.to_csv('C:/Users/harini/Desktop/NEUCourses/ADM/Project/pricing_strategy_data.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16523325514725046, 'fraction_numerical': 0.004691164972634871, 'mean_word_length': 3.680487804878049, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '84111', 'license': 'MIT', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1389, 'n_words': 217, 'path': 'Final_Project_Marketing_Strategies/notebooks/Pricing.ipynb', 'repo_name': 'Harini-Grandhi/ADM', 'size': '3837'}