import pandas as pd from datasets import Dataset from sklearn.modelselection import…
- User
import pandas as pd from datasets import Dataset from sklearn.model_selection import train_test_split
Load the tags from tags.txt
with open("tags.txt", "r") as f: tags = f.read().splitlines()
Create a mapping from tags to numerical labels (0 based or 1 based, adjust accordingly)
tag_to_label = {tag: i + 1 for i, tag in enumerate(tags)} # Start labels from 1
print(tag_to_label)
df = pd.read_csv('train.csv') def map_tag_to_label(text): return tag_to_label.get(text, None)
df['labels'] = df['label_text'].apply(map_tag_to_label) print(df.head(2)) df['labels'] = df['labels'] - 1 # Subtract 1 if your labels start from 1 print(df.head(2)) dataset = Dataset.from_pandas(df)
Split the dataset into training and test sets (if needed)
train_df, test_df = train_test_split(df, test_size=0.2, random_state=42)
Convert to Hugging Face Dataset
train_dataset = Dataset.from_pandas(train_df) test_dataset = Dataset.from_pandas(test_df)
save this df to csv file
- Assistant
To save the DataFrame
dfto a CSV file after processing, you can use thepandasmethodto_csv(). You can specify the file name and you can also choose to include the index or set options likeheader,sep, etc., depending on your requirements. The following code snippet shows how to save the DataFrame to a CSV file:# Save the processed DataFrame to a new CSV file df.to_csv('processed_train.csv', index=False)