# K-means clustering on the data frame having only one column

unsup_df is a DataFrame which has only one column: review.

I want to form 2 clusters of the reviews. One positive and one negative.

from sklearn.cluster import KMeans

tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(unsup_df)
num_clusters = 2
km = KMeans(n_clusters=num_clusters)
km.fit(tfidf_matrix)
clusters = km.labels_.tolist()


The above piece of code is throwing an error:

ValueError: n_samples=1 should be >= n_clusters=2

on the line km.fit(tfidf_matrix)

• Is your input transposed? It's saying you passed only one data point but you mean to pass one column – Sean Owen Sep 7 '18 at 22:58

Here is how to fit k-means to single dimensional text data in Pandas:

import pandas as pd
from sklearn.cluster import KMeans
from sklearn.feature_extraction.text import TfidfVectorizer

df = pd.DataFrame({"corpus": ["I am Sam. Sam-I-am",
"That Sam-I-am! That Sam-I-am! I do not like that Sam-I-am",
"Do you like green eggs and ham?",
"I do not like them, Sam-I-am. I do not like green eggs and ham"]})

x = TfidfVectorizer().fit_transform(df.corpus)
km = KMeans(n_clusters=2).fit(x)
km.labels_.tolist() # Results in a list similar to this: [0, 0, 1, 1]


Your unsup_df must be in the wrong shape. Otherwise, it should work.

• it's a data frame. am I suppose to convert it into list or series ? @Louis – Dhanshree Bagal Nov 10 '17 at 14:31
• X should be a list of lists or numpy array. One can use 'df.values'. – rnso Oct 30 '18 at 1:07