I have a table with columnd ID, Data, and Target. See an example ID Data Target 1…
- User
I have a table with columnd ID, Data, and Target. See an example ID|Data|Target 1|[5541,5541,6623,7451,2545,5541,5541,6623,7451,2545,6688,6669,6622]|[4125,5541,5541,4125,5541,5541,4125,5541,5541,1478] 2|[5541,5541,6623,7451,5541,6623,7451,2545,6688,6669,6662]|[6125,5541,5541,6125,5541,5541,4125,5541,5541,1478] 3|[6688,6669,6662,5541,5541,6623,7451,5541,6623,7451,2545,7711,7711,8985,8865]|[5541,5541,1478,6125,5541,5541,6125,5541,5541,4125]
ID is identification number of customer. Data contains a list of consecutive Merchant Category Codes for purchases that have been made. Merchant Category Codes always have four digits. Column Target contains the list of Merchant Category Codes for future 10 consecutive purchases. Note that Data contains lists with different number of elements and variable Target contains lists with 10 elements. Write a Python script to predict Target based on Data. Use sequential model. Use mapk as a measure
def apk(actual, predicted, k=10): if len(predicted) > k: predicted = predicted[:k]
score = 0.0 num_hits = 0.0
for i, p in enumerate(predicted): if p in actual and p not in predicted[:i]: num_hits += 1.0 score += num_hits / (i+1.0)
if not actual: return 0.0
return score / min(len(actual), k)
def mapk(actual, predicted, k=10): return np.mean([apk(a, p, k) for a, p in zip(actual, predicted)])
Note that the model should predict MCC codes, not probabilities! After training model, I need to load new Data to get Target values that are 10 MCC codes.
ID|Data 1|[5541,5541,6623,7451,2545,5541,5541,6623] 2|[5541,5541,6623,7451,2545,6688,6669,6622,7451,5541,6623,7451,2545,6688,6669,6662] 3|[6688,6669,5541,6623,7451,5541,6623,7451,2545,7711,8865]
If I unclear describe this, ask questions
- Assistant
What is the specific task you would like assistance with?
- User
Write a Python script to predict Target based on Data taking into account all information that I provided
- Assistant
To predict the Target based on the Data using a sequential model in Python, you can follow the steps below:
- Import the necessary libraries: