0

我是数据科学的新手,我试图为一个有 4000 行的数据集绘制散点图。我在 macbook 上运行 Jupyter Notebook。我发现散点图出现在 Jupyter notebook 中需要超过五分钟。我的笔记本是最近买的,它是 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()
4

1 回答 1

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

我得到了这张图片 在此处输入图像描述

于 2018-10-10T06:44:42.910 回答