我不知道我在做什么。在
我想把网站分为三类:“金融”、“IT”、“医疗”
我对每个网站都有几个指标(基本上是一个关键字列表),最后我使用了knn分类器:
# fit the classifier
>>> y = array(['financial_services', 'health_care', 'information_technology'], dtype=object)
>>> X.shape = (3L, 571L)
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X, y)
# predict the result for some website (predict is a matrix with my features)
print(neigh.predict(predict))
>>> ['financial_services'] # predict the first category
print(neigh.kneighbors(predict)) # get the "distances" to each category
>>> (array([[ 2323819.25162006, 2323841.23289028, 2323852.69883011]]), array([[2, 0, 1]], dtype=int64)) # we can see that this website is closer to the category #2, which is IT
我试图预测其他网站的类别,但我总是用neigh.predict
来预测,例如“金融服务”,而使用距离(使用neigh.predict
)得到不同的值。在
我错过了什么?在
根据
sklearn
文档,从kneighbours
返回的index数组为您提供了“总体矩阵中最近点的索引”,而不是类标签。距离从最近到最远排序,索引告诉您哪个距离对应于训练集中的哪个实例。因此[[ 2323819.25162006, 2323841.23289028, 2323852.69883011]], [[2, 0, 1]]
告诉您第3个(索引2)实例是距离2323819.25162006
最近的实例。这可能有点令人困惑,因为在训练数据和k=3
中正好有3个点。这里要记住的是,索引引用的是原始训练样本数组中的索引,而不是同一函数返回的距离数组。在我认为你的主要问题是人口矩阵包含了3个3个类的例子(每个类1个)。当您设置
kneighbours=3
时,您认为分类器试图做什么?找到一个测试点的三个最近的邻居,但是只有三个例子,它们都有不同的类。在用户指南有这样的说法
在你的例子中,最接近的3个点。同样只有三点,而且他们都有不同的等级,所以多数票永远无法正常运作。在
相关问题 更多 >
编程相关推荐