Skip to content
Advertisement

How to use Scikit kmeans when I have a dataframe

I have converted my dataset to dataframe. I was wondering how to use it in scikit kmeans or if any other kmeans package available.

import csv
import codecs
import pandas as pd
import sklearn
from sklearn import cross_validation
from sklearn.cross_validation import train_test_split
sample_df = pd.read_csv('sample.csv',sep='t',keep_default_na=False, na_values=[""])
print sample_df['Polarity']
print sample_df['Gravity']
print sample_df['Sense']
print sample_df[['Polarity','Gravity']]
sklearn.cluster.KMeans(n_clusters=8, init='k-means++', n_init=10, max_iter=300, tol=0.0001, precompute_distances='auto', verbose=0, random_    state=None, copy_x=True, n_jobs=1)

Advertisement

Answer

sklearn is fully compatible with pandas DataFrames. Therefore, it’s as simple as:

sample_df_train, sample_df_test = sklearn.cross_validation.train_test_split(sample_df, train_size=0.6)

cluster = sklearn.cluster.KMeans(n_clusters=8, init='k-means++', n_init=10, max_iter=300, tol=0.0001, precompute_distances='auto', verbose=0, random_state=None, copy_x=True, n_jobs=1)
cluster.fit(sample_df_train)
result = cluster.predict(sample_df_test)

That 0.6 means you use 60% of your data for training, 40% for testing.

More info here:

http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.train_test_split.html http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement