How to fix value error broadcasting in python loop?

Question:

I am having trouble fixing my broadcasting error, the current error i am getting is ValueError: operands could not be broadcast together with shapes (10,) (5,)

preprocessing data:

data1 = pd.DataFrame({"cust_id": ['x111'], #customer data
                      "state": ['B'],
                      "amount": [1000],
                      "year":[3],
                      "group":[10],
                      "loan_rate":[0.12]})

data1['state'] = pd.Categorical(data1['state'], 
                                        categories=state_list, 
                                        ordered=True).codes

state_list = ['A','B','C','D','E'] #possible states


lookup1 = pd.DataFrame({'year': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       'lim %': [0.1, 0.1, 0.1, 0.1, 0.1,0.1, 0.1, 0.1, 0.1, 0.1]}).set_index(['year'])

matrix_data = np.arange(250).reshape(10,5,5) #3d matrix by state(A-E) and year(1-10)

results={}
for cust_id, state, amount, start, group, loan_rate in data1.itertuples(name=None, index=False):
    res = [amount * matrix_data[start-1, state, :]]
    for year in range(start+1, len(matrix_data)+1,):
        temp = np.array([lookup1.loc[year].iat[0]]*len(matrix_data))
        temp[3] = temp[4] = 1
        res.append(temp * np.array(res[-1])) #trying to edit this line
        res.append(res[-1] * loan_rate)
        res.append(res[-1]+ 60)
        res.append([res[-1] @ matrix_data[year-1]])
        results[cust_id] = res

expected output:

{'x111': [array([55000, 56000, 57000, 58000, 59000]),
  array([ 5500.,  5600.,  5700., 58000., 59000.]),...

related to this question How to use append with np.where in nested loop?

Asked By: user1000x

||

Answers:

See if below code snippet works for you, and make changes as per your convenience:

import pandas as pd
import numpy as np

state_list = ['A','B','C','D','E'] #possible states

data1 = pd.DataFrame({"cust_id": ['x111'], #customer data
                    "state": ['B'],
                    "amount": [1000],
                    "year":[3],
                    "group":[10],
                    "loan_rate":[0.12]})

data1['state'] = pd.Categorical(data1['state'], 
                                        categories=state_list, 
                                        ordered=True).codes




lookup1 = pd.DataFrame({'year': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                    'lim %': [0.1, 0.1, 0.1, 0.1, 0.1,0.1, 0.1, 0.1, 0.1, 0.1]}).set_index(['year'])

matrix_data = np.arange(250).reshape(10,5,5) #3d matrix by state(A-E) and year(1-10)

results={}
for cust_id, state, amount, start, group, loan_rate in data1.itertuples(name=None, index=False):
    print(cust_id, state, amount, start, group, loan_rate)
    res = [amount * matrix_data[start-1, state, :]]
    for year in range(start+1, len(matrix_data)+1,):
        temp = np.array([lookup1.loc[year].iat[0]]*len(matrix_data))
        temp[3] = temp[4] = 1
        res.append(temp[:5] * np.array(res[-1])) #trying to edit this line
        res.append(res[-1] * loan_rate)
        res.append(res[-1]+ 60)
        res.append([res[-1] @ matrix_data[year-1]])
        results[cust_id] = res

print(results)
Answered By: Piyush Sambhi
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.