I have a Matrix XY_C where XY_C0 and XY_C1 represent X and Y represents my data points and XY_C2 the color of the group to which they belong...
print(XY_C)
#[['-0.9006811702978088' '1.019004351971607' '#890007']
# ['-1.1430169111851105' '-0.13197947932162468' '#890007']
# ['-1.3853526520724133' '0.32841405319566835' '#890007']
# ['-1.5065205225160652' '0.09821728693702184' '#890007']
# ['-1.0218490407414595' '1.2492011182302534' '#890007']
# ['-0.537177558966854' '1.939791417006192' '#35893b']
# ['-1.5065205225160652' '0.7888075857129604' '#890007']
# ['-1.0218490407414595' '0.7888075857129604' '#890007']
# ['-1.7488562634033669' '-0.3621762455802712' '#890007']
# ['-1.1430169111851105' '0.09821728693702184' '#890007']
# ['-0.537177558966854' '1.4793978844889' '#35893b']
# ['-1.2641847816287624' '0.7888075857129604' '#890007']
# ['-1.2641847816287624' '-0.13197947932162468' '#890007']
# ['-1.870024133847019' '-0.13197947932162468' '#890007']
# ['-0.05250607719224957' '2.1699881832648384' '#35893b']
# ['-0.1736739476359004' '3.090775248299425' '#35893b']
# ['-0.537177558966854' '1.939791417006192' '#35893b']
# ['-0.9006811702978088' '1.019004351971607' '#890007']
# ['-0.1736739476359004' '1.7095946507475455' '#35893b']
# ['-0.9006811702978088' '1.7095946507475455' '#35893b']
# ['-0.537177558966854' '0.7888075857129604' '#90177d']
# ['-0.9006811702978088' '1.4793978844889' '#890007']
# ['-1.5065205225160652' '1.2492011182302534' '#890007']
# ['-0.9006811702978088' '0.5586108194543139' '#890007']
# ['-1.2641847816287624' '0.7888075857129604' '#890007']
# ['-1.0218490407414595' '-0.13197947932162468' '#890007']
# ['-1.0218490407414595' '0.7888075857129604' '#890007']
# ['-0.7795132998541568' '1.019004351971607' '#890007']
# ['-0.7795132998541568' '0.7888075857129604' '#890007']
# ['-1.3853526520724133' '0.32841405319566835' '#890007']
# ['-1.2641847816287624' '0.09821728693702184' '#890007']
# ['-0.537177558966854' '0.7888075857129604' '#90177d']
# ['-0.7795132998541568' '2.400184949523484' '#35893b']
# ['-0.4160096885232032' '2.6303817157821316' '#35893b']
# ['-1.1430169111851105' '0.09821728693702184' '#890007']
# ['-1.0218490407414595' '0.32841405319566835' '#890007']
# ['-0.4160096885232032' '1.019004351971607' '#35893b']
# ['-1.1430169111851105' '1.2492011182302534' '#890007']
# ['-1.7488562634033669' '-0.13197947932162468' '#890007']
# ['-0.9006811702978088' '0.7888075857129604' '#890007']
# ['-1.0218490407414595' '1.019004351971607' '#890007']
# ['-1.6276883929597161' '-1.7433568431321493' '#890007']
# ['-1.7488562634033669' '0.32841405319566835' '#890007']
# ['-1.0218490407414595' '1.019004351971607' '#890007']
# ['-0.9006811702978088' '1.7095946507475455' '#35893b']
# ['-1.2641847816287624' '-0.13197947932162468' '#890007']
# ['-0.9006811702978088' '1.7095946507475455' '#35893b']
# ['-1.5065205225160652' '0.32841405319566835' '#890007']
# ['-0.658345429410506' '1.4793978844889' '#35893b']
# ['-1.0218490407414595' '0.5586108194543139' '#890007']
# ['1.401508368131566' '0.32841405319566835' '#35893b']
# ['0.6745011454696588' '0.32841405319566835' '#35893b']
# ['1.2803404976879151' '0.09821728693702184' '#35893b']
# ['-0.4160096885232032' '-1.7433568431321493' '#90177d']
# ['0.7956690159133096' '-0.5923730118389178' '#90177d']
# ['-0.1736739476359004' '-0.5923730118389178' '#90177d']
# ['0.5533332750260068' '0.5586108194543139' '#35893b']
# ['-1.1430169111851105' '-1.513160076873503' '#890007']
# ['0.9168368863569605' '-0.3621762455802712' '#90177d']
# ['-0.7795132998541568' '-0.8225697780975633' '#890007']
# ['-1.0218490407414595' '-2.433947141908088' '#890007']
# ['0.06866179325140237' '-0.13197947932162468' '#90177d']
# ['0.18982966369505322' '-1.9735536093907948' '#90177d']
# ['0.31099753413870407' '-0.3621762455802712' '#90177d']
# ['-0.29484181807955234' '-0.3621762455802712' '#90177d']
# ['1.0380047568006125' '0.09821728693702184' '#35893b']
# ['-0.29484181807955234' '-0.13197947932162468' '#90177d']
# ['-0.05250607719224957' '-0.8225697780975633' '#90177d']
# ['0.432165404582356' '-1.9735536093907948' '#90177d']
# ['-0.29484181807955234' '-1.2829633106148564' '#90177d']
# ['0.06866179325140237' '0.32841405319566835' '#90177d']
# ['0.31099753413870407' '-0.5923730118389178' '#90177d']
# ['0.5533332750260068' '-1.2829633106148564' '#90177d']
# ['0.31099753413870407' '-0.5923730118389178' '#90177d']
# ['0.6745011454696588' '-0.3621762455802712' '#90177d']
# ['0.9168368863569605' '-0.13197947932162468' '#35893b']
# ['1.1591726272442633' '-0.5923730118389178' '#90177d']
# ['1.0380047568006125' '-0.13197947932162468' '#35893b']
# ['0.18982966369505322' '-0.3621762455802712' '#90177d']
# ['-0.1736739476359004' '-1.0527665443562098' '#90177d']
# ['-0.4160096885232032' '-1.513160076873503' '#90177d']
# ['-0.4160096885232032' '-1.513160076873503' '#90177d']
# ['-0.05250607719224957' '-0.8225697780975633' '#90177d']
# ['0.18982966369505322' '-0.8225697780975633' '#90177d']
# ['-0.537177558966854' '-0.13197947932162468' '#90177d']
# ['0.18982966369505322' '0.7888075857129604' '#35893b']
# ['1.0380047568006125' '0.09821728693702184' '#35893b']
# ['0.5533332750260068' '-1.7433568431321493' '#90177d']
# ['-0.29484181807955234' '-0.13197947932162468' '#90177d']
# ['-0.4160096885232032' '-1.2829633106148564' '#90177d']
# ['-0.4160096885232032' '-1.0527665443562098' '#90177d']
# ['0.31099753413870407' '-0.13197947932162468' '#90177d']
# ['-0.05250607719224957' '-1.0527665443562098' '#90177d']
# ['-1.0218490407414595' '-1.7433568431321493' '#890007']
# ['-0.29484181807955234' '-0.8225697780975633' '#90177d']
# ['-0.1736739476359004' '-0.13197947932162468' '#90177d']
# ['-0.1736739476359004' '-0.3621762455802712' '#90177d']
# ['0.432165404582356' '-0.3621762455802712' '#90177d']
# ['-0.9006811702978088' '-1.2829633106148564' '#890007']
# ['-0.1736739476359004' '-0.5923730118389178' '#90177d']
# ['0.5533332750260068' '0.5586108194543139' '#35893b']
# ['-0.05250607719224957' '-0.8225697780975633' '#90177d']
# ['1.522676238575217' '-0.13197947932162468' '#35893b']
# ['0.5533332750260068' '-0.3621762455802712' '#90177d']
# ['0.7956690159133096' '-0.13197947932162468' '#90177d']
# ['2.1285155907934734' '-0.13197947932162468' '#35893b']
# ['-1.1430169111851105' '-1.2829633106148564' '#890007']
# ['1.7650119794625196' '-0.3621762455802712' '#35893b']
# ['1.0380047568006125' '-1.2829633106148564' '#90177d']
# ['1.643844109018869' '1.2492011182302534' '#35893b']
# ['0.7956690159133096' '0.32841405319566835' '#35893b']
# ['0.6745011454696588' '-0.8225697780975633' '#90177d']
# ['1.1591726272442633' '-0.13197947932162468' '#35893b']
# ['-0.1736739476359004' '-1.2829633106148564' '#90177d']
# ['-0.05250607719224957' '-0.5923730118389178' '#90177d']
# ['0.6745011454696588' '0.32841405319566835' '#35893b']
# ['0.7956690159133096' '-0.13197947932162468' '#90177d']
# ['2.2496834612371255' '1.7095946507475455' '#35893b']
# ['2.2496834612371255' '-1.0527665443562098' '#35893b']
# ['0.18982966369505322' '-1.9735536093907948' '#90177d']
# ['1.2803404976879151' '0.32841405319566835' '#35893b']
# ['-0.29484181807955234' '-0.5923730118389178' '#90177d']
# ['2.2496834612371255' '-0.5923730118389178' '#35893b']
# ['0.5533332750260068' '-0.8225697780975633' '#90177d']
# ['1.0380047568006125' '0.5586108194543139' '#35893b']
# ['1.643844109018869' '0.32841405319566835' '#35893b']
# ['0.432165404582356' '-0.5923730118389178' '#90177d']
# ['0.31099753413870407' '-0.13197947932162468' '#90177d']
# ['0.6745011454696588' '-0.5923730118389178' '#90177d']
# ['1.643844109018869' '-0.13197947932162468' '#35893b']
# ['1.8861798499061717' '-0.5923730118389178' '#35893b']
# ['2.4920192021244283' '1.7095946507475455' '#35893b']
# ['0.6745011454696588' '-0.5923730118389178' '#90177d']
# ['0.5533332750260068' '-0.5923730118389178' '#90177d']
# ['0.31099753413870407' '-1.0527665443562098' '#90177d']
# ['2.2496834612371255' '-0.13197947932162468' '#35893b']
# ['0.5533332750260068' '0.7888075857129604' '#35893b']
# ['0.6745011454696588' '0.09821728693702184' '#35893b']
# ['0.18982966369505322' '-0.13197947932162468' '#90177d']
# ['1.2803404976879151' '0.09821728693702184' '#35893b']
# ['1.0380047568006125' '0.09821728693702184' '#35893b']
# ['1.2803404976879151' '0.09821728693702184' '#35893b']
# ['-0.05250607719224957' '-0.8225697780975633' '#90177d']
# ['1.1591726272442633' '0.32841405319566835' '#35893b']
# ['1.0380047568006125' '0.5586108194543139' '#35893b']
# ['1.0380047568006125' '-0.13197947932162468' '#35893b']
# ['0.5533332750260068' '-1.2829633106148564' '#90177d']
# ['0.7956690159133096' '-0.13197947932162468' '#90177d']
# ['0.432165404582356' '0.7888075857129604' '#35893b']
# ['0.06866179325140237' '-0.13197947932162468' '#90177d']]
# Here I'm plotting my centroids and my XY_C
# K is the number of centroids. (and x1 and x2 are XY but in two vectors).
ic = [np.random.choice(x1.shape[0]) for i in range(k)]
centr = np.array([x1[ic, 0], x2[ic, 0]]).T
obtenerColor = lambda: "#%06x" % random.randint(0, 0xFFFFFF)
colors = [ obtenerColor() for x in range(k) ]
for c in range(k):
plt.plot(centr[c, 0], centr[c, 1], c=colors[c], marker="X", markersize=15)
plt.scatter( XY_C[:, 0].astype('float64'), XY_C[:, 1].astype('float64'), c=XY_C[:, 2] )
plt.show()
So I got this one.
Every time I executed my code is positioning my centroids into a random (X,Y). That's how it works K-means.
Ok, now what I need is calculate their new position, and as far as I know this would be done by calculating the mean of each (X, Y) for each group of colors. So let's go.
lst = []
for i in range(k):
mean_ = XY_C[ XY_C[:, 2] == colors[i]][:, 0:2]
lst.append(np.mean(mean_.astype('float'), axis=0))
centr_means = np.column_stack(lst).T
for c in range(k):
plt.plot(centr_means[c, 0], centr_means[c, 1], c=colors[c], marker="X", markersize=15)
Looking at what centr_means is saving, I'd say I was sucesfull...
print(centr_means)
# [[-0.78442551 -0.13820101]
# [ 1.77926702 0.16592222]
# [ 0.47118557 0.12552877]]
But for some weird reason, once I plot the new positions of my centroids, I got always weird things like these ones...
And it makes little to nothing of sense... What's doing wrong?
I hope I was clear with my question and sorry for my bad English, thanks.


