5
$\begingroup$

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)

$\endgroup$
1
  • 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, 2018 at 22:58

2 Answers 2

2
$\begingroup$

Here is how to fit k-means to a 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()
$\endgroup$
0
$\begingroup$

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

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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