I have a table with columnd ID, Data, and Target. ID is identification number of…
- User
I have a table with columnd ID, Data, and Target. ID is identification number of customer. Data contains a list of Merchant Category Codes for purchases that have been made. Each customer has different number of purchases. Morevover, codes are given according to the sequence of purchases. Column Target contains the list of Merchant Category Codes for future 10 consecutive purchases. Write a Python script to predict Target based on Data. See a numeric example.
Id|Data|Target 0|4814,4814,6010,6011,4814,6011,6011,4814,6011,6011,4814,6011,4814,4814,6011,6011,4814,4814,6011,6011,6011,4814,4814,6011,6011,6011,6011,4814,4814,6011,6011,6011,6011,4814,6011,4814,4814,4814,4814,4814,6011,6011,4814|4814,4814,4814,4814,5411,4814,4814,4814,4814,4814 1|6011,6011,6011,6011,6011,6011,6011,4814,4814,4814,4814,4814,4814,6011,4829,4814,6011,4814,6011,4814,4814,6010,6011,4814,6010,4814,4814,4814,4814,4814,4814,4814,5541,6011,4814,4814,4814,4814,4814,4814,5732,6011,4814,4814,4814|4814,6011,4814,6011,4814,4814,6011,4814,6011,4814 2|8021,6011,6011,6010,4829,4814,6011,6011,6011,6011,5719,5331,4814,6010,4829,4829,6010,5411,5211,8999,4814,5814,5814,5814,5814,5814,5814,5331,5814,5331,6010,4814,5814,5814,5814,4829,6010,5814,5814,5641,5641,4511,5331|6011,6011,6010,4829,4829,6010,6011,6011,4814,6011 3|4814,6011,4814,4814,4814,6011,6011,5691,5691,5411,5411,5411,4814,5411,4814,4814,4814,4814,5411,5411,5814,5411,5411,5411,6011,6011,4814,4814,4814,6011,4814,4814,5411,5411,6011,4814,5411,4814,4814|6011,6011,6010,6011,6011,4814,4814,6011,4814,4814 4|4814,4814,4814,4814,4814,4814,5946,4814,4814,6011,4814,4814,6010,5411,5499,5411,8021,6011,5912,6011,4829,4814,5977,6011,6011,6011,5499,5411,5999,5411,5411,4814,4814,4814,4814,6011,6011,6011,5411,5691,4814,5912,4814,4814|5499,6011,4814,4829,5200,5411,5499,5912,5411,5912
Take into account that variable Data contains lists with different number of elements and variable Target contains lists with 10 elements.
use as a metric mapk which is calculated as follows:
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)])
- Assistant
- User
Perhaps, a recurrent neural network (RNN) with Long Short-Term Memory (LSTM) cells to handle sequences of different lengths in the Data column provide better results. For the Target column, treat each element as a separate regression target and train separate models for each target.
- Assistant