1

I was trying to plot the elbow method chart to decide how many clusters I had to put in my k-means clustering. I was applying the following code:

import seaborn as sns

iris = sns.load_dataset('iris')
iris.columns
data = iris.iloc[:, 0:3] # selecting all lines and 3 columns

sse = {}

for k in range(1, 10):
kmeans = KMeans(n_clusters=k, max_iter=1000).fit(data)
data["clusters"] = kmeans.labels_
sse[k] = kmeans.inertia_ # Inertia: Sum of distances of samples to their closest cluster center

plt.figure()
plt.plot(list(sse.keys()), list(sse.values()))
plt.xlabel("Number of cluster")
plt.ylabel("SSE")
plt.show()

However, if I use more than 3 columns in this code:

data = iris.iloc[:, 0:3]

for example:

data = iris.iloc[:, 0:4]

the code don't run.

In addition, if I use:

data = iris[['sepal_length', 'sepal_width', 'petal_length']]

instead of:

data = iris.iloc[:, 0:3]

the code also don't run.

This two problems result in the following message:

C:\Users...\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy This is separate from the ipykernel package so we can avoid doing imports until C:\Users\ledag\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

Why is this happening?

Leda Grasiele
  • 413
  • 1
  • 6
  • 14
  • 1
    As to the posted warning (note, this is a warning, not an error), [here](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) is a question specifically regarding settingwithcopywarning. When you say "the code also don't run", what do you mean? There is no output? There is an error? – G. Anderson Feb 22 '19 at 22:36
  • Indeed, it is a warning message. Answering you question about the code don't run: yes, there is no output, just the message. And the rest of the code (the plot) do not have an output because it needs the previous code. The plot is an empty chart. – Leda Grasiele Feb 23 '19 at 02:34

0 Answers0