i see something strange. the final output file, is a csv with more than 14k columns. i expect, from the code, that it has 250 columns, since i reduced the num_clusters to 50. i want that each keyword category has at max 50k unique words
import csv
import re
import os
import nltk
import pandas as pd
from collections import defaultdict, Counter
from nltk.stem import WordNetLemmatizer
from gensim.models import KeyedVectors, FastText
from sklearn.cluster import KMeans, AgglomerativeClustering
import numpy as np
from sklearn.preprocessing import OneHotEncoder
------------------------------#
Setup
------------------------------#
Initialize the lemmatizer
lemmatizer = WordNetLemmatizer()
Import for POS tagging
from nltk import pos_tag, word_tokenize
from nltk.corpus import wordnet as wn
from nltk.corpus import stopwords
Function to load the appropriate word embedding model
def load_model(model_type, model_path):
if model_type == 'word2vec':
print("Loading Word2Vec model. This may take a few minutes...")
model = KeyedVectors.load_word2vec_format(model_path, binary=True)
print("Word2Vec model loaded successfully.")
elif model_type == 'glove':
print("Loading GloVe model. This may take a few minutes...")
model = {}
with open(model_path, 'r', encoding='utf-8') as f:
for line in f:
values = line.split()
word = values[0]
vector = np.asarray(values[1:], dtype='float32')
model[word] = vector
print("GloVe model loaded successfully.")
elif model_type == 'fasttext':
print("Loading FastText model. This may take a few minutes...")
model = FastText.load_fasttext_format(model_path)
print("FastText model loaded successfully.")
else:
raise ValueError(f"Unsupported model type: {model_type}")
return model
Function to get the vector for a word/phrase
def get_word_vector(word, model, model_type):
if model_type in ['word2vec', 'fasttext']:
if word in model:
return model[word]
elif model_type == 'glove':
if word in model:
return model[word]
# Handle multi-word phrases by averaging their word vectors
words = word.split()
vectors = []
for w in words:
if model_type in ['word2vec', 'fasttext'] and w in model:
vectors.append(model[w])
elif model_type == 'glove' and w in model:
vectors.append(model[w])
if vectors:
return np.mean(vectors, axis=0)
else:
return None
Function to process the CSV file and lemmatize keywords per row, and output lemmatized counts
def lemmatize_keywords(input_file, lemmatized_counts_output_file):
# Read the CSV file using pandas
df = pd.read_csv(input_file)
# Ensure 'Image Name' and 'Keywords' columns exist
if 'Image Name' not in df.columns or 'Keywords' not in df.columns:
print("Error: 'Image Name' and 'Keywords' columns not found in input CSV.")
return None, None
per_row_data = []
keyword_counts = Counter()
# Iterate through each row to extract and lemmatize keywords
for index, row in df.iterrows():
image_name = row.get('Image Name', '')
keywords_str = row.get('Keywords', '')
if pd.isna(keywords_str):
keywords_str = ''
# Split keywords by comma and strip whitespace
extracted_keywords = [kw.strip() for kw in keywords_str.split(',')]
# Lemmatize keywords
lemmatized_keywords = [lemmatize_keyword(kw) for kw in extracted_keywords if kw]
# Update global counts
keyword_counts.update(lemmatized_keywords)
# Store per-row data
per_row_data.append({
'Image Name': image_name,
'Keywords': ', '.join(extracted_keywords),
'Lemmatized Keywords': ', '.join(lemmatized_keywords)
})
# Create a DataFrame from per_row_data
per_row_df = pd.DataFrame(per_row_data)
# Write the lemmatized keyword counts to CSV
lemmatized_counts = [{'Lemmatized Keyword': kw, 'Count': count}
for kw, count in keyword_counts.items()]
lemmatized_counts_df = pd.DataFrame(lemmatized_counts)
# Sort by Count in descending order
lemmatized_counts_df = lemmatized_counts_df.sort_values(by='Count', ascending=False)
# Write to CSV
lemmatized_counts_df.to_csv(lemmatized_counts_output_file, index=False)
print(f"Lemmatized keyword counts saved to '{lemmatized_counts_output_file}'.")
return per_row_df, keyword_counts
Function to cluster infrequent keywords (count <= threshold)
def cluster_infrequent_keywords(keywords, keyword_counts, model, model_type, num_clusters=5, distance_threshold=None):
vectors = []
valid_keywords = []
for keyword in keywords:
vector = get_word_vector(keyword, model, model_type)
if vector is not None:
vectors.append(vector)
valid_keywords.append(keyword)
else:
print(f"Vector not found for keyword: '{keyword}'")
if not vectors:
print("No valid vectors found for clustering.")
return {}
vectors = np.array(vectors)
# Perform Hierarchical Clustering with distance threshold
if distance_threshold is not None:
print(f"Performing Agglomerative Clustering with distance_threshold={distance_threshold}")
clustering_model = AgglomerativeClustering(n_clusters=None, distance_threshold=distance_threshold)
else:
print(f"Performing Agglomerative Clustering with n_clusters={num_clusters}")
clustering_model = AgglomerativeClustering(n_clusters=num_clusters)
labels = clustering_model.fit_predict(vectors)
# Organize keywords by clusters
clusters = defaultdict(list)
for label, keyword in zip(labels, valid_keywords):
clusters[label].append(keyword)
# Select the most frequent keyword in each cluster as the representative
representative_keywords = {}
for label, words in clusters.items():
most_frequent = max(words, key=lambda w: keyword_counts[w])
representative_keywords[label] = most_frequent
# Create a mapping from keyword to representative
keyword_to_representative = {}
for label, words in clusters.items():
rep = representative_keywords[label]
for word in words:
keyword_to_representative[word] = rep
return keyword_to_representative
Function to perform clustering with frequency threshold
def perform_clustering_with_threshold(keyword_counts, model, model_type, threshold=5, num_clusters=5, distance_threshold=None):
# Separate frequent and infrequent keywords
frequent_keywords = [kw for kw, count in keyword_counts.items() if count > threshold]
infrequent_keywords = [kw for kw, count in keyword_counts.items() if count <= threshold]
print(f"Number of frequent keywords (> {threshold}): {len(frequent_keywords)}")
print(f"Number of infrequent keywords (<= {threshold}): {len(infrequent_keywords)}")
# Keep frequent keywords as-is
clustered = {kw: kw for kw in frequent_keywords}
# Cluster infrequent keywords and map them to their representatives
if infrequent_keywords:
mapping = cluster_infrequent_keywords(
infrequent_keywords, keyword_counts, model, model_type,
num_clusters=num_clusters, distance_threshold=distance_threshold
)
clustered.update(mapping)
print(f"Number of cluster representatives: {len(set(mapping.values()))}")
else:
print("No infrequent keywords to cluster.")
return clustered
Function to generate clustered keywords per row and categorize them based on position
for idx, row in per_row_df.iterrows():
clustered_keywords = row['Clustered Keywords'].split(', ')
if len(clustered_keywords) > 0:
category_keyword_counts['Object'][clustered_keywords[0]] +=1
if len(clustered_keywords) > 1:
category_keyword_counts['Adjective'][clustered_keywords[1]] +=1
if len(clustered_keywords) > 2:
category_keyword_counts['Activity'][clustered_keywords[2]] +=1
if len(clustered_keywords) > 3:
category_keyword_counts['Place'][clustered_keywords[3]] +=1
if len(clustered_keywords) > 4:
category_keyword_counts['Feeling'][clustered_keywords[4]] +=1
# Limit to top 100 keywords per category
for category in category_keyword_counts:
category_keyword_counts[category] = Counter(dict(category_keyword_counts[category].most_common(50)))
return category_keyword_counts
import pandas as pd
import numpy as np
from sklearn.preprocessing import OneHotEncoder
# Read only the necessary columns to improve efficiency
columns_to_encode=['Image Name', 'Object', 'Adjective', 'Activity', 'Place', 'Feeling']
columns_to_read = [id_column] + columns_to_encode
df = pd.read_csv(input_csv_path, usecols=columns_to_read)
# Initialize OneHotEncoder with dtype set to integer to output int values
enc = OneHotEncoder(handle_unknown='ignore', dtype=np.int8)
# Fit and transform the categorical columns
emb = enc.fit_transform(df[columns_to_encode])
# Retrieve the names of the one-hot encoded features
feature_names = enc.get_feature_names_out(columns_to_encode)
# Create a DataFrame with the encoded features and ensure they are integers
emb_df = pd.DataFrame(emb.toarray(), columns=feature_names, dtype=np.int8)
# Concatenate the id_column with the one-hot encoded features
result_df = pd.concat([df[[id_column]], emb_df], axis=1)
# Remove specified extension from id_column
if remove_extension:
result_df[id_column] = result_df[id_column].str.replace(remove_extension, '', regex=False)
# Save the final DataFrame to a TSV file without headers and index
result_df.to_csv(output_tsv_path, index=index, header=header, sep=sep)
------------------------------#
Main
------------------------------#
def main():
# File paths (replace these with your actual file paths)
original_input_file = '/home/danilo/Projects/IR_MD/cleaned_/home/danilo/Projects/IR_MD/cleaned_/home/danilo/Projects/IR_MD/TESTING/cleaned_graffe_better_prompt_KeyExt_Qwen2VL7B_20240912-131955_.csv'
lemmatized_counts_output_file = '/home/danilo/Projects/IR_MD/TESTING/keywords_lemmatized_counts.csv'
# Clustering parameters
model_type = 'glove' # Choose from 'word2vec', 'glove', 'fasttext'
model_path = '/home/danilo/Projects/IR_MD/models/glove.6B.300d.txt' # Path to the model file
frequency_threshold = 5 # Keywords with count >5 are kept as-is
num_clusters = 50 # Number of clusters for infrequent keywords (used if distance_threshold is None)
distance_threshold = None # Set this to a float value to use hierarchical clustering with a distance threshold
remove_duplicates = True # Set to True to remove duplicates in clustered keywords per row
# Step 1: Lemmatization and saving lemmatized counts
per_row_df, keyword_counts = lemmatize_keywords(original_input_file, lemmatized_counts_output_file)
if per_row_df is None:
return # Exit if there was an error
# Step 2: Load the appropriate model
model = load_model(model_type, model_path)
# Step 3: Perform clustering on infrequent keywords
clustered_keyword_mapping = perform_clustering_with_threshold(
keyword_counts, model, model_type,
threshold=frequency_threshold, num_clusters=num_clusters,
distance_threshold=distance_threshold
)
# Step 4: Generate Clustered Keywords per row and categorize
per_row_df = generate_clustered_keywords_and_categories(per_row_df, clustered_keyword_mapping, remove_duplicates=remove_duplicates)
# Step 5: Categorize and count clustered keywords
category_keyword_counts = categorize_and_count_clustered_keywords(per_row_df)
# Print counts for each category
for category, counts in category_keyword_counts.items():
print(f"\nCategory: {category}")
for keyword, count in counts.items():
print(f"{keyword}: {count}")
# Optionally, write category counts to CSV files
for category, counts in category_keyword_counts.items():
category_counts_df = pd.DataFrame({'Keyword': list(counts.keys()), 'Count': list(counts.values())})
category_counts_df = category_counts_df.sort_values(by='Count', ascending=False)
category_counts_output_file = f'/home/danilo/Projects/IR_MD/TESTING/{category}keywords_counts.csv'
category_counts_df.to_csv(category_counts_output_file, index=False)
print(f"\n{category} keyword counts saved to '{category_counts_output_file}'.")
# Step 6: Construct the output file name dynamically based on parameters
# Replace dots in distance_threshold with underscores for file naming
if distance_threshold is not None:
distance_threshold_str = str(distance_threshold).replace('.', '')
clustering_info = f'DistThresh{distance_threshold_str}'
else:
clustering_info = f'{num_clusters}Clusters'
dup_info = 'NoDup' if remove_duplicates else 'WithDup'
# Replace invalid characters in model_type for file naming
#model_type_safe = model_type.replace('/', '').replace("\", "")
output_file = f'/home/danilo/Projects/IR_MD/TESTING/{frequency_threshold}Freq{clustering_info}{dup_info}_keywords_clustered.csv'
# Step 7: Write the output CSV
per_row_df.to_csv(output_file, index=False)
print(f"\nProcess completed. Output saved to '{output_file}'.")
print(f"Lemmatized counts saved to '{lemmatized_counts_output_file}'.")
# Step 8: One-hot encode the categorical features
one_hot_encoded_file = f'/home/danilo/Projects/IR_MD/TESTING/{frequency_threshold}Freq{clustering_info}{dup_info}_keywords_clustered_onehot.csv'
encode_categorical_features(output_file, one_hot_encoded_file, id_column='Image Name', remove_extension='.jpg', sep=',', header=False, index=False)
print(f"\nOne-hot encoded features saved to '{one_hot_encoded_file}'.")
if name == "main":
main()
It seems like the unexpected number of columns in your output CSV is due to the way the one-hot encoding is processing the categorical features. Despite clustering infrequent keywords and limiting counts per category to the top 50 keywords, the OneHotEncoder in your encode_categorical_features function is encoding all unique keywords present in each category. This includes keywords beyond your top 50, leading to a large number of columns (over 14,000) in the final output.
Why is this happening?
In your current workflow:
Clustering and Limiting Counts:
You cluster infrequent keywords and limit the counts per category to the top 50 keywords when generating summary counts.
This is done in the categorize_and_count_clustered_keywords function.
However, this limitation is not applied to the actual data in per_row_df, which still contains all clustered keywords across all categories.
One-Hot Encoding:
When you perform one-hot encoding in encode_categorical_features, the OneHotEncoder processes all unique keyword values in each category from per_row_df.
Since per_row_df was not filtered to contain only the top 50 keywords per category, the encoder generates a separate column for every unique keyword in each category.
This results in a much larger number of columns than expected.
How to Fix It?
To ensure that each keyword category has at most 50 unique words in your final output, you need to filter per_row_df to only include the top 50 keywords per category before one-hot encoding. Here's how you can modify your code to achieve this:
Filter per_row_df to Keep Only Top 50 Keywords per Category:
After you have the top 50 keywords per category from your counts, you can map any keywords not in the top 50 to a special category, such as 'Other'.
Modify the generate_clustered_keywords_and_categories function to include this mapping.
def generate_clustered_keywords_and_categories(per_row_df, clustered_keyword_mapping, category_keyword_counts, remove_duplicates=False):
top_keywords_per_category = {category: set(counts.keys()) for category, counts in category_keyword_counts.items()}
# Initialize lists for categories
categories = ['Object', 'Adjective', 'Activity', 'Place', 'Feeling']
category_keywords_list = {cat: [] for cat in categories}
for idx, row in per_row_df.iterrows():
lemmatized_keywords = row['Lemmatized Keywords'].split(', ')
clustered_keywords = [clustered_keyword_mapping.get(kw, kw) for kw in lemmatized_keywords]
if remove_duplicates:
# Remove duplicates while preserving order
clustered_keywords = list(dict.fromkeys(clustered_keywords))
# Assign keywords to categories based on positions
for i, category in enumerate(categories):
if i < len(clustered_keywords):
keyword = clustered_keywords[i]
# Map to 'Other' if not in top keywords
if keyword not in top_keywords_per_category[category]:
keyword = 'Other'
category_keywords_list[category].append(keyword)
else:
category_keywords_list[category].append('') # or 'Other'
# Add the category columns to per_row_df
for category in categories:
per_row_df[category] = category_keywords_list[category]
return per_row_df
In this modification:
top_keywords_per_category is a dictionary that holds the top 50 keywords for each category.
When assigning keywords to categories, we check if the keyword is in the top 50. If not, we map it to 'Other'.
This ensures that each category in per_row_df contains only the top 50 keywords or 'Other'.
Adjust the Main Function to Pass category_keyword_counts:
You'll need to pass category_keyword_counts to generate_clustered_keywords_and_categories:
# After categorizing and counting clustered keywords
category_keyword_counts = categorize_and_count_clustered_keywords(per_row_df)
# Generate clustered keywords and categories with top keywords only
per_row_df = generate_clustered_keywords_and_categories(per_row_df, clustered_keyword_mapping, category_keyword_counts, remove_duplicates=remove_duplicates)
Re-run One-Hot Encoding:
With per_row_df now containing only the top 50 keywords per category (and 'Other'), the OneHotEncoder will generate at most 51 columns per category (including 'Other'), giving you a total of 255 columns (5 categories × 51 keywords).
Revised Code Snippet:
Here’s the revised generate_clustered_keywords_and_categories function:
def generate_clustered_keywords_and_categories(per_row_df, clustered_keyword_mapping, category_keyword_counts, remove_duplicates=False):
top_keywords_per_category = {category: set(counts.keys()) for category, counts in category_keyword_counts.items()}
# Initialize lists for categories
categories = ['Object', 'Adjective', 'Activity', 'Place', 'Feeling']
category_keywords_list = {cat: [] for cat in categories}
for idx, row in per_row_df.iterrows():
lemmatized_keywords = row['Lemmatized Keywords'].split(', ')
clustered_keywords = [clustered_keyword_mapping.get(kw, kw) for kw in lemmatized_keywords]
if remove_duplicates:
# Remove duplicates while preserving order
clustered_keywords = list(dict.fromkeys(clustered_keywords))
# Assign keywords to categories based on positions
for i, category in enumerate(categories):
if i < len(clustered_keywords):
keyword = clustered_keywords[i]
# Map to 'Other' if not in top keywords
if keyword not in top_keywords_per_category[category]:
keyword = 'Other'
category_keywords_list[category].append(keyword)
else:
category_keywords_list[category].append('') # or keep as 'Other'
# Add the category columns to per_row_df
for category in categories:
per_row_df[category] = category_keywords_list[category]
return per_row_df
Updated Main Function:
Ensure your main function passes category_keyword_counts to this function:
# Step 4: Categorize and count clustered keywords
category_keyword_counts = categorize_and_count_clustered_keywords(per_row_df)
# Step 5: Generate clustered keywords per row and categorize with top keywords only
per_row_df = generate_clustered_keywords_and_categories(per_row_df, clustered_keyword_mapping, category_keyword_counts, remove_duplicates=remove_duplicates)
Result:
After making these changes, when you perform one-hot encoding, the OneHotEncoder will only encode the limited set of keywords (top 50 per category plus 'Other'), resulting in the expected number of columns in your output CSV.
Additional Tips:
Handle Missing Values:
In cases where a category might not have a keyword (e.g., the list is shorter than 5 keywords), you might want to replace empty strings with 'Other' or a placeholder like 'None'.
This ensures consistency and avoids unexpected behavior during encoding.
Check for Duplicates:
Be cautious with the remove_duplicates parameter. Removing duplicates might shift keywords and change their assigned categories (positions). Ensure that this behavior aligns with your expectations.
Consistency in Keyword Positions:
The assignment of keywords to categories based on their positions assumes that the order of keywords reflects their categorical relevance.
If this assumption doesn't hold, consider implementing a more sophisticated method to assign keywords to categories, possibly using Part-of-Speech tagging or semantic analysis.
Conclusion:
By implementing these changes, you ensure that the categories in your dataset have a controlled number of unique values, which aligns with your initial expectation of having at most 50 unique words per keyword category. This approach not only resolves the unexpected number of columns but also enhances the quality of your one-hot encoded data for further analysis or machine learning tasks.