Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么权重不改变sci工具包学习包中的K-均值聚类中心位置?_Python_Scikit Learn_K Means - Fatal编程技术网

Python 为什么权重不改变sci工具包学习包中的K-均值聚类中心位置?

Python 为什么权重不改变sci工具包学习包中的K-均值聚类中心位置?,python,scikit-learn,k-means,Python,Scikit Learn,K Means,我正在尝试使用权重选项计算簇的中心位置。但重量似乎不起作用 下面是表示问题的简单脚本 X = [] weights = [] for x in range(-10,10): for y in range(-10,10): X+= [[x,y]] if x>0 and y>0: weights += [10000] else: weights += [1] X = np.array

我正在尝试使用权重选项计算簇的中心位置。但重量似乎不起作用

下面是表示问题的简单脚本

X = []
weights = []
for x in range(-10,10):
    for y in range(-10,10):
        X+= [[x,y]]
        if x>0 and y>0:
            weights += [10000]
        else:
            weights += [1]

X = np.array(X)
weights = np.array(weights)

kmeans = KMeans(n_clusters=1, random_state=0).fit(X,weights)
print kmeans.cluster_centers_
它在第一季度打印重量为10000的
[-0.5-0.5]]

我预计大约为
(5,5)

编辑1: 尝试将fit()调用为:

返回:

TypeError: fit() got an unexpected keyword argument 'sample_weight'
TypeError: fit() takes at most 3 arguments (4 given)
添加第二个变量也没有帮助:

fit(X,None,weights)
返回:

TypeError: fit() got an unexpected keyword argument 'sample_weight'
TypeError: fit() takes at most 3 arguments (4 given)

问题在于调用
fit
方法的方式。 您需要将
权重作为关键字参数传递

kmeans = KMeans(n_clusters=1, random_state=0).fit(X, sample_weight=weights)
说明

fit
方法的签名如下:

KMeans.fit(self, X, y=None, sample_weight=None)
通过调用
KMeans.fit(self,X,weights)
然后隐式地
y=weights
。由于
y
被忽略,因此没有效果


有关更多信息,请参阅。

谢谢您的回答。我一开始就尝试过这个,但是我得到了一个错误:TypeError:fit()得到了一个意外的关键字参数“sample\u weight”它对你有用吗?我认为你需要获得一个更新版本的scikit LearnChange版本,从0.19.2更改为0.20.2解决了这个问题。非常感谢。