繁体   English   中英

Macbook上的KMeans散点图

[英]KMeans scatter plot on macbook

我是数据科学领域的新手,我试图为具有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()

我尝试运行您的代码,但没有成功。 我进行以下更正

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()

我得到了这张图片 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM