Зміст курсу
Cluster Analysis in Python
Cluster Analysis in Python
Weather Data: Complete and Ward Linkages
The last chart was good, but if you remember the K-Means and K-Medoids algorithms results, you may remember that there was at least one more line that unlike all the others goes downwards close to July. The average linkage in hierarchical clustering didn't catch that dynamic.
We saw that for complete and ward linkages there is sense to consider 4 clusters. Let's find out will they catch that?
Swipe to start coding

- Import
numpy
withnp
alias. - Iterate over the
linkages
list. At each step:
- Create a hierarchical clustering model with 4 clusters and method
j
namedmodel
. - Fit the numerical data of
temp
and predict the labels. Add predicted labels as the'prediction'
column totemp
. - Create a
temp_res
DataFrame with monthly averages for each group. To do it group the values oftemp
by the'prediction'
column, calculate themean
, and then apply the.stack()
method. - Add column
'method'
totemp_res
DataFrame with valuej
being repeated the number of rows intemp_res
times. - Merge
res
andtemp_res
dataframes using.concat
function ofpd
.
- Reassign the column names of
res
to['Group', 'Month', 'Temp', "Method"]
. - Within the
FacetGrid
function set thecol
parameter to'Method'
. This will build a separate chart for each value of the'Method'
column. - Within the
.map
function set theseaborn
line plot function as the first parameter.
Рішення
Дякуємо за ваш відгук!
Weather Data: Complete and Ward Linkages
The last chart was good, but if you remember the K-Means and K-Medoids algorithms results, you may remember that there was at least one more line that unlike all the others goes downwards close to July. The average linkage in hierarchical clustering didn't catch that dynamic.
We saw that for complete and ward linkages there is sense to consider 4 clusters. Let's find out will they catch that?
Swipe to start coding

- Import
numpy
withnp
alias. - Iterate over the
linkages
list. At each step:
- Create a hierarchical clustering model with 4 clusters and method
j
namedmodel
. - Fit the numerical data of
temp
and predict the labels. Add predicted labels as the'prediction'
column totemp
. - Create a
temp_res
DataFrame with monthly averages for each group. To do it group the values oftemp
by the'prediction'
column, calculate themean
, and then apply the.stack()
method. - Add column
'method'
totemp_res
DataFrame with valuej
being repeated the number of rows intemp_res
times. - Merge
res
andtemp_res
dataframes using.concat
function ofpd
.
- Reassign the column names of
res
to['Group', 'Month', 'Temp', "Method"]
. - Within the
FacetGrid
function set thecol
parameter to'Method'
. This will build a separate chart for each value of the'Method'
column. - Within the
.map
function set theseaborn
line plot function as the first parameter.
Рішення
Дякуємо за ваш відгук!