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
- Import
SpectralClustering
function fromsklearn.cluster
. - Create a
SpectralClustering
model object with 4 clusters and set theaffinity
parameter to'nearest_neighbors'
. - Fit the
data
to themodel
and predict the labels. Save predicted labels as the'prediction'
column ofdata
. - Build the
seaborn
scatter plot with'x'
column ofdata
on the x-axis,'y'
column ofdata
on the y-axis for each value of'prediction'
. Then, display the plot.
Solução
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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?
Obrigado pelo seu feedback!
Seção 4. Capítulo 3
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo