column index out of range with a data frame

Viewed 283

I am currently training a neural network. I would like to split up my training and validation data with a 80:20 ratio. I would like to have the full purchases. Unfortunately, I get an IndexError: column index (12) out of range. How can I fix this? At this position the error occurs mat[purchaseid, itemid] = 1.0. So I always split after each purchase (a complete purchase = if I have all rows with the same purchaseid).

Dataframe:

d = {'purchaseid': [0, 0, 0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 9, 9, 9, 9],
     'itemid': [ 3, 8, 2, 10, 3, 10, 4, 12, 3, 12, 3, 4, 8, 6, 3, 0, 5, 12, 9, 9, 13, 1, 7, 11, 11]}
df = pd.DataFrame(data=d)
print(df.head(20))

Methods:

PERCENTAGE_SPLIT = 20
def splitter(df):
  df_ = pd.DataFrame()
  sum_purchase = df['purchaseid'].nunique()
  amount = round((sum_purchase / 100) * PERCENTAGE_SPLIT)
  
  random_list = random.sample(df['purchaseid'].unique().tolist(), amount)
  df_ = df.loc[df['purchaseid'].isin(random_list)]
  df_reduced = df.loc[~df['purchaseid'].isin(random_list)]
  return [df_reduced, df_]

def generate_matrix(dataframe, name):
  mat = sp.dok_matrix((dataframe.shape[0], len(dataframe['itemid'].unique())), dtype=np.float32)
  for purchaseid, itemid in zip(dataframe['purchaseid'], dataframe['itemid']):
      mat[purchaseid, itemid] = 1.0 # At this position is the error
  return mat

Call:

dfs = splitter(df)
df_tr = dfs[0].copy(deep=True)
df_val = dfs[1].copy(deep=True)

train_mat_ = generate_matrix(df_tr, 'train')
val_mat_ = generate_matrix(df_val, 'val')

Error:

IndexError: column index (12) out of range

Dataframe:

#df
        purchaseid  itemid
    0            0       3
    1            0       8
    2            0       2
    3            1      10
    4            2       3
    5            2      10
    6            3       4
    7            3      12
    8            3       3
    9            4      12
    10           4       3
    11           4       4
    12           5       8
    13           5       6
    14           5       3
    15           6       0
    16           6       5
    17           6      12
    18           7       9
    19           7       9



 # df_tr
        purchaseid  itemid
    0            0       3
    1            0       8
    2            0       2
    3            1      10
    4            2       3
    5            2      10
    6            3       4
    7            3      12
    8            3       3
    9            4      12
    10           4       3
    11           4       4
    12           5       8
    13           5       6
    14           5       3
    18           7       9
    19           7       9
    20           8      13

   # df_val
        purchaseid  itemid
    15           6       0
    16           6       5
    17           6      12
    21           9       1
    22           9       7
    23           9      11
    24           9      11
1 Answers

Try this instead. sp.dok_matrix requires dimensions of target matrix. I have assumed range of purchaseid to be within [ 0, max(purchaseid) ] and range of itemid to be within [ 0, max(itemid) ] looking at your data.

def generate_matrix(dataframe, name):
  mat = sp.dok_matrix((dataframe['purchaseid'].max() + 1, dataframe['itemid'].max() + 1), dtype=np.float32)
  for purchaseid, itemid in zip(dataframe['purchaseid'], dataframe['itemid']):
      mat[purchaseid, itemid] = 1.0 # At this position is the error
  return mat
Related