i got this error: A value is trying to be set on a copy of a slice from a DataFrame. Try…
- User
i got this error: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy attributes_filtered[0] = attributes_filtered[0].map(items_map)
this is the code import numpy as np import pandas as pd import os import argparse
Set up the argument parser
parser = argparse.ArgumentParser(description="Mapping and synchronization script.") parser.add_argument('--folder_name', type=str, help="Folder name.", required=True) args = parser.parse_args()
Change the working directory to the specified folder
os.chdir(args.folder_name)
Read train, validation, and test datasets
train = pd.read_csv('train.tsv', sep='\t', header=None) val = pd.read_csv('val.tsv', sep='\t', header=None) test = pd.read_csv('test.tsv', sep='\t', header=None)
Read the original item attributes dataset
attributes = pd.read_csv('item_attributes.tsv', sep='\t', header=None)
Concatenate train, validation, and test datasets to get all unique users and items
df = pd.concat([train, val, test], axis=0) users = df[0].unique() # Assuming the first column is user IDs items = df[1].unique() # Assuming the second column is item IDs (ASINs)
Create mappings from original IDs to new indices
users_map = {u: idx for idx, u in enumerate(users)} items_map = {i: idx for idx, i in enumerate(items)}
Save the mappings to TSV files
pd.DataFrame(list(users_map.items()), columns=['OriginalID', 'MappedID'])
.to_csv('users_map.tsv', sep='\t', index=False) pd.DataFrame(list(items_map.items()), columns=['OriginalID', 'MappedID'])
.to_csv('items_map.tsv', sep='\t', index=False)Apply the mappings to the train, validation, and test datasets
train[0] = train[0].map(users_map) train[1] = train[1].map(items_map) val[0] = val[0].map(users_map) val[1] = val[1].map(items_map) test[0] = test[0].map(users_map) test[1] = test[1].map(items_map)
Filter the attributes to include only items present in the datasets
attributes_filtered = attributes[attributes[0].isin(items)]
Apply the item mapping to the attributes dataset
attributes_filtered[0] = attributes_filtered[0].map(items_map)
Save the mapped and filtered datasets to new TSV files
train.to_csv('train_indexed.tsv', sep='\t', index=False, header=None) val.to_csv('val_indexed.tsv', sep='\t', index=False, header=None) test.to_csv('test_indexed.tsv', sep='\t', index=False, header=None) attributes_filtered.to_csv('item_attributes_filtered_indexed.tsv', sep='\t', index=False, header=None)
- Assistant