February vs July Average Temperatures
Well, as you remember, there are no 100% correct answers to clustering problems. For the last task you solved it seems like 5 clusters might be a good option.
Let's visualize the results of clustering into 5 groups by building the scatter plot for average February vs July temperatures, which are one of the coldest and hottest months respectively.
Opgave
Swipe to start coding

- Create a
KMeans
model namedmodel
with 5 clusters. - Fit the numerical columns of
data
(2 - 13 indices) tomodel
. - Add the
'prediction'
column to thedata
DataFrame with predicted bymodel
labels. - Build a scatter plot of average
'Feb'
vs'Jul'
temperatures, having each point colored with respect to the'prediction'
column of thedata
DataFrame.
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Import the libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
# 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 KMeans model
model = KMeans(n_clusters = 5)
# Fit the data to model
model.fit(data.iloc[:,2:14])
# Predict the labels
data['prediction'] = model.predict(data.iloc[:,2:14])
# Visualize the results
sns.scatterplot(x = 'Feb', y = 'Jul', hue = 'prediction', data = data)
plt.show()
Var alt klart?
Tak for dine kommentarer!
Sektion 1. Kapitel 7
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Import the libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
# 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 KMeans model
model = ___(___ = ___)
# Fit the data to model
___.___(data.iloc[:,2:14])
# Predict the labels
data['prediction'] = ___.___(data.___[___])
# Visualize the results
sns.scatterplot(x = '___', y = '___', hue = '___', data = ___)
plt.show()
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat