Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Setting Parameters: Affinity | Spectral Clustering
Cluster Analysis in Python

book
Setting Parameters: Affinity

Well, that was not the result we were looking for. Can we improve it? Can we make the clustering algorithm learn to differ such structures?

The answer is yes - we need to set some parameters within the SpectralClustering function. The parameter we should change is affinity. This parameter defines how should affinity matrix be built (the math explanation of this is outside the scope of this course). By default, the parameter's value is 'rbf'. If we want to differ the clusters with such a structure as in the previous chapter, we should consider the 'nearest_neighbors' value of the parameter.

Tarefa

Swipe to start coding

  1. Import SpectralClustering function from sklearn.cluster.
  2. Create a SpectralClustering model object with 4 clusters and set the affinity parameter to 'nearest_neighbors'.
  3. Fit the data to the model and predict the labels. Save predicted labels as the 'prediction' column of data.
  4. Build the seaborn scatter plot with 'x' column of data on the x-axis, 'y' column of data on the y-axis for each value of 'prediction'. Then, display the plot.

Solução

# Import the libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import SpectralClustering

# Read the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/138ab9ad-aa37-4310-873f-0f62abafb038/model_data4.csv', index_col = 0)

# Create the model
model = SpectralClustering(n_clusters = 4, affinity = 'nearest_neighbors')

# Fit the data and predict the labels
data['prediction'] = model.fit_predict(data)

# Visualize the results
sns.scatterplot(x = 'x', y = 'y', hue = 'prediction', data = data)
plt.show()

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3
# Import the libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
___

# Read the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/138ab9ad-aa37-4310-873f-0f62abafb038/model_data4.csv', index_col = 0)

# Create the model
model = ___(___ = ___, ___ = '___')

# Fit the data and predict the labels
data['prediction'] = ___.___(___)

# Visualize the results
sns.___(x = '___', y = '___', hue = '___', data = ___)
___.___()

Pergunte à IA

expand
ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt