-1

write python code to add a list into a list, as follows:

mbk = MiniBatchKMeans(n_clusters=cluster_number).fit(normal_one_dataset_matrix)
labels = mbk.labels_

miniBatchKMeans_cluster_centers = mbk.cluster_centers_

center_list = []
center_list[0] = miniBatchKMeans_cluster_centers

the miniBatchKMeans_cluster_centers is like:

[[  1.35976987e-01   3.62686053e-02   1.57226220e-02   1.55101081e-01
    9.22227057e-02   4.69291492e-03   6.31832469e-01   1.18909881e-01
    3.98820502e-02   1.31951911e-01   7.74235425e-02]
 [  6.77025243e-02   4.98708544e-02   1.35180194e-02   1.87703505e-01
    8.38242136e-02   5.39580583e-03   2.70955680e-01   2.70501184e-01
    6.28647087e-02   2.05018029e-01   1.90661340e-01]
 [  2.28943182e-02   1.18150568e-02   3.38147727e-03   3.76551136e-02
    3.63664773e-03   3.86363636e-04   5.12753977e-02   3.60461932e-02
    0.00000000e+00   8.47871989e-01   6.48064205e-02]]

when running, it errors:

File "F:/MyDocument/F/My Document/Training/Python/PyCharmProject/FaceBookCrawl/FB_group_user_stability.py", line 47, in minibatchkmeansClustering_no_gender
center_list[0] = miniBatchKMeans_cluster_centers
IndexError: list assignment index out of range

could you please tell me the reason and how to solve it

bin
  • 51
  • 2
  • 7
  • 1
    `center_list` is empty. You cannot index an empty list. Maybe you could `append` into `center_list` but im not sure exactly what you're trying to achieve. – roganjosh Nov 08 '17 at 06:39

3 Answers3

2

You have empty list here

center_list = []

What you can do is appending to that list as follows.

center_list.append(miniBatchKMeans_cluster_centers)
Mitiku
  • 5,337
  • 3
  • 18
  • 35
1

This should do it -

center_list.append(miniBatchKMeans_cluster_centers)

This is the standard way of adding elements in the list.

Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42
0

When you are trying to assign a value to center_list[0], it gives you an IndexError because center_list is empty, it doesn't have a 0th. item.

You could do like this instead:

center_list = [miniBatchKMeans_cluster_centers]
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80