Explanation of the Scenario
- Data Setup: The same sample dataset is used, where each row represents a customer’s preferences (e.g., product ratings).
- Cosine Similarity Calculation: The cosine_similarity function computes the similarity between all pairs of customers. We extract the similarities for Customer 1 (index 0) compared to all customers, including itself.
- Visualization: A line graph is created using Matplotlib, plotting Customer 1’s similarity scores against other customers. Each point is marked with a dot, and lines connect them for clarity. The x-axis shows customer indices, and the y-axis shows similarity scores (0 to 1). The plot is saved as 'cosine_similarity_line_graph.png'.
- Output: The similarity scores for Customer 1 are printed for reference.
References:
Cohen, Mike X. Practical Linear Algebra for Data Science (pp. 31-32). O'Reilly Media. Kindle Edition.
Code from Grok
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics.pairwise import cosine_similarity
# Sample data: vectors representing customer preferences (e.g., product ratings)
customers = np.array([
[5, 3, 0, 1], # Customer 1
[4, 0, 0, 1], # Customer 2
[1, 1, 5, 0], # Customer 3
[0, 2, 4, 0] # Customer 4
])
# Calculate cosine similarity matrix
cos_sim_matrix = cosine_similarity(customers)
# Extract similarity scores for Customer 1 (index 0) compared to all customers
customer_1_similarities = cos_sim_matrix[0]
# Plotting as a simple line graph
plt.figure(figsize=(8, 5))
plt.plot(range(1, len(customers) + 1), customer_1_similarities, marker='o', linestyle='-', color='b')
plt.title('Cosine Similarity of Customer 1 to Other Customers')
plt.xlabel('Customer Index')
plt.ylabel('Cosine Similarity')
plt.xticks(range(1, len(customers) + 1), [f'Cust {i+1}' for i in range(len(customers))])
plt.grid(True)
plt.ylim(0, 1.1) # Similarity ranges from 0 to 1
plt.tight_layout()
# Save the plot to a file
plt.savefig('cosine_similarity_line_graph.png')
plt.show()
# Print the cosine similarity scores for Customer 1
print("Cosine Similarity of Customer 1 to Others:")
for i, score in enumerate(customer_1_similarities, 1):
print(f"Customer {i}: {score:.2f}")

No comments:
Post a Comment