我们使用简单的 k-means 算法来演示如何进行聚类。 聚类可以帮助发现数据中有价值的、隐藏的分组。 数据集在 Obtain_dataset Notebook 中创建。
# imports
import numpy as np
import pandas as pd# load data
datafile_path = "./data/fine_food_reviews_with_embeddings_1k.csv"df = pd.read_csv(datafile_path)
df["embedding"] = df.embedding.apply(eval).apply(np.array) # convert string to numpy array
matrix = np.vstack(df.embedding.values)
matrix.shape
(1000, 1536)
1. 使用 K-means 找到聚类
我们展示了 K-means 的最简单用法。 您可以选择最适合您的用例的聚类。
from sklearn.cluster import KMeansn_clusters = 4kmeans = KMeans(n_clusters=n_clusters, init="k-means++", random_state=42)
kmeans.fit(matrix)
labels = kmeans.labels_
df["Cluster"] = labelsdf.groupby("Cluster").Score.mean().sort_values()
/Users/ted/.virtualenvs/openai/lib/python3.9/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warningwarnings.warn(
Cluster
0 4.105691
1 4.191176
2 4.215613
3 4.306590
Name: Score, dtype: float64
from sklearn.manifold import TSNE
import matplotlib
import matplotlib.pyplot as plttsne = TSNE(n_components=2, perplexity=15, random_state=42, init="random", learning_rate=200)
vis_dims2 = tsne.fit_transform(matrix)x = [x for x, y in vis_dims2]
y = [y for x, y in vis_dims2]for category, color in enumerate(["purple", "green", "red", "blue"]):xs = np.array(x)[df.Cluster == category]ys = np.array(y)[df.Cluster == category]plt.scatter(xs, ys, color=color, alpha=0.3)avg_x = xs.mean()avg_y = ys.mean()plt.scatter(avg_x, avg_y, marker="x", color=color, s=100)
plt.title("Clusters identified visualized in language 2d using t-SNE")
Text(0.5, 1.0, 'Clusters identified visualized in language 2d using t-SNE')
二维投影中簇的可视化。 在此运行中,绿色集群 (#1) 似乎与其他集群完全不同。 让我们看看每个集群的一些样本。
2.簇中的文本样本和命名簇
让我们展示来自每个集群的随机样本。 我们将使用 text-davinci-003 来命名集群,基于来自该集群的 5 条评论的随机样本。
import openai# Reading a review which belong to each group.
rev_per_cluster = 5for i in range(n_clusters):print(f"Cluster {i} Theme:", end=" ")reviews = "\n".join(df[df.Cluster == i].combined.str.replace("Title: ", "").str.replace("\n\nContent: ", ": ").sample(rev_per_cluster, random_state=42).values)response = openai.Completion.create(engine="text-davinci-003",prompt=f'What do the following customer reviews have in common?\n\nCustomer reviews:\n"""\n{reviews}\n"""\n\nTheme:',temperature=0,max_tokens=64,top_p=1,frequency_penalty=0,presence_penalty=0,)print(response["choices"][0]["text"].replace("\n", ""))sample_cluster_rows = df[df.Cluster == i].sample(rev_per_cluster, random_state=42)for j in range(rev_per_cluster):print(sample_cluster_rows.Score.values[j], end=", ")print(sample_cluster_rows.Summary.values[j], end=": ")print(sample_cluster_rows.Text.str[:70].values[j])print("-" * 100)
Cluster 0 Theme: All of the reviews are positive and the customers are satisfied with the product they purchased.
5, Loved these gluten free healthy bars, saved $$ ordering on Amazon: These Kind Bars are so good and healthy & gluten free. My daughter ca
1, Should advertise coconut as an ingredient more prominently: First, these should be called Mac - Coconut bars, as Coconut is the #2
5, very good!!: just like the runts<br />great flavor, def worth getting<br />I even o
5, Excellent product: After scouring every store in town for orange peels and not finding an
5, delicious: Gummi Frogs have been my favourite candy that I have ever tried. of co
----------------------------------------------------------------------------------------------------
Cluster 1 Theme: All of the reviews are about pet food.
2, Messy and apparently undelicious: My cat is not a huge fan. Sure, she'll lap up the gravy, but leaves th
4, The cats like it: My 7 cats like this food but it is a little yucky for the human. Piece
5, cant get enough of it!!!: Our lil shih tzu puppy cannot get enough of it. Everytime she sees the
1, Food Caused Illness: I switched my cats over from the Blue Buffalo Wildnerness Food to this
5, My furbabies LOVE these!: Shake the container and they come running. Even my boy cat, who isn't
----------------------------------------------------------------------------------------------------
Cluster 2 Theme: All of the reviews are positive and express satisfaction with the product.
5, Fog Chaser Coffee: This coffee has a full body and a rich taste. The price is far below t
5, Excellent taste: This is to me a great coffee, once you try it you will enjoy it, this
4, Good, but not Wolfgang Puck good: Honestly, I have to admit that I expected a little better. That's not
5, Just My Kind of Coffee: Coffee Masters Hazelnut coffee used to be carried in a local coffee/pa
5, Rodeo Drive is Crazy Good Coffee!: Rodeo Drive is my absolute favorite and I'm ready to order more! That
----------------------------------------------------------------------------------------------------
Cluster 3 Theme: All of the reviews are about food or drink products.
5, Wonderful alternative to soda pop: This is a wonderful alternative to soda pop. It's carbonated for thos
5, So convenient, for so little!: I needed two vanilla beans for the Love Goddess cake that my husbands
2, bot very cheesy: Got this about a month ago.first of all it smells horrible...it tastes
5, Delicious!: I am not a huge beer lover. I do enjoy an occasional Blue Moon (all o
3, Just ok: I bought this brand because it was all they had at Ranch 99 near us. I
----------------------------------------------------------------------------------------------------
请务必注意,集群不一定与您打算使用它们的用途相匹配。 大量的聚类将关注更具体的模式,而少量的聚类通常会关注数据中最大的差异。