2016-12-17 95 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)。

相關問題