2016-03-23 27 views
1

分配定製顏色的簇是否有使用一個首選的顏色(8至10個或更多),用於通過以下代碼繪製不同簇的一種方法:使用numpy的

import numpy as np 

existing_df_2d.plot(
    kind='scatter', 
    x='PC2',y='PC1', 
    c=existing_df_2d.cluster.astype(np.float), 
    figsize=(16,8)) 

的代碼是從這裏:https://www.codementor.io/python/tutorial/data-science-python-pandas-r-dimensionality-reduction

感謝

我曾嘗試以下沒有成功:

LABEL_COLOR_MAP = {0 : 'red', 
       1 : 'blue', 
       2 : 'green', 
       3 : 'purple'} 

label_color = [LABEL_COLOR_MAP[l] for l in range(len(np.unique(existing_df_2d.cluster)))] 

existing_df_2d.plot(
    kind='scatter', 
    x='PC2',y='PC1', 
    c=label_color, 
    figsize=(16,8)) 
+1

http://stackoverflow.com/a/14779462/3838691 – MaxNoe

+0

由於最大。但不知何故,我仍然無法解決問題。我在原始文章中添加了幾行代碼 – user27976

回答

1

您需要通過字典LABEL_COLOR_MAP添加一個新的顏色4和使用maping

LABEL_COLOR_MAP = {0 : 'red', 
        1 : 'blue', 
        2 : 'green', 
        3 : 'purple', 
        4 : 'yellow'} 

existing_df_2d.plot(
     kind='scatter', 
     x='PC2',y='PC1', 
     c=existing_df_2d.cluster.map(LABEL_COLOR_MAP), 
     figsize=(16,8)) 

因爲:

print np.unique(existing_df_2d.cluster) 
[0 1 2 3 4] 

所有代碼:

import numpy as np 
import pandas as pd 
from sklearn.decomposition import PCA 
from sklearn.cluster import KMeans 

tb_existing_url_csv = 'https://docs.google.com/spreadsheets/d/1X5Jp7Q8pTs3KLJ5JBWKhncVACGsg5v4xu6badNs4C7I/pub?gid=0&output=csv' 

existing_df = pd.read_csv(
    tb_existing_url_csv, 
    index_col = 0, 
    thousands = ',') 
existing_df.index.names = ['country'] 
existing_df.columns.names = ['year'] 

pca = PCA(n_components=2) 
pca.fit(existing_df) 
PCA(copy=True, n_components=2, whiten=False) 
existing_2d = pca.transform(existing_df) 

existing_df_2d = pd.DataFrame(existing_2d) 
existing_df_2d.index = existing_df.index 
existing_df_2d.columns = ['PC1','PC2'] 
existing_df_2d.head() 

kmeans = KMeans(n_clusters=5) 
clusters = kmeans.fit(existing_df) 
existing_df_2d['cluster'] = pd.Series(clusters.labels_, index=existing_df_2d.index) 
print existing_df_2d.head() 

         PC1   PC2 cluster 
country           
Afghanistan -732.215864 203.381494  2 
Albania   613.296510 4.715978  3 
Algeria   569.303713 -36.837051  3 
American Samoa 717.082766 5.464696  3 
Andorra   661.802241 11.037736  3  

LABEL_COLOR_MAP = {0 : 'red', 
        1 : 'blue', 
        2 : 'green', 
        3 : 'purple', 
        4 : 'yellow'} 

existing_df_2d.plot(
     kind='scatter', 
     x='PC2',y='PC1', 
     c=existing_df_2d.cluster.map(LABEL_COLOR_MAP), 
     figsize=(16,8)) 

graph

測試

前10行通過PC2柱:

print existing_df_2d.loc[existing_df_2d['PC2'].nlargest(10).index,:] 
          PC1   PC2 cluster 
country           
Kiribati   -2234.809790 864.494075  2 
Djibouti   -3798.447446 578.975277  4 
Bhutan   -1742.709249 569.448954  2 
Solomon Islands -809.277671 530.292939  1 
Nepal    -986.570652 525.624757  1 
Korea, Dem. Rep. -2146.623299 438.945977  2 
Timor-Leste  -1618.364795 428.244340  2 
Tuvalu   -1075.316806 366.666171  1 
Mongolia   -686.839037 363.722971  1 
India   -1146.809345 363.270389  1 
+0

非常感謝@jezrael。但我的擔心仍然是,不同的顏色似乎在整個地方,並沒有顯示在該網站上獨特的羣集。我認爲顏色和羣集之間的聯繫仍然缺失。 – user27976

+0

是的,你是對的。我編輯答案並添加測試部分。 – jezrael

+0

優秀!它完美的作品。非常感謝。 – user27976