Guest User

Untitled

a guest
Dec 10th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. CSR = CreateMatrix(receipts)
  2.  
  3. max_idx = CSR.data.argmax()
  4.  
  5. col_idx = CSR.indices[max_idx]
  6. col_code = idx2code[col_idx]
  7.  
  8. # Берем последнюю строку которая начинается в максимальном элементе так как до нее строки начинающийся в этом
  9. # элементе будут пустые
  10. row_idx = np.where(CSR.indptr == max_idx)[0][-1]
  11. row_code = idx2code[row_idx]
  12.  
  13. first_product_idx = np.where(receipts['StockCode'] == row_code)[0][0]
  14. second_product_idx = np.where(receipts['StockCode'] == col_code)[0][0]
  15.  
  16. print('Most popular pair is:')
  17. print(receipts['Description'][first_product_idx])
  18. print(receipts['Description'][second_product_idx])
  19.  
  20. bottle_idx = np.where(receipts['Description'] == 'KNITTED UNION FLAG HOT WATER BOTTLE')[0][0]
  21. bottle_stock_code = receipts['StockCode'][bottle_idx]
  22. bottle_row_idx = code2idx[bottle_stock_code]
  23.  
  24. # Индекс первого товара в строке с бутылками
  25. bottle_neighbors_start = CSR.indptr[bottle_row_idx]
  26.  
  27. # Индекс первого товара в строке следующей за строкой с бутылками
  28. bottle_neighbors_end = CSR.indptr[bottle_row_idx + 1]
  29.  
  30. # num_bottle_neighbors товаров которые покупают с бутылками
  31. num_bottle_neighbors = bottle_neighbors_end - bottle_neighbors_start
  32.  
  33. # Ищем товар который чаще всего покупали с бутылками
  34. max_idx = np.where(CSR.data == CSR.data[bottle_neighbors_start:bottle_neighbors_end:].max())[0][0]
  35.  
  36. product_col = CSR.indices[max_idx]
  37. product_code = idx2code[product_col]
  38. product_descr_idx = np.where(receipts['StockCode'] == product_code)[0][0]
  39.  
  40. print('\nMost often with bottles bought:')
  41. print(receipts['Description'][product_descr_idx])
Add Comment
Please, Sign In to add comment