我已经将数据集转换为数据帧。我想知道如何在scikit kmeans中使用它,或者是否有任何其他kmeans软件包可用。
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)
sklearn
与pandas
数据帧完全兼容。因此,它就像
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)
0.6
这意味着您将 60% 的数据用于训练,40% 用于测试。
更多信息在这里:
http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.train_test_split.htmlhttp://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html