本文介绍了Scikit-learn-KN​​eighborsClassifier的用户定义的权重函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个KNeighborsClassifier,它根据4个属性对数据进行分类.我想手动加权这4个属性,但总是遇到操作数不能与形状(1,5)(4)一起广播"的问题.

I have a KNeighborsClassifier which classifies data based on 4 attributes. I'd like to weight those 4 attributes manually but always run into "operands could not be broadcast together with shapes (1,5) (4)".

weights : [callable] : a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.上的文档很少(来自此处)

There is very little documentation on weights : [callable] : a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.(from here)

这就是我现在拥有的:

    for v in result:
        params = [v['a_one'], v['a_two'], v['a_three'], v['a_four']]
        self.training_data['data'].append(params)
        self.training_data['target'].append(v['answer'])

    def get_weights(array_weights):
        return [1,1,2,1]

    classifier = neighbors.KNeighborsClassifier(weights=get_weights)

推荐答案

可解释的sklearn权重的解释

Explanation of the sklearn weights callable

import numpy as np
from sklearn.neighbors import KNeighborsClassifier

创建用于模型训练的样本数据

Create sample data for model training

df = pd.DataFrame({'feature1':[1,3,3,4,5], 'response':[1,1,1,2,2]})

y = df.response
# [1,1,1,2,2]

X_train = df[['feature1']]
# [1,3,3,4,5]

定义自定义距离函数(打印输入数据结构)

Define a custom distance function (print input data structure)

def my_distance(weights):
    print(weights)
    return weights

定义传递my_distance作为权重的模型

Define model passing in my_distance as a callable to weights

knn = KNeighborsClassifier(n_neighbors=3, weights=my_distance)

knn.fit(X_train,y)

knn.predict([[1]])
# [[ 0.  2.  2.]]
# array([1])

说明:显示3个最接近预测值1的邻居(n_neighbors = 3)

Explanation: display the 3 closest neighbors (n_neighbors=3) to the predicted value of 1

X_train中最接近1的三个邻居:

The three closest neighbors to 1 in X_train:

1, 3, 3 

距离:

[[ 0.  2.  2.]]

1 - 1 = 0 
3 - 1 = 2
3 - 1 = 2

预测的班级:

array([1])

这篇关于Scikit-learn-KN​​eighborsClassifier的用户定义的权重函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 01:16