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)  
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)

  • 1
    $\begingroup$ Is your input transposed? It's saying you passed only one data point but you mean to pass one column $\endgroup$ – 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.

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.