from sklearn.cluster import AgglomerativeClustering
from sklearn.preprocessing import LabelEncoder
import pandas as pd
# Sample data
data = [
["green", "nature", "thumbnail", "landscape"],
["blue", "architecture", "medium", "portrait"],
["blue", "people", "medium", "landscape"],
["yellow", "nature", "medium", "portrait"],
["green", "nature", "thumbnail", "landscape"],
["blue", "people", "medium", "landscape"],
["blue", "nature", "thumbnail", "portrait"],
["yellow", "architecture", "thumbnail", "landscape"],
["blue", "people", "medium", "portrait"],
["yellow", "nature", "medium", "landscape"],
["yellow", "people", "thumbnail", "portrait"],
["blue", "people", "medium", "landscape"],
["red", "architecture", "thumbnail", "landscape"],
]
# Encode categorical features
label_encoders = [LabelEncoder() for _ in range(len(data[0]))]
encoded_data = []
for i, column in enumerate(zip(*data)):
encoded_data.append(label_encoders[i].fit_transform(column))
X = list(zip(*encoded_data)) # Features
# Clustering
n_clusters = 3 # Number of clusters for Agglomerative Clustering
clusters = AgglomerativeClustering(n_clusters=n_clusters)
labels = clusters.fit_predict(X)
# Add cluster labels to the original data
data_with_clusters = pd.DataFrame(data, columns=["Color", "Category", "Size", "Type"])
data_with_clusters["Cluster"] = labels
# Recommendation function
def recommend_items(cluster_label, data_with_clusters):
items_in_cluster = data_with_clusters[data_with_clusters["Cluster"] == cluster_label]
recommended_items = items_in_cluster.sample(n=min(3, len(items_in_cluster))) # Sample up to 3 items from the cluster
return recommended_items
# Example usage
user_interaction = ["yellow", "architecture", "thumbnail", "landscape"] # Assuming user interacted with this item
interaction_index = data.index(user_interaction)
cluster = clusters.labels_[interaction_index]
recommendations = recommend_items(cluster, data_with_clusters)
print("Recommended items:")
print(recommendations)