Jan-13-2020, 10:41 AM
(This post was last modified: Jan-13-2020, 10:41 AM by sandeep_ganga.)
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.
Sandeep
GANGA SANDEEP KUMAR
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