Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.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 RuntimeWarning:空片的平均值。out=out,**kwargs)_Python_Numpy_Math - Fatal编程技术网

Python RuntimeWarning:空片的平均值。out=out,**kwargs)

Python RuntimeWarning:空片的平均值。out=out,**kwargs),python,numpy,math,Python,Numpy,Math,我一直试图用numpy做一些数学运算,但由于某种原因,我得到了这个错误 /Users/Library/Python/3.7/lib/python/site-packages/numpy/core/fromnumeric.py:3420: RuntimeWarning: Mean of empty slice. out=out, **kwargs) /Users/Library/Python/3.7/lib/python/site-packages/numpy/core/_methods.py

我一直试图用numpy做一些数学运算,但由于某种原因,我得到了这个错误

/Users/Library/Python/3.7/lib/python/site-packages/numpy/core/fromnumeric.py:3420: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/Users/Library/Python/3.7/lib/python/site-packages/numpy/core/_methods.py:188: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
只有很少几行代码真正做了一些数学运算;主要是一些减法。这是我第一次遇到这个问题。如果有人能帮忙,我将不胜感激 下面是我的代码

import numpy as np
from copy import deepcopy
from numpy.linalg import norm
import matplotlib.pyplot as plt
def readfile(file_name):
    file = open(file_name,'r')
    lines = [list(map(int, line.strip("\n").split(","))) for line in file]
    x= np.array(lines)
    file.close()
    return x
X= readfile("LocationB.txt")
def dist(a, b, ax=1):
    return np.linalg.norm(a - b, axis=ax)
k = 3
# X coordinates of random centroids
C_x = np.random.randint(0, np.max(X)-2, size=k)
# Y coordinates of random centroids
C_y = np.random.randint(0, np.max(X)-2, size=k)
C = np.array(list(zip(C_x, C_y)), dtype=np.float32)
# To store the value of centroids when it updates
C_old = np.zeros(C.shape)
# Cluster Lables(0, 1)
clusters = np.zeros(len(X))
# Error func. - Distance between new centroids and old centroids
error = dist(C, C_old, None)
# Loop will run till the error becomes zero
while error != 0:
    # Assigning each value to its closest cluster
    for i in range(len(X)):
        distances = dist(X[i], C)
        cluster = np.argmin(distances)
        clusters[i] = cluster
    # Storing the old centroid values
    C_old = deepcopy(C)
    # Finding the new centroids by taking the average value
    for i in range(k):
        points = [X[j] for j in range(len(X)) if clusters[j] == i]
        C[i] = np.mean(points, axis=0)
    error = dist(C, C_old, None)
colors = ['r', 'g', 'b', 'y', 'c', 'm']
fig, ax = plt.subplots()
for i in range(k):
        points = np.array([X[j] for j in range(len(X)) if clusters[j] == i])
        ax.scatter(points[:, 0], points[:, 1], s=5, c=colors[i])
ax.scatter(C[:, 0], C[:, 1], marker='*', s=200, c='#050505')
plt.show()

确保
X
数组长度大于0是的,长度大于零,然后确保两件事:1<传递到
dist
函数的code>a和
b
参数的长度大于0。2.
np.max(X)-2
大于0如果不查看完整代码,将很难理解错误。正如@crackanddie提到的,这可能是由于
np.max(X)-2
不大于0造成的。@gofvonx我已经更新了代码,如果您可以看一下的话