问题背景

使用matplotlib将DBSCAN分类结果散点图可视化时提示此TypeError

源代码:
from sklearn.cluster import DBSCAN import pandas as pd import
matplotlib.pyplot as plt def devicesDbscan():
path="./unique_deviceID_lng_lat.csv"
df=pd.read_csv(path,header=None,names=["DEVICE_ID","LNG","LAT"])
print(df.shape()) X_df=df.drop("DEVICE_ID",axis=1) #去掉ID
y_pred=DBSCAN(eps=0.1,min_samples=1,n_jobs=20).fit_predict(X_df) #拟合并返回预测标签
count_clusters = len(set(y_pred)) #聚类簇的数目
print("clusters的数目:"+str(count_clusters))
plt.scatter(X_df[:,0],X_df[:,1],c=y_pred) #-->提示错误地方 plt.show() if
__name__=="__main__": devicesDbscan()
解决

将DataFrame对象X_df转成ndarray数组即可
from sklearn.cluster import DBSCAN import pandas as pd import
matplotlib.pyplot as plt def devicesDbscan():
path="./unique_deviceID_lng_lat.csv"
df=pd.read_csv(path,header=None,names=["DEVICE_ID","LNG","LAT"])
print(df.shape()) X_df=df.drop("DEVICE_ID",axis=1)
y_pred=DBSCAN(eps=0.1,min_samples=1,n_jobs=20).fit_predict(X_df) count_clusters
= len(set(y_pred)) print("clusters的数目:"+str(count_clusters))
plt.scatter(X_df.values[:,0],X_df.values[:,1],c=y_pred) plt.show() if
__name__=="__main__": devicesDbscan()
 

 

技术
©2019-2020 Toolsou All rights reserved,
Python常用函数航拍:特斯拉上海工厂Model Y已经大量下线如何用Python批量导入Excel并用Pandas整合2020年,Java还有发展前景么?MapReduce工作流程(编程规范及示例编写)如何防止Mac进入睡眠状态?告诉你4种有效的方法哈希映射(HashMap)浅谈Spring事件机制网上买菜莫名被开通“美团月付” 逾期或影响个人征信element表单验证数字类型和范围