0

I have read some threads but I can not solve the problem.

I am using this code:

data_new=dataset_circulos[['x_tipif','y_tipif']]

A=cluster.KMeans(n_clusters=2).fit(data_new[['x_tipif','y_tipif']])
predicciones=A.predict(pd.DataFrame(data_new[['x_tipif','y_tipif']]))
data_new.loc[ : , 'predicciones'] = predicciones
centroides=A.cluster_centers_
sns.pairplot(x_vars='x_tipif', y_vars='y_tipif', data=data_new, hue="predicciones")

The application shows me the message below:

C:\Users\USER-PC\Anaconda3\lib\site-packages\pandas\core\indexing.py:376: 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

How can I fix it?

dspencer
  • 4,297
  • 4
  • 22
  • 43

1 Answers1

1

To solve this warning, the line

data_new=dataset_circulos[['x_tipif','y_tipif']]

should instead by written as:

data_new = dataset_circulos[['x_tipif','y_tipif']].copy()

to make an explicit copy of the slice of dataset_circulos.

Given that data_new only contains two columns, 'x_tipif' and 'y_tipif', your later indexing in

A=cluster.KMeans(n_clusters=2).fit(data_new[['x_tipif','y_tipif']])

is redundant. This could be more simply written as

A=cluster.KMeans(n_clusters=2).fit(data_new)
dspencer
  • 4,297
  • 4
  • 22
  • 43