Jan-13-2020, 03:43 PM
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.
Sandeep.
GANGA SANDEEP KUMAR
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])))
Output:C:\Users\testuser\Downloads\DL>python test3.py
given input: [1.7 1.6 1.3 1.3 2.8 1.4 2.8 2.6 1.6 2.7]
total size of array: 10
even array: [1.6 2.8 1.4 2.8 2.6 1.6]
odd array: [1.7 1.3 1.3 2.7]
mean of even class: 2.1333333333333333
mean of odd class: 1.75
array for passing to find nearest : [2.1333333333333333, 1.75]
input value : 1.7, near to mean: 1.75
input value : 1.6, near to mean: 1.75
input value : 1.3, near to mean: 1.75
input value : 1.3, near to mean: 1.75
input value : 2.8, near to mean: 2.1333333333333333
input value : 1.4, near to mean: 1.75
input value : 2.8, near to mean: 2.1333333333333333
input value : 2.6, near to mean: 2.1333333333333333
input value : 1.6, near to mean: 1.75
input value : 2.7, near to mean: 2.1333333333333333
[1 1 1 1 2 1 2 2 1 2]
C:\Users\testuser\Downloads\DL>python test3.py
given input: [1.7 1.6 1.3 1.3 2.8 1.4 2.8 2.6 1.6 2.7 1.1]
total size of array: 11
even array: [1.6 2.8 1.4 2.8 2.6 1.6]
odd array: [1.7 1.3 1.3 2.7 1.1]
mean of even class: 2.1333333333333333
mean of odd class: 1.6199999999999999
array for passing to find nearest : [2.1333333333333333, 1.6199999999999999]
input value : 1.7, near to mean: 1.6199999999999999
input value : 1.6, near to mean: 1.6199999999999999
input value : 1.3, near to mean: 1.6199999999999999
input value : 1.3, near to mean: 1.6199999999999999
input value : 2.8, near to mean: 2.1333333333333333
input value : 1.4, near to mean: 1.6199999999999999
input value : 2.8, near to mean: 2.1333333333333333
input value : 2.6, near to mean: 2.1333333333333333
input value : 1.6, near to mean: 1.6199999999999999
input value : 2.7, near to mean: 2.1333333333333333
input value : 1.1, near to mean: 1.6199999999999999
[1 1 1 1 2 1 2 2 1 2 1]
Best regards,Sandeep.
GANGA SANDEEP KUMAR