帮我写一个python的波士顿房价k-means聚类分析
时间: 2023-05-31 08:05:39 浏览: 143
以下是一个基本的Python波士顿房价K-means聚类分析的实现:
首先,我们需要导入必要的库和数据集:
```
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import load_boston
boston = load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
```
然后,我们需要选择要使用的特征并对其进行标准化:
```
X = df[['RM', 'LSTAT']]
from sklearn.preprocessing import StandardScaler
X = StandardScaler().fit_transform(X)
```
接下来,我们需要确定要使用的聚类数:
```
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)
```
然后,我们可以使用matplotlib库可视化聚类结果:
```
plt.scatter(X[:,0], X[:,1], c=kmeans.labels_)
plt.xlabel('Average Number of Rooms per Dwelling')
plt.ylabel('% Lower Status of the Population')
plt.title('K-means Clustering (k=3) of Boston Housing Data')
plt.show()
```
完整的代码如下所示:
```
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import load_boston
boston = load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
X = df[['RM', 'LSTAT']]
from sklearn.preprocessing import StandardScaler
X = StandardScaler().fit_transform(X)
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)
plt.scatter(X[:,0], X[:,1], c=kmeans.labels_)
plt.xlabel('Average Number of Rooms per Dwelling')
plt.ylabel('% Lower Status of the Population')
plt.title('K-means Clustering (k=3) of Boston Housing Data')
plt.show()
```
阅读全文