I have a table with columnd ID, Data, and Target. See an example of first three rows ID…
- User
I have a table with columnd ID, Data, and Target. See an example of first three rows ID|Data|Target 1|[5541,5541,6623,7451,2545,5541,5541,6623,7451,2545,6688,6669,6622,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,666251,5541,6623,7451]|[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 Target contains lists with 10 elements. Write a Python script to predict Target based on Data. 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)])
The goal is to maximize mapk. 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. Here is the example of first three rows
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. The order of MCC codes matter
- Assistant
Based on the given table, you have an ID column representing the identification number of the customer, a Data column containing lists of consecutive Merchant Category Codes (MCC) for purchases made, and a Target column containing lists of MCC for future 10 consecutive purchases.
To predict the Target based on the Data, you can use the provided apk and mapk functions. These functions calculate the average precision at k (apk) and the mean average precision at k (mapk).
Here's an example of how you can use these functions:
import numpy as np 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)]) # Example data actual = [[4125,5541,5541,4125,5541,5541,4125,5541,5541,1478], [6125,5541,5541,6125,5541,5541,4125,5541,5541,1478], [5541,5541,1478,6125,5541,5541,6125,5541,5541,4125]] predicted = [[5541,5541,6623,7451,2545,5541,5541,6623,7451,2545,6688,6669,6622,5541,6623,7451,2545,6688,6669,6622], [5541,5541,6623,7451,5541,6623,7451,2545,6688,6669,6662,5541,6623,7451], [6688,6669,6662,5541,5541,6623,7451,5541,6623,7451,2545,7711,7711,8985,8865]] # Calculate mapk k = 10 mapk_score = mapk(actual, predicted, k) print(f"MAPK score: {mapk_score}") - User
Data is actual and Target is expected!!!!! Where is your model to make predictions?
- Assistant
- User
Modify the code a bit to get predictions for initial Data and then compare them with Target using metrics mapk
- Assistant
- User
TypeError Traceback (most recent call last) Cell In[170], line 9 7 # Calculate mapk score for initial predictions 8 k = 10 ----> 9 mapk_score_initial = mapk(df_train['Target'], predicted_mcc_initial, k) 10 print(f"MAPK score for initial predictions: {mapk_score_initial}")
Cell In[2], line 20, in mapk(actual, predicted, k) 19 def mapk(actual, predicted, k=10): ---> 20 return np.mean([apk(a, p, k) for a, p in zip(actual, predicted)])
Cell In[2], line 20, in (.0) 19 def mapk(actual, predicted, k=10): ---> 20 return np.mean([apk(a, p, k) for a, p in zip(actual, predicted)])
Cell In[2], line 3, in apk(actual, predicted, k) 2 def apk(actual, predicted, k=10): ----> 3 if len(predicted) > k: 4 predicted = predicted[:k] 6 score = 0.0
TypeError: object of type 'int' has no len()
- Assistant
Apologies for the error. The error is occurring because the
predicted_mcc_initialis a list of lists, and each individual prediction should be a list or iterable rather than an integer.To fix the error, you can modify the code as follows:
# Convert predictions to MCC codes (assuming one-hot encoding) predicted_mcc_initial = [] for prediction in initial_predictions: mcc_codes = [np.argmax(p) for p in prediction] predicted_mcc_initial.append(mcc_codes) # Calculate mapk score for initial predictions k = 10 mapk_score_initial = mapk([t[:10] for t in Target], predicted_mcc_initial, k) # Considering only the first 10 elements of Target print(f"MAPK score for initial predictions: {mapk_score_initial}")In this updated code, the
predicted_mcc_initiallist is modified to ensure each prediction is a list of MCC codes. Additionally, theTargetlist is sliced to consider only the first 10 MCC codes for each target. Then, the mapk score is calculated by passing in the sliced Target and predicted MCC codes.Please note that in the
mapkfunction, I'm considering only the first 10 MCC codes of the target to match the provided parameter. Feel free to adjust the code based on your specific requirements. - User
ValueError Traceback (most recent call last) Cell In[172], line 9 7 # Calculate mapk score for initial predictions 8 k = 10 ----> 9 mapk_score_initial = mapk([t[:10] for t in df_train['Target']], predicted_mcc_initial, k) # Considering only the first 10 elements of Target 10 print(f"MAPK score for initial predictions: {mapk_score_initial}")
Cell In[2], line 20, in mapk(actual, predicted, k) 19 def mapk(actual, predicted, k=10): ---> 20 return np.mean([apk(a, p, k) for a, p in zip(actual, predicted)])
Cell In[2], line 20, in (.0) 19 def mapk(actual, predicted, k=10): ---> 20 return np.mean([apk(a, p, k) for a, p in zip(actual, predicted)])
Cell In[2], line 14, in apk(actual, predicted, k) 11 num_hits += 1.0 12 score += num_hits / (i+1.0) ---> 14 if not actual: 15 return 0.0 17 return score / min(len(actual), k)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
- Assistant
Apologies for the error once again. The error is occurring because when checking
if not actual, it encounters an array with multiple elements, which is ambiguous for checking truth value.To resolve this, you can modify the
mapkfunction to handle arrays of targets. Here's an updated version of themapkfunction that can handle arrays with more than one element:def mapk(actual, predicted, k=10): scores = [] for a, p in zip(actual, predicted): scores.append(apk(a, p, k)) return np.mean(scores)Replace the original
mapkfunction with this updated version in your code to fix the error. Now, when calculating the mapk score, you can use the updated version as follows:mapk_score_initial = mapk([t[:10] for t in df_train['Target']], predicted_mcc_initial, k) # Considering only the first 10 elements of Target print(f"MAPK score for initial predictions: {mapk_score_initial}")This should handle the ValueError and correctly calculate the mapk score for the initial predictions.
- User
Nothing changes. Can you change the approach?
- Assistant
Apologies for the confusion. Let's take a different approach to calculate the MAPK score for the initial
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.