Dec-13-2018, 05:41 PM
I have a dataset with locations (coordinates) and a scalar attribute of each location (for example, temperature). I need to cluster the locations based on the scalar attribute, but taking into consideration the distance between locations.
The problem is that, using temperature as an example, it is possible for locations that are far from each other to have the same temperature. If I cluster on temperature, these locations will be in the same cluster when they shouldn't. The opposite is true if two locations that are near each other have different temperatures. In this case, clustering on temperature may result in these observations being in different clusters, while clustering based on a distance matrix would put them in the same one.
So, is there a way in which I could cluster observations giving more importance to one attribute (temperature) and then "refining" based on the distance matrix?
Here is a simple example showing how clustering differs depending on whether an attribute is used as the basis or the distance matrix. My goal is to be able to use both, the attribute and the distance matrix, giving more importance to the attribute.
haversine.py is available here: https://gist.github.com/rochacbruno/2883505
For full disclosure, I posted this question in stackoverflow a couple of days ago but so far I haven't received any feedback.
Thanks
The problem is that, using temperature as an example, it is possible for locations that are far from each other to have the same temperature. If I cluster on temperature, these locations will be in the same cluster when they shouldn't. The opposite is true if two locations that are near each other have different temperatures. In this case, clustering on temperature may result in these observations being in different clusters, while clustering based on a distance matrix would put them in the same one.
So, is there a way in which I could cluster observations giving more importance to one attribute (temperature) and then "refining" based on the distance matrix?
Here is a simple example showing how clustering differs depending on whether an attribute is used as the basis or the distance matrix. My goal is to be able to use both, the attribute and the distance matrix, giving more importance to the attribute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import numpy as np import matplotlib.pyplot as plt import haversine from scipy.cluster.hierarchy import linkage, fcluster from scipy.spatial import distance as ssd # Create location data x = np.random.rand( 100 , 1 ) y = np.random.rand( 100 , 1 ) t = np.random.randint( 0 , 20 , size = ( 100 , 1 )) # Compute distance matrix D = np.zeros(( len (x), len (y))) for k in range ( len (x)): for j in range ( len (y)): distance_pair = haversine.distance((x[k], y[k]), (x[j], y[j])) D[k,j] = distance_pair # Compare clustering alternatives Zt = linkage(t, 'complete' ) Zd = linkage(ssd.squareform(D), method = "complete" ) # Cluster based on t clt = fcluster(Zt, 5 , criterion = 'distance' ).reshape( 100 , 1 ) plt.figure(figsize = ( 10 , 8 )) plt.scatter(x, y, c = clt) plt.show() # Cluster based on distance matrix cld = fcluster(Zd, 10 , criterion = 'distance' ).reshape( 100 , 1 ) plt.figure(figsize = ( 10 , 8 )) plt.scatter(x, y, c = cld) plt.show() |
For full disclosure, I posted this question in stackoverflow a couple of days ago but so far I haven't received any feedback.
Thanks