我转载了您的代码,但确实收到了您的错误。 我在这里解释得更好:

他具有一个vectorized_text变量( np.stack ),该变量模拟一个“热编码”功能集(仅包含0和1)。 在DBSCAN模型中,他使用custom_metric函数来计算距离。 可以预期的是,在运行模型时,自定义指标函数将观测对按原样作为参数对:一个热编码值,但是当在距离函数中打印这些值时,仅照原样取,正如他在问题中所描述的,另一个似乎是一列实际价值:

x = [0.5 0.5 0.5 ... 0.5 0.5] y = [0. 0. 0. 1. 0. 0. ... 1. 0.]

无论如何,当我将列表传递给fit参数时,该函数将按原样获取值:

from sklearn.cluster import KMeans, DBSCAN, MeanShift

x = [1, 0, 1]

y = [0, 0, 1]

feature_set = [x*5]*5

def distance(x, y):

# Printing here the values. Should be 0s and 1s

print(x, y)

match_count = 0.

for xi, yi in zip(x, y):

if float(xi) == 1. and xi == yi:

match_count += 1

return match_count

def custom_metric(x, y):

# x, y are two vectors

# distance(.,.) calculates count of elements when both xi and yi are True

return distance(x, y)

dbscan = DBSCAN(min_samples=2, metric=custom_metric, eps=3, p=1).fit(feature_set)`

结果:

[1. 0. 1. 1. 0. 1. 1. 0. 1. 1. 0. 1. 1. 0. 1.] ... [1. 0. 1. 1. 0.1. 1. 0. 1. 1. 0. 1. 1. 0. 1.]

[1. 0. 1. 1. 0. 1. 1. 0. 1. 1. 0. 1. 1. 0. 1.] ... [1. 0. 1. 1. 0.1. 1. 0. 1. 1. 0. 1. 1. 0. 1.]

我建议您使用pandas DataFrame或其他类型的值,并查看其是否有效。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐