pythonk-means

3d Plot Python Tuple Index out of Range


Deleted Questions. Thanks for answering


Solution

  • If you print out 'clusters', you will see that there are three values in each dimension.

    print(clusters)

    [(-0.12855869, -0.49862774, -0.18206914), (-2.76121712, 2.90472514, -3.18620993), (0.78315222, -0.4328264, 0.81931112), (-0.38753925, 1.35436192, -0.18206914), (-1.96191743, 0.09194179, -2.18482966), (1.81659021, -0.41859101, 1.82069139)]
    

    You request five c[0], c[1], c[2], c[3], c[4], c[5]

    It would be correct to request three c[0], c[1], c[2]

    Therefore(Tuple Index out of Range). Having dimension three, you turn to the fourth and fifth dimension, which do not exist.

    That is, filling in the data in the loop should look like this:

    for i, c in enumerate(clusters):
        ax.scatter(c[0], c[1], c[2], c=colors[i], label='cluster {}'.format(i))
    

    If my answer helped you, vote (check the box below the upper and lower triangle). enter image description here