2016-12-17 68 views
1

我想在下面提到的数据上应用KMeans(Scikit-learn)。 Data是否有可能在Python中使用KMeans中的非浮点数据(Scikit-Learn)?

我已经看到足够的例子,其中Float64值显示在群集中。我想知道的是如果在列df [[Description]]上有可能进行聚类,则将x和y轴作为经度和纬度。

我的代码看起来像这样。

from sklearn.cluster import KMeans 
import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib 
from sklearn.preprocessing import LabelEncoder 
import pandas as pd 
matplotlib.style.use('ggplot') 

df = pd.read_csv('df.csv') 

encoder =LabelEncoder() 
Longitude = encoder.fit_transform(df.Longitude) 
Latitude= df[df.columns[19]].values #(latitude) 

x=np.array([Longitude, Latitude]).T 

est = KMeans(3) 

est.fit(df[['Longitude', 'Latitude', 'Description']]) 

但我得到这一行的错误是

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) in() ----> 1 est.fit(df[['Longitude', 'Latitude', 'Description']])

c:\users\magiri\appdata\local\programs\python\python35-32\lib\site-packages\sklearn\cluster\k_means_.py in fit(self, X, y) 878 """ 879 random_state = check_random_state(self.random_state) --> 880 X = self._check_fit_data(X) 881 882 self.cluster_centers_, self.labels_, self.inertia_, self.n_iter_ = \

c:\users\magiri\appdata\local\programs\python\python35-32\lib\site-packages\sklearn\cluster\k_means_.py in _check_fit_data(self, X) 852 def _check_fit_data(self, X): 853 """Verify that the number of samples given is larger than k""" --> 854 X = check_array(X, accept_sparse='csr', dtype=[np.float64, np.float32]) 855 if X.shape[0] < self.n_clusters: 856 raise ValueError("n_samples=%d should be >= n_clusters=%d" % (

c:\users\magiri\appdata\local\programs\python\python35-32\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator) 380 force_all_finite) 381 else: --> 382 array = np.array(array, dtype=dtype, order=order, copy=copy) 383 384 if ensure_2d:

ValueError: could not convert string to float: 'GAME/DICE'

所以,我想知道的是df.Description簇,参照经度和纬度。我知道描述列有字符串值,这就是为什么我得到错误。无论如何,我可以避免这个错误,并可以看到描述列的聚类。

回答

2

K均值算法只适用于数值数据。您可以将LabelEncoder应用到您的“描述”字段,以将其转换为类ID。

同样将LabelEncoder应用于经度/纬度并不是最佳选择,因为这样就失去了两点之间多么接近的概念。相反,您应该在K-means之前对数据应用StandardScaler,以规范不同领域的相对权重。

+0

谢谢。因为它不能对数字数据以外的任何其他数据进行集群。 K-means对我来说可能不是正确的做法。 –

+0

@ManeetGiri所有聚类算法都适用于数字数据。如果您有文本数据,您可以使用“LabelEncoder”(如果您的类别数量有限)或“CountVectorizer”(用于常规文本)将其转换为数字数组,然后可将其提供给K-means (或者任何其他聚类算法)。 – rth

0

我已成功使用kmodes和kprototype来对分类数据进行聚类。这里有一个python实现:https://github.com/nicodv/kmodes。 Kmodes允许将分类数据和k原型聚类为分类和数字数据(kmeans和kmodes的混合)。来自github页面的示例用法

import numpy as np 
from kmodes.kmodes import KModes 

# random categorical data 
data = np.random.choice(20, (100, 10)) 

km = KModes(n_clusters=4, init='Huang', n_init=5, verbose=1) 

clusters = km.fit_predict(data) 

# Print the cluster centroids 
print(km.cluster_centroids_) 

kmodes根据点之间的常见类别进行简单聚类。距离测度用于kprototypes的简化概述是

distance = np.sum((a_num - b_num) ** 2) + gamma * np.sum(a_cat != b_cat) 

其中a_numb_num是两个百分点,和a_catb_cat的数值是分类值。 gamma是分类差异与数字距离成本的权重。默认值是数字特征标准偏差的一半(如果事先标准化数字特征,则为= 0.5)。

相关问题