Question

In: Computer Science

Use Python programming to find most popular single products and co-purchased products from the large transaction...

Use Python programming to find most popular single products and co-purchased products from the large transaction data: retail.csv. Each row in the file is one purchase transaction from a customer, including a set of product ids separated by commas. The first column is transaction ID, column 2-3 are the products ID purchased in this transaction. It is worth mentioning that if the value in third column is zero, it means this customer only purchased one product (the one in second column).

Note:

• Co-purchased products is defined as a pair of products purchased in the same transaction. For example a row is: "2 24 35". Then 24 and 35 is a pair of copurchased products IDs,.

• To find co-purchased product in each transaction, you might use a nested loop.

• Write top 10 single products and top 10 co-purchased product pairs into a new file: output.txt

Solutions

Expert Solution

import csv
import operator
f = open('retail.csv')
csv_f = csv.reader(f)

row_data = []
co_purchased={}       #dict for storing co_purchased items
singly_purchased={}  #dict for storing singly purchased items
for row in csv_f:
  if row[1]!=0 and row[2]!=0: #if two object are in same transaction
    key= str(row[1])+ " " + str(row[2])
    if key not in co_purchased:
        co_purchased[key]=1
    else
        co_purchased[key]+=1
  elif row[1]!=0:       # if object 1 is only there
     key=str(row[1])
     if key not in singly_purchased:
        singly_purchased[key]=1
     else
        singly_purchased[key]+=1 
  elif row[2]!=0:       #if only obj 2 is there
     key=str(row[2])
     if key not in singly_purchased:
        singly_purchased[key]=1
     else
        singly_purchased[key]+=1 
       
#Now we need to sort the dictionaries according to frequency High to Low
co_purchased_sorted= dict( sorted(co_purchased.items(), key=operator.itemgetter(1),reverse=True))
top_10_co_purchased=[]
counter=0
for key in co_purchased_sorted:
    top_10_co_purchased.append(key+"\n")  #appending only keys that is item set into list
    counter+=1
    if counter==10:
       break
file1 = open("output.txt","w") #file1 represent the reads empty output file output.txt

file1.writelines(top_10_co_purchased) #writes top 10 co purchased items in it

singly_purchased_sorted= dict( sorted(singly_purchased.items(), key=operator.itemgetter(1),reverse=True)) 
top_10_singly_purchased=[]
counter=0
for key in singly_purchased_sorted:
    top_10_singly_purchased.append(key+"\n")
    counter+=1
    if counter==10:
       break
file1.writelines(top_10_co_purchased) #writes top 10 co purchased items in it


Related Solutions

Major Mills is a large manufacturer of breakfast cereal. One of its most popular products is...
Major Mills is a large manufacturer of breakfast cereal. One of its most popular products is Sugar-Bombs, which sells at wholesale for $55/case.   The cost to produce is $42.20 per case, as follows: Ingredients, $8.70; Packaging, $4.10; Direct Labor, $3.40; Overhead, $26 (20% variable). A large grocery retailer has approached Major Mills with a proposal to create a house-brand version of Sugar-Bombs. The retailer offers to purchase 10,000 cases per month of the house brand for one year, after which...
McElroy, Inc., produces a single model of a popular cell phone in large quantities. A single...
McElroy, Inc., produces a single model of a popular cell phone in large quantities. A single cell phone moves through two departments, assembly and testing. The manufacturing costs in the assembly department during March follow: Direct materials $ 140,400 Conversion costs 117,500 $ 257,900 The assembly department has no beginning Work-in-Process Inventory. During the month, it started 26,000 cell phones, but only 21,000 were fully completed and transferred to the testing department. All parts had been made and placed in...
McElroy, Inc., produces a single model of a popular cell phone in large quantities. A single...
McElroy, Inc., produces a single model of a popular cell phone in large quantities. A single cell phone moves through two departments, assembly and testing. The manufacturing costs in the assembly department during March follow: Direct materials $ 148,800 Conversion costs 114,400 $ 263,200 The assembly department has no beginning Work-in-Process Inventory. During the month, it started 24,000 cell phones, but only 20,000 were fully completed and transferred to the testing department. All parts had been made and placed in...
HI, ANY PROGRAMMING LANGUAGE WOULD WORK, FROM PYTHON TO C++, MOST PREFERABLY OR ELSE ANY FOR...
HI, ANY PROGRAMMING LANGUAGE WOULD WORK, FROM PYTHON TO C++, MOST PREFERABLY OR ELSE ANY FOR YOUR CONVENIENCE. Let f(x) = x6 + 7x5− 15x4− 70x3 + 75x2 + 175x − 125. a.) Write a program that carries out the Secant Method on f(x). You do not need to make your program take arbitrary input (i.e. you can tailor it to this specific f(x)). Your program should take as input the appropriate number of initial guesses, an interval [a, b]...
Java programming One of the most popular games of chance is a dice game known as...
Java programming One of the most popular games of chance is a dice game known as “craps,” which is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2,3,4,5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on...
Java, Python, and C++ are three of the most useful programming languages to learn. Compare the...
Java, Python, and C++ are three of the most useful programming languages to learn. Compare the functionalities of all three programming languages. Why would you choose one language over another? Provide code examples demonstrating their usefulness in a real-world scenario.
Starting out with Python 4th edition Chapter 2 Programming Exercise 12 Stock Transaction Program Not sure...
Starting out with Python 4th edition Chapter 2 Programming Exercise 12 Stock Transaction Program Not sure how to do this
This is for Python programming, and I am trying to use 2 for loops to ask...
This is for Python programming, and I am trying to use 2 for loops to ask a slaesperson how many of 5 different items they sold, then the program is to calculate the total dollar amount sold. Formatting and all that aside, I am having an issue with the loops not stepping through the 2 lists at the same time. The first loop is taking all 5 entered quantities times the price of the first item, and then all 5...
Python please A string is one of most powerful data types in programming. A string object...
Python please A string is one of most powerful data types in programming. A string object is a sequence of characters and because it is a sequence, it is indexable, using index numbers starting with 0. Similar to a list object, a string object allows for the use of negative index with -1 representing the index of the last character in the sequence. Accessing a string object with an invalid index will result in IndexError exception. In Python a string...
Say No to the​ Brush, LLC. is a manufacturer of premium hairstyling products.​ It’s most popular​...
Say No to the​ Brush, LLC. is a manufacturer of premium hairstyling products.​ It’s most popular​ product, It’s​ Real, is sold in 10 ounce bottles. One bottle of​ It’s Real is expected to use 8 ounces of direct material at a standard direct material cost of​ $1.75 per ounce. During the​ month, the company purchased​ 320,000 ounces of direct material at a total cost of​ $608,000. However, only​ 285,000 ounces of direct material were used in the monthly production of​...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT