Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Comparing the Trends Across Clusters | Spectral Clustering
Cluster Analysis in Python
course content

Зміст курсу

Cluster Analysis in Python

Cluster Analysis in Python

1. K-Means Algorithm
2. K-Medoids Algorithm
3. Hierarchical Clustering
4. Spectral Clustering

Comparing the Trends Across Clusters

Both cases look correct, or at least there is nothing wrong. Let's compare the dynamics across months for both cases.

You might remember from the previous sections how the algorithms predicted the dynamics. The spectral clustering with 4 clusters will predict the following dynamics.

12345678910111213141516171819202122232425262728
# Import the librarires import pandas as pd import seaborn as sns import matplotlib.pyplot as plt 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/Cities+weather.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.iloc[:,2:14]) # Extract the list of the columns col = list(data.columns[2:14]) col.append('prediction') # Calculate the monthly mean averages for each cluster d = data[col].groupby('prediction').mean().stack().reset_index() # Assign new column names d.columns = ['Group', 'Month', "Temp"] # Visualize the results sns.lineplot(x = 'Month', y = "Temp", hue = 'Group', data = d) plt.show()
copy

Quite an interesting result! The spectral clustering algorithm catches the 'downwards` up to summer dynamics even in the case of 4 clusters. Let's find out what will be the fifth line produced by this algorithm.

Завдання

Table
  1. Import SpectralClustering function from sklearn.cluster.
  2. Create SpectralClustering model named model with 5 clusters and 'nearest_neighbors' affinity.
  3. Fit the 3-14 columns of data to model and predict labels. Save them within 'prediction' column of data.
  4. Calculate the mean for each month within the monthly_data variable:
  • Group the observation of col columns by the 'prediction' column.
  • Calculate the mean within each group.
  • Stack the table.
  • Reset the indices.
  1. Reassign the column names of newly created DataFrame monthly_data to ['Group', 'Month', 'Temp'].
  2. Build seaborn line plot 'Month' vs 'Temp' for each 'Group' value. Display the plot.

Завдання

Table
  1. Import SpectralClustering function from sklearn.cluster.
  2. Create SpectralClustering model named model with 5 clusters and 'nearest_neighbors' affinity.
  3. Fit the 3-14 columns of data to model and predict labels. Save them within 'prediction' column of data.
  4. Calculate the mean for each month within the monthly_data variable:
  • Group the observation of col columns by the 'prediction' column.
  • Calculate the mean within each group.
  • Stack the table.
  • Reset the indices.
  1. Reassign the column names of newly created DataFrame monthly_data to ['Group', 'Month', 'Temp'].
  2. Build seaborn line plot 'Month' vs 'Temp' for each 'Group' value. Display the plot.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 4. Розділ 6
toggle bottom row

Comparing the Trends Across Clusters

Both cases look correct, or at least there is nothing wrong. Let's compare the dynamics across months for both cases.

You might remember from the previous sections how the algorithms predicted the dynamics. The spectral clustering with 4 clusters will predict the following dynamics.

12345678910111213141516171819202122232425262728
# Import the librarires import pandas as pd import seaborn as sns import matplotlib.pyplot as plt 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/Cities+weather.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.iloc[:,2:14]) # Extract the list of the columns col = list(data.columns[2:14]) col.append('prediction') # Calculate the monthly mean averages for each cluster d = data[col].groupby('prediction').mean().stack().reset_index() # Assign new column names d.columns = ['Group', 'Month', "Temp"] # Visualize the results sns.lineplot(x = 'Month', y = "Temp", hue = 'Group', data = d) plt.show()
copy

Quite an interesting result! The spectral clustering algorithm catches the 'downwards` up to summer dynamics even in the case of 4 clusters. Let's find out what will be the fifth line produced by this algorithm.

Завдання

Table
  1. Import SpectralClustering function from sklearn.cluster.
  2. Create SpectralClustering model named model with 5 clusters and 'nearest_neighbors' affinity.
  3. Fit the 3-14 columns of data to model and predict labels. Save them within 'prediction' column of data.
  4. Calculate the mean for each month within the monthly_data variable:
  • Group the observation of col columns by the 'prediction' column.
  • Calculate the mean within each group.
  • Stack the table.
  • Reset the indices.
  1. Reassign the column names of newly created DataFrame monthly_data to ['Group', 'Month', 'Temp'].
  2. Build seaborn line plot 'Month' vs 'Temp' for each 'Group' value. Display the plot.

Завдання

Table
  1. Import SpectralClustering function from sklearn.cluster.
  2. Create SpectralClustering model named model with 5 clusters and 'nearest_neighbors' affinity.
  3. Fit the 3-14 columns of data to model and predict labels. Save them within 'prediction' column of data.
  4. Calculate the mean for each month within the monthly_data variable:
  • Group the observation of col columns by the 'prediction' column.
  • Calculate the mean within each group.
  • Stack the table.
  • Reset the indices.
  1. Reassign the column names of newly created DataFrame monthly_data to ['Group', 'Month', 'Temp'].
  2. Build seaborn line plot 'Month' vs 'Temp' for each 'Group' value. Display the plot.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 4. Розділ 6
toggle bottom row

Comparing the Trends Across Clusters

Both cases look correct, or at least there is nothing wrong. Let's compare the dynamics across months for both cases.

You might remember from the previous sections how the algorithms predicted the dynamics. The spectral clustering with 4 clusters will predict the following dynamics.

12345678910111213141516171819202122232425262728
# Import the librarires import pandas as pd import seaborn as sns import matplotlib.pyplot as plt 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/Cities+weather.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.iloc[:,2:14]) # Extract the list of the columns col = list(data.columns[2:14]) col.append('prediction') # Calculate the monthly mean averages for each cluster d = data[col].groupby('prediction').mean().stack().reset_index() # Assign new column names d.columns = ['Group', 'Month', "Temp"] # Visualize the results sns.lineplot(x = 'Month', y = "Temp", hue = 'Group', data = d) plt.show()
copy

Quite an interesting result! The spectral clustering algorithm catches the 'downwards` up to summer dynamics even in the case of 4 clusters. Let's find out what will be the fifth line produced by this algorithm.

Завдання

Table
  1. Import SpectralClustering function from sklearn.cluster.
  2. Create SpectralClustering model named model with 5 clusters and 'nearest_neighbors' affinity.
  3. Fit the 3-14 columns of data to model and predict labels. Save them within 'prediction' column of data.
  4. Calculate the mean for each month within the monthly_data variable:
  • Group the observation of col columns by the 'prediction' column.
  • Calculate the mean within each group.
  • Stack the table.
  • Reset the indices.
  1. Reassign the column names of newly created DataFrame monthly_data to ['Group', 'Month', 'Temp'].
  2. Build seaborn line plot 'Month' vs 'Temp' for each 'Group' value. Display the plot.

Завдання

Table
  1. Import SpectralClustering function from sklearn.cluster.
  2. Create SpectralClustering model named model with 5 clusters and 'nearest_neighbors' affinity.
  3. Fit the 3-14 columns of data to model and predict labels. Save them within 'prediction' column of data.
  4. Calculate the mean for each month within the monthly_data variable:
  • Group the observation of col columns by the 'prediction' column.
  • Calculate the mean within each group.
  • Stack the table.
  • Reset the indices.
  1. Reassign the column names of newly created DataFrame monthly_data to ['Group', 'Month', 'Temp'].
  2. Build seaborn line plot 'Month' vs 'Temp' for each 'Group' value. Display the plot.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Both cases look correct, or at least there is nothing wrong. Let's compare the dynamics across months for both cases.

You might remember from the previous sections how the algorithms predicted the dynamics. The spectral clustering with 4 clusters will predict the following dynamics.

12345678910111213141516171819202122232425262728
# Import the librarires import pandas as pd import seaborn as sns import matplotlib.pyplot as plt 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/Cities+weather.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.iloc[:,2:14]) # Extract the list of the columns col = list(data.columns[2:14]) col.append('prediction') # Calculate the monthly mean averages for each cluster d = data[col].groupby('prediction').mean().stack().reset_index() # Assign new column names d.columns = ['Group', 'Month', "Temp"] # Visualize the results sns.lineplot(x = 'Month', y = "Temp", hue = 'Group', data = d) plt.show()
copy

Quite an interesting result! The spectral clustering algorithm catches the 'downwards` up to summer dynamics even in the case of 4 clusters. Let's find out what will be the fifth line produced by this algorithm.

Завдання

Table
  1. Import SpectralClustering function from sklearn.cluster.
  2. Create SpectralClustering model named model with 5 clusters and 'nearest_neighbors' affinity.
  3. Fit the 3-14 columns of data to model and predict labels. Save them within 'prediction' column of data.
  4. Calculate the mean for each month within the monthly_data variable:
  • Group the observation of col columns by the 'prediction' column.
  • Calculate the mean within each group.
  • Stack the table.
  • Reset the indices.
  1. Reassign the column names of newly created DataFrame monthly_data to ['Group', 'Month', 'Temp'].
  2. Build seaborn line plot 'Month' vs 'Temp' for each 'Group' value. Display the plot.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 4. Розділ 6
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt