Python Forum
Loop does not work in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop does not work in python
#1
It seems that my loop function does not work and I do not understand why..
I get a result 2 and I should get this list of number:2 2 2 2 1 2 1 1 2 1.

My code:

import numpy as np
import math
def clusterAnalysis(reflectance):
ref=np.size(reflectance)
even1=np.any(ref %2==0)
uneven1=np.any(ref%2>0)
even2="2"
uneven2="1"
even3=(np.mean(even1))
uneven3=(np.mean(uneven1))
while ref==even1:
clusterAssigments="2"
if ref==uneven1:
clusterAssigments="1"
else:ref==even1 and ref==uneven3
clusterAssigments="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])))
The assigment:
The clustering algorithm
Given a set of N measurements, (r1, r2, . . . , rN ), we will initially assign the odd-numbered measurements to
class 1 and the even numbered measurements to class 2. Then the following two steps are repeated:
• Update step: Compute the mean value (average) of the measurements within each cluster.
• Assignment step: Assign each measurement to the cluster with the closest mean value. In case of a tie,
assign the measurement to cluster 1.
Repeat the above steps until the cluster assignments do not change. It can not be determined in advance how
many steps will be needed before the clustering assignment stabilizes.
Problem definition
Create a function that takes as an input a vector of reflectance measurements and returns a vector of cluster
assignments computed using the algorithm described above.

Can anyone help me out? Thank you in advance for your help!
Reply
#2
where's your indentation?
not showing up
Reply
#3
Yes, sorry my mistake.

import numpy as np
import math
def clusterAnalysis(reflectance):
    ref=np.size(reflectance)
    even1=np.any(ref %2==0)
    uneven1=np.any(ref%2>0)
    even2="2"
    uneven2="1"
    even3=(np.mean(even1))
    uneven3=(np.mean(uneven1))
    while ref==even1:
        clusterAssigments=even2
    if ref==uneven1:
        clusterAssigments=uneven1
    else:ref==even1 and ref==uneven3
    clusterAssigments=even2
    return clusterAssigments

Please..anyone? I have been trying to figure this out for last 2 days and changed the code multiple times...
Reply
#4
Yes indeed. The function will always return "2". Look sharp at your code:
    even2="2"
    ...
    clusterAssigments=even2
    return clusterAssigments
You should write comment lines (starting with "#") in your code for explanation, because I cannot understand what you are trying to achieve. But I can see there are problems with the following part:
    while ref==even1:
        clusterAssigments=even2
        if ref==uneven1:
            clusterAssigments=uneven1
        else:ref==even1 and ref==uneven3
In the while you compare an integer (ref) with a bool (even1). I checked it by placing print statements before the while loop.
    print("ref=" + str(ref))
    print("even1=" + str(even1))
Output:
ref=10 even1=True
So I can tell you: the loop will not execute. And if it would execute, how would it end? The line else:ref==even1 and ref==uneven3 is of course also not correct. You execute a boolean expression and do not assign it to anything.
I hope this helps you.
Reply
#5
Can you give me any tip how to change it?
Reply
#6
It seems that I misunderstood the step : Assignment step: Assign each measurement to the cluster with the closest mean value. In case of a tie,assign the measurement to cluster 1 in the assigment. I changed my code now but it does not give me any output right now...
My code:
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])))
Can anybody help me please? I am struggling very much with this assigment....
Reply
#7
Can you try below to see if that helps,


import numpy as np
import math


def find_nearest(array, value):
    array = np.asarray(array)
    idx = (np.abs(array - value)).argmin()
    return array[idx]
	

def clusterAnalysis(reflectance):
	totalsize=np.size(reflectance)# total size
	evenarray = []
	oddarray =[]	
	for i in range(totalsize):
		if (reflectance[i]*10)%2 ==0:      #reading upto first decimal value and checking for even number, but this goes wrong like if given 3.0, it thinks as even while it is actually odd.
			evenarray.append(reflectance[i])
		else:
			oddarray.append(reflectance[i])
	np_even = np.array(evenarray)
	np_odd = np.array(oddarray)
	mean_even=np.mean(np_even)#average of even numbers in cluster 2
	mean_odd=np.mean(np_odd)# average of odd numbers in cluster 1
	#print(totalsize)
	#print(np_even)
	#print(np_odd)
	#print(mean_even)
	#print(mean_odd)
	array=[]
	clusterAssigments=[]
	
	array.append(mean_even)
	array.append(mean_odd)
	#print(array)
	for i in range(totalsize):
		if find_nearest(array,reflectance[i]) == mean_even:
			clusterAssigments.append(1)
		else:
			clusterAssigments.append(2)
	clusterAssigment=np.array(clusterAssigments)
	return clusterAssigment
		 
print(clusterAnalysis(np.array([1.7, 1.6, 1.3, 1.3, 2.8, 1.4,2.8, 2.6, 1.6, 2.7])))
Output:
python test2.py [2 2 2 2 1 2 1 1 2 1]
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
Reply
#8
Yes! it actually works for the
print(clusterAnalysis(np.array([1.7, 1.6, 1.3, 1.3, 2.8, 1.4,2.8, 2.6, 1.6, 2.7])))
However, if I put it into the checking software( the one that check the assigments) and the software uses list of 11 figures in the array( instead of 10 like I mentioned orginally), then isnt the line in code:
if (reflectance[i]*10)%2 ==0:     
 #reading upto first decimal value and checking for even number, but this goes wrong like if given 3.0, it thinks as even while it is actually odd.evenarray.append(reflectance[i])
wrong?
Reply
#9
It works good for any number of elements we pass, since we have for loop added to run till range of input given "totalsize=np.size(reflectance)
for i in range(totalsize):"

And the statement, "if (reflectance[i]*10)%2 ==0:" is to read the input element upto first decimal as a number and check for even or odd, Also you can think of considering round or int(reflectance[i]) to check if even or odd as per requirement.


import numpy as np
import math


def find_nearest(array, value):
	array = np.asarray(array)
	idx = (np.abs(array - value)).argmin()
	#print(array[idx])
	return array[idx]
	

def clusterAnalysis(reflectance):
	totalsize=np.size(reflectance)# total size
	evenarray = []
	oddarray =[]	
	for i in range(totalsize):
		if ((reflectance[i]*10)%2 ==0):
		#if ((reflectance[i]*10)%2 ==0) and (reflectance[i] %2==0):      
			evenarray.append(reflectance[i])
		else:
			oddarray.append(reflectance[i])
	np_even = np.array(evenarray)
	np_odd = np.array(oddarray)
	mean_even=np.mean(np_even)#average of even numbers in cluster 2
	mean_odd=np.mean(np_odd)# average of odd numbers in cluster 1
	#print(totalsize)
	#print(np_even)
	#print(np_odd)
	#print(mean_even)
	#print(mean_odd)
	array=[]
	clusterAssigments=[]
	
	array.append(mean_even)
	array.append(mean_odd)
	#print(array)
	for i in range(totalsize):
		if find_nearest(array,reflectance[i]) == mean_even:
			clusterAssigments.append(1)
		else:
			clusterAssigments.append(2)
	clusterAssigment=np.array(clusterAssigments)
	
	return clusterAssigment
		 
print(clusterAnalysis(np.array([1.7, 1.6, 1.3, 1.3, 2.8, 1.4,2.8, 2.6, 1.6, 2.7,3.1])))
Output:
python test2.py [2 2 2 2 1 2 1 1 2 1 1]
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
Reply
#10
I ran it again and for
import numpy as np
print(clusterAnalysis(np.array([1.7, 1.6, 1.3, 1.3, 2.8, 1.4, 2.8, 2.6, 1.6, 2.7, 1.1])))
The output of the code is
[2 2 2 2 1 2 1 1 2 1 2]
but it should be
[1 1 1 1 2 1 2 2 1 2 1]
I dont understand it...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issues with while loop, while (var != 0): does not work, but while (var !=''): works danilo 2 2,054 Apr-15-2019, 12:08 AM
Last Post: danilo
  Hi, my loop yes/no part doesn't work and I really need help fordxman5 2 2,606 Feb-14-2018, 11:38 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020