Loop does not work in python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Loop does not work in python (/thread-23675.html) Pages:
1
2
|
RE: Loop does not work in python - sandeep_ganga - Jan-13-2020 Not sure if i missed something, Given a set of N measurements, (r1, r2, . . . , rN ) - reflectance array we will initially assign the odd-numbered measurements to class 1 - find odd numbers, odd mean and the even numbered measurements to class 2. - find even numbers , even mean Update step: Compute the mean value (average) of the measurements within each cluster. - mean of even , mean of odd is calculated Assignment step: Assign each measurement to the cluster with the closest mean value. - if nearest to odd mean set 1 , else 2 In case of a tie,assign the measurement to cluster 1. import numpy as np import math def find_nearest(array, value): array = np.asarray(array) idx = (np.abs(array - value)).argmin() print(f"input value : {value}, near to mean: {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("\ngiven input: ", reflectance) print("total size of array: ", totalsize) print("even array: ",np_even) print("odd array: ",np_odd) print("mean of even class: ",mean_even) print("mean of odd class: ",mean_odd) array=[] clusterAssigments=[] array.append(mean_even) array.append(mean_odd) print("array for passing to find nearest : ",array) for i in range(totalsize): #print(i) if find_nearest(array,reflectance[i]) == mean_odd: 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, 1.1]))) Best regards,Sandeep. GANGA SANDEEP KUMAR RE: Loop does not work in python - Ola92 - Jan-13-2020 Still the same problem.... |