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...
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
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...
programming in python Design a program that initializes a list of 5 items to zero (use...
programming in python Design a program that initializes a list of 5 items to zero (use repetition operator). It then updates that list with a series of 5 random numbers. The program should find and display the following data: -The lowest number in the list - Average of the numbers stored in the list
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): A website requests an user to input his account password. Write a program to examize the validity of the password. The valid password must consists of: At least 1 letter in [a-z] At least 1 number in [0-9] At least a letter in [A-Z] (capital) At least one special letter in [$#@] At least...
Use the Python programming language to complete below (I need the New Answer for this, and...
Use the Python programming language to complete below (I need the New Answer for this, and please provide the screenshot of the source code. Thank you!): Write a function to seek for all even numbers and odd numbers in the middle of two number A and B. Print even and odd numbers in 1 and 2020 (including both these two numbers)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT