Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CSR = CreateMatrix(receipts)
- max_idx = CSR.data.argmax()
- col_idx = CSR.indices[max_idx]
- col_code = idx2code[col_idx]
- # Берем последнюю строку которая начинается в максимальном элементе так как до нее строки начинающийся в этом
- # элементе будут пустые
- row_idx = np.where(CSR.indptr == max_idx)[0][-1]
- row_code = idx2code[row_idx]
- first_product_idx = np.where(receipts['StockCode'] == row_code)[0][0]
- second_product_idx = np.where(receipts['StockCode'] == col_code)[0][0]
- print('Most popular pair is:')
- print(receipts['Description'][first_product_idx])
- print(receipts['Description'][second_product_idx])
- bottle_idx = np.where(receipts['Description'] == 'KNITTED UNION FLAG HOT WATER BOTTLE')[0][0]
- bottle_stock_code = receipts['StockCode'][bottle_idx]
- bottle_row_idx = code2idx[bottle_stock_code]
- # Индекс первого товара в строке с бутылками
- bottle_neighbors_start = CSR.indptr[bottle_row_idx]
- # Индекс первого товара в строке следующей за строкой с бутылками
- bottle_neighbors_end = CSR.indptr[bottle_row_idx + 1]
- # num_bottle_neighbors товаров которые покупают с бутылками
- num_bottle_neighbors = bottle_neighbors_end - bottle_neighbors_start
- # Ищем товар который чаще всего покупали с бутылками
- max_idx = np.where(CSR.data == CSR.data[bottle_neighbors_start:bottle_neighbors_end:].max())[0][0]
- product_col = CSR.indices[max_idx]
- product_code = idx2code[product_col]
- product_descr_idx = np.where(receipts['StockCode'] == product_code)[0][0]
- print('\nMost often with bottles bought:')
- print(receipts['Description'][product_descr_idx])
Add Comment
Please, Sign In to add comment