Macbook上的KMeans散点图

时间:2018-10-10 06:27:53

标签: python python-3.x matplotlib k-means

我是数据科学领域的新手,我试图为具有4000行的数据集绘制散点图。我在Macbook上运行Jupyter Notebook。我发现散点图需要五分钟多的时间才能显示在Jupyter笔记本中。我的笔记本是最近购买的,它是2.3Ghz的Intel Core i5,内存是8GB。

我有两个问题:为什么花了这么长时间?为什么情节如此拥挤(例如,所有x比例尺都显得很小,它们放在一起并无法清晰地阅读)并且不太清楚。数据集在这里:https://raw.githubusercontent.com/datascienceinc/learn-data-science/master/Introduction-to-K-means-Clustering/Data/data_1024.csv

真的很感谢您的光临

这是我的代码:

import numpy as np
import pandas as pd
import matplotlib
from matplotlib import pyplot as plt
%matplotlib inline
from sklearn.cluster import KMeans

df= pd.read_csv('/users/kyaw/Downloads/data_1024.csv')
df = df.join(df['Driver_ID'].str.split(expand=True))
df = df.drop(["Driver_ID"], axis=1)
df.columns=['Driver_ID','Distance_Feature','Speeding_Feature']

f1 = df['Distance_Feature'].values
f2 = df['Speeding_Feature'].values

X=np.array(list(zip(f1,f2)))

fig=plt.gcf()
fig.set_size_inches(10,8)
kmeans = KMeans(n_clusters=3).fit(X) 

plt.scatter(X[:,0], X[:,1], c=kmeans.labels_, cmap='rainbow')  
plt.scatter(kmeans.cluster_centers_[:,0] ,kmeans.cluster_centers_[:,1], color='black')
plt.show()

1 个答案:

答案 0 :(得分:0)

我试图运行您的代码,但没有成功。我做了以下更正

import numpy as np 
import pandas as pd 
import matplotlib 
from matplotlib import pyplot as plt
#%matplotlib inline  --> Removed this inline, maybe is here due to jupyter
from sklearn.cluster import KMeans    

df= pd.read_csv('./data_1024.csv',sep='\t' )  #indicate the separator as tab.  
#remove the other instructions that are useless

f1 = df['Distance_Feature'].values 
f2 = df['Speeding_Feature'].values

X=np.array(list(zip(f1,f2)))

fig=plt.gcf() 
fig.set_size_inches(10,8) 
kmeans = KMeans(n_clusters=3).fit(X) 

plt.scatter(X[:,0], X[:,1], c=kmeans.labels_, cmap='rainbow')    
plt.scatter(kmeans.cluster_centers_[:,0] ,kmeans.cluster_centers_[:,1], color='black') 
plt.show()

我得到了这张图片 enter image description here