1
resposta

não consegui identificar o motivo do erro

def calcular_wcss(data):
    wcss = []
    for k in range(1,10):
        kmeans = KMeans(n_clusters = k)
        kmeans.fit(X=data)
        data['Clusters']=kmeans.labels_
        wcss.append(kmeans.inertia_)
    return wcss

from sklearn.cluster import KMeans

df_recencia=df_usuario[['Recencia']]
df_recencia.head()

soma_quadrados = calcular_wcss(df_recencia)

Erro :6: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['Clusters']=kmeans.labels_ :6: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['Clusters']=kmeans.labels_ :6: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['Clusters']=kmeans.labels_ :6: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['Clusters']=kmeans.labels_ :6: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['Clusters']=kmeans.labels_ :6: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['Clusters']=kmeans.labels_ :6: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['Clusters']=kmeans.labels_ :6: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['Clusters']=kmeans.labels_ :6: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy data['Clusters']=kmeans.labels_

1 resposta

Olá Luis! Tudo tranquilo?

Sinto muito pela demora em dar um retorno.

O SettingwithCopyWarning é um tipo de warning em que o Pandas detecta que o usuário está fazendo uma alteração em um dado DataFrame e essa alteração refletirá também em um anterior.

Sendo assim, quando fazemos a seleção de df_recencia do dataframe df_usuario da seguinte forma:

df_recencia=df_usuario[['Recencia']]

O pandas cria uma visualização dessa tabela, e não uma tabela em si. O que pode acarretar problemas quando tentarmos fazer alterações em um dos DataFrames.

De todo modo, esse warning não afeta o código, mas caso queira silenciá-lo, basta criar o DataFrame df_recencia da seguinte forma:

df_recencia = df_usuario.loc[:, ['Recencia']]

Dessa forma, fazemos a seleção da coluna Recencia e criamos um DataFrame a partir dela.

Outra forma de fazer seria assim:

df_recencia = df_usuario[['Recencia']].copy()

Dessa outra maneira estamos fazendo uma cópia profunda da coluna Recencia do dataframe original (df_usuario).

Fazendo de qualquer uma dessas duas formas, estaremos evitando o warning e possíveis erros ^^

Caso queira entender um pouco mais a fundo sobre o motivo desse warning, sugiro a leitura desse outro tópico, que explica de forma mais detalhada sobre esse warning :)

Bons estudos!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software