为什么我在python中按“run”按钮时,它不会给出任何输出?

2024-09-30 00:35:17 发布

您现在位置:Python中文网/ 问答频道 /正文

几天前我开始了一门python课程。我在python分配中遇到了循环问题,即:

给定一组N个测量值(r1,r2,…,rN),我们首先将奇数编号的测量值分配给 等级1,偶数测量值为等级2。然后重复以下两个步骤:

•更新步骤:计算每个集群内测量的平均值(平均值)

•分配步骤:将每个测量值分配给具有最接近平均值的集群。如果打成平局, 将测量值分配给簇1

重复上述步骤,直到集群分配不变。怎么做还不能事先确定 在集群分配稳定之前,需要执行许多步骤

问题定义 创建一个函数,该函数以反射率测量向量作为输入,并返回聚类向量 使用上述算法计算分配

我创建了一个代码:

import numpy as np
import math
def clusterAnalysis(reflectance):
    ref=np.size(reflectance)# all N measurements
    even=np.any(ref %2==0)# N measurements are even numbers
    uneven=np.any(ref%2>0)# N measurements are odd number
    mean1=np.mean(even)#average of even numbers in cluster 2
    mean2=np.mean(uneven)# average of odd numbers in cluster 1
    sub1=abs(ref-mean1)
    sub2=abs(ref-mean2)
    while sub1<=sub2:
        clusterAssigment="1"
    else:
        clusterAssigment="2"
    return clusterAssigments
#print(clusterAnalysis(np.array([1.7, 1.6, 1.3, 1.3, 2.8, 1.4,
#2.8, 2.6, 1.6, 2.7])))

**新代码:

import math
def clusterAnalysis(reflectance):
    reflectance=np.arange(reflectance.size)
    evens =reflectance % 2 == 0 # N measurements are even numbers
    clusters = np.ones(reflectance.shape)
    clusters[evens] = 2
    while True:
        mean_even=np.mean(reflectance[evens])
        mean_odd=np.mean(reflectance[np.logical_not(evens)])
        diff_even = np.abs(reflectance - mean_even) 
        diff_odd = np.abs(reflectance - mean_odd)
        new_clusters= np.ones(reflectance.shape)
        new_clusters[diff_even < diff_odd] = 2
        clusterAssigments= new_clusters

        if np.all(clusters == new_clusters):
            break
        clusters = new_clusters
        clusterAssigments= new_clusters
    return clusterAssigments 
print(clusterAnalysis(np.array([1.7, 1.6, 1.3, 1.3, 2.8, 1.4,2.8, 2.6, 1.6, 2.7])))```**



The code above does not work at all. My output should be :`2 2 2 2 1 2 1 1 2 1 `
I have been working on this for last 2 days and I just dont know what I am doing wrong...

Tags: refnewnp步骤集群absmeanclusters
1条回答
网友
1楼 · 发布于 2024-09-30 00:35:17

你的while循环永远不会结束。您从不修改sub1sub2,因此while循环只是旋转和旋转,sub1始终小于sub2

您遇到的问题是,在while循环的每次迭代中,您都需要根据旧集群的平均值计算出新集群,然后检查它们是否已更改

您是对的,您需要一个while循环,您可以使用如下内容:

def clusterAnalysis(reflectance):
    # Work out your odd/even clusters to start it off
    # EDIT: I misread how this bit works - see the update.
    # You don't want to use the size here:
    #     you want to use a list of numbers the size
    # of reflectance [0, 1, 2, ..., n]
    even_numbers = np.arange(reflectance.size) % 2 == 0

    # Now we'll create an array to hold the cluster ids, that's the
    # same shape as reflectance.
    # By default it'll store ones. Then we'll change all the entries
    # for even numbers to hold twos
    clusters = np.ones(reflectance.shape)

    # This uses what's called "logical indexing" - it'll change all the elements
    # that equal "true" in even_numbers
    clusters[even_numbers] = 2

    # This will create a never-ending while loop, but we'll manually
    # end it using 'break'
    while True:
        # Work out the new clusters, by working out the means of the two 
        # clusters and finding the closest to the reflectance numbers
        # Clue: You can use logical indexing here like we did above
        cluster_1_mean = ...
        cluster_2_mean = ...
        new_clusters = ...

        # This is the really important bit - we've created the new array 
        # `new_clusters` above and we want to check if it's the same as 
        # the last one. If it is, we'll use `break` which tells python
        # to stop doing the loop now.
        if np.all(clusters == new_clusters):
            break

        # We'll only reach here if we haven't called 'break' - so we'll update with the new clusters and we can go again
        clusters = new_clusters

    # Once the while loop is finished, we should return the clusters array.
    return clusters

编辑: 我将试着给你一个拼图的片段

假设我有两个20个介于0和200之间的随机数的列表,我想得到奇数和偶数的平均值:

x = np.random.randint(200, size=(20))

# Just like above, we'll get the id of each element in an array [0, 1, ..., 20]
x_ids = np.arange(x.size)
evens = x_ids % 2 == 0

# We'll get the mean of all the even `x`'s
mean_even = np.mean(x[evens])

# And we can do it the other way round for the odds
mean_odd = np.mean(x[np.logical_not(evens)])

另一次编辑:

好的,最后一块拼图:)

所以我们想找出x中哪一个最接近mean_even,哪一个最接近mean_odd。我们可以做一些类似于您最初尝试的事情:

diff_even = np.abs(x - mean_even) 
diff_odd = np.abs(x - mean_odd)

# Start by creating a new clusters array, just like we did before,
# where all the ids are originally set to `1`.    
new_clusters = np.ones(x.shape)

# Now get the numbers closest to `diff_even` and set their cluster to
# two.
# Because we're using less-than, it means that any that are equally
# close to both means will stay in cluster one. If we wanted it to
#  work so they went to cluster 2 by default, we could use `<=`.
new_clusters[diff_even < diff_odd] = 2

您需要更改一些变量名,以使用逻辑索引从clusters数组(而不是evens数组)获取平均值,但这基本上可以满足您的所有需要

请注意,我之前做mean_odd时犯了一个错误。我不小心将布尔数组变成了数字,从而破坏了逻辑索引,这就是为什么我将它更新为使用np.logical_not。如果有兴趣的话,我可以更详细地解释它是如何工作的

相关问题 更多 >

    热门问题