对Customer_Info.csv使用kmeans算法,进行银行客户分组画像,使用Python进行编译
111浏览 · 2024-02-19 08:02:48
好的,以下是一个使用Python实现对Customer_Info.csv进行KMeans聚类分析的示例代码:
```python
import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# 读取数据
df = pd.read_csv('Customer_Info.csv')
# 数据预处理
df = df.dropna() # 删除空值行
df = df[['age', 'income', 'expenses', 'savings']] # 选择相关特征
# KMeans聚类
sse = []
for k in range(1, 11):
kmeans = KMeans(n_clusters=k, random_state=42)
kmeans.fit(df)
sse.append(kmeans.inertia_)
# 肘部法则确定K值
plt.plot(range(1, 11), sse)
plt.title('Elbow Method')
plt.xlabel('Number of Clusters')
plt.ylabel('SSE')
plt.show()
# 选择K=3进行聚类
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(df)
labels = kmeans.labels_
# 绘制散点图
plt.scatter(df['income']