如何对 python 中具有分类知识库的新条目进行分类
How classify new entries in python having classified knowledge base
我在 python 中有一组向量构成我的知识库,例如:
KB=[[1,2,3,4],[1,2,2,1],[4,3,1,2],[5,4,3,5]]
现在我计算了 KB 的集群,使用:
from sklearn.cluster import KMeans
model=KMeans(n_clusters=3)
model.fit(KB)
现在我有一个新条目(我可以有多个),
A=[3,2,1,3]
我会知道哪个集群最适合 A 关于上面计算的集群,然后利用 KB。
你能帮帮我吗?
提前致谢
你在这里:
KB=[[1,2,3,4],[1,2,2,1],[4,3,1,2],[5,4,3,5]]
from sklearn.cluster import KMeans
model=KMeans(n_clusters=3).fit(KB)
A=[3,2,1,3]
l = model.predict([A])
print model.labels_, l
centers = model.cluster_centers_.copy()
print centers
为了让你的模型成为'fit',我加入了两条线。
然后我使用预测方法来 .. 预测。
我还打印了模型中使用的每个示例的标签。
编辑 添加情节
import matplotlib.pyplot as plt
import numpy
# Compute the distances vector to vector
d = numpy.array([[numpy.sum(KBi - cj) for KBi in KB] for cj in centers])
print d
# for cluster 0 and 1
plt.scatter(d[0], d[1])
plt.pause(10)
我在 python 中有一组向量构成我的知识库,例如:
KB=[[1,2,3,4],[1,2,2,1],[4,3,1,2],[5,4,3,5]]
现在我计算了 KB 的集群,使用:
from sklearn.cluster import KMeans
model=KMeans(n_clusters=3)
model.fit(KB)
现在我有一个新条目(我可以有多个),
A=[3,2,1,3]
我会知道哪个集群最适合 A 关于上面计算的集群,然后利用 KB。
你能帮帮我吗?
提前致谢
你在这里:
KB=[[1,2,3,4],[1,2,2,1],[4,3,1,2],[5,4,3,5]]
from sklearn.cluster import KMeans
model=KMeans(n_clusters=3).fit(KB)
A=[3,2,1,3]
l = model.predict([A])
print model.labels_, l
centers = model.cluster_centers_.copy()
print centers
为了让你的模型成为'fit',我加入了两条线。 然后我使用预测方法来 .. 预测。 我还打印了模型中使用的每个示例的标签。
编辑 添加情节
import matplotlib.pyplot as plt
import numpy
# Compute the distances vector to vector
d = numpy.array([[numpy.sum(KBi - cj) for KBi in KB] for cj in centers])
print d
# for cluster 0 and 1
plt.scatter(d[0], d[1])
plt.pause(10)