#C12929. Customer Transaction Prediction
Customer Transaction Prediction
Customer Transaction Prediction
In this problem, you are given a CSV file containing customer transaction data. The CSV includes a header and several rows, each containing the following fields: CustomerID, VisitDate, ItemsViewed, ItemsAddedToCart, and PurchaseMade. Your task is to preprocess the data and simulate a simple machine learning workflow.
First, split the data into a training set and a test set. The split is done by taking the first ⌊0.6 * (number of data rows)⌋ rows as the training set and the remaining rows as the test set. Next, compute the median of the ItemsViewed column in the training set. For an even number of observations, use the lower median value (i.e. the element at index (n/2 - 1) after sorting).
Then, for each row in the test set, predict PurchaseMade as follows:
[ \text{prediction} = \begin{cases} 1, & \text{if } ItemsViewed \geq m \ 0, & \text{if } ItemsViewed < m \end{cases} ]
where (m) is the median computed from the training set. Finally, calculate the accuracy on the test set as (\text{accuracy} = \frac{\text{number of correct predictions}}{\text{total test samples}}).
Your program should read the CSV data from standard input (stdin) and output the accuracy as a floating point number to standard output (stdout).
inputFormat
Input is given via standard input. The first line contains an integer n representing the total number of lines in the CSV file (including the header). The next n lines contain the CSV content. The first line is the header with the column names: CustomerID,VisitDate,ItemsViewed,ItemsAddedToCart,PurchaseMade. Each subsequent line represents one record, with fields separated by commas.
outputFormat
Output a single floating point value representing the accuracy (in the range [0, 1]) of the predictions on the test set. The accuracy is computed as the number of correct predictions divided by the total number of test samples.## sample
6
CustomerID,VisitDate,ItemsViewed,ItemsAddedToCart,PurchaseMade
1,2021-01-01,10,2,1
2,2021-01-02,0,0,0
3,2021-01-03,5,0,0
4,2021-01-04,15,1,1
5,2021-01-05,3,1,0
1.0