Python 返回numpy数组返回NoneType
我有一个生成numpy ndarray的函数,我需要返回这个数组,但当我这样做时,它在我调用它的另一个函数中给了我一个NoneType,它看起来像这样:Python 返回numpy数组返回NoneType,python,numpy,numpy-ndarray,Python,Numpy,Numpy Ndarray,我有一个生成numpy ndarray的函数,我需要返回这个数组,但当我这样做时,它在我调用它的另一个函数中给了我一个NoneType,它看起来像这样: def kMeans(dataSet, kMin, kMax): for k in range(kMin,kMax +1): centeroids = np.random.choice(dataSet, k) oldClusters = [] clusters = np.array(fin
def kMeans(dataSet, kMin, kMax):
for k in range(kMin,kMax +1):
centeroids = np.random.choice(dataSet, k)
oldClusters = []
clusters = np.array(findClosestCenteroids(dataSet, centeroids))
finalizedClusters = kMeansAlgorithm(dataSet, centeroids, clusters, oldClusters, k)
print(type(finalizedClusters))
def kMeansAlgorithm(dataset,centeroids, currCluster, oldCluster, k):
idx = np.lexsort((currCluster[:, 0], currCluster[:, 1]))
currentClusters = currCluster[idx]
change = np.array_equal(currentClusters, oldCluster)
oldClusters = np.copy(currentClusters)
totalCount = 0
for i in range(k): #vind het aantal items in een cluster
count = np.count_nonzero(currentClusters == str(i))
centeroids[i] = currentClusters[totalCount+ round(count/2)][0]
totalCount = totalCount + count
if change == True:
print(type(currentClusters))
return currentClusters
print(change)
newClusters = np.array(findClosestCenteroids(dataset, centeroids))
kMeansAlgorithm(dataset, centeroids, newClusters, oldClusters, k)
现在打印:
<class 'numpy.ndarray'>
<class 'NoneType'>
如何正确返回numpy数组?发现了问题,使用了递归函数,但忘记了总是需要返回函数,而不是在
fun1
末尾添加return array1
?这不够代码(并且有语法错误array 2
)来查看错误;您可以直接返回一个numpy数组,它将正常工作,因此代码中存在一些错误!我还是想用array1来搞笑这能回答你的问题吗?请出示实际代码。此处显示的内容有多个打字错误;例如,array1=fun2():
是一个语法错误。