Press "Enter" to skip to content

### 文章目录

 Hi，大家好，这里是丹成学长的毕设系列文章！

 对毕设有任何疑问都可以问学长哦!

####  基于k-means的大数据客户细分

磊学长这里给一个题目综合评分(每项满分5分)

#### 刺 选题指导, 项目分享：

https://blog.csdn.net/Mr_DC_IT/article/details/126460477

## 1 数据分析步骤

1、导入必要的库
2、了解数据
3、数据可视化
4、使用k-means 进行聚类分析

## 2.1 引入库

```import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
plt.style.use('fivethirtyeight')
from sklearn.cluster import KMeans
import plotly as py
import plotly.graph_objects as go
import warnings
import os
warnings.filterwarnings('ignore')```

## 2.2 查看数据

```df=pd.read_csv('/home/kesci/input/1564558710/超市数据.csv')
df.head()```

## 3 kmeans聚类分析

```x1=df[['Age','Spending Score (1-100)']].iloc[:,:].values
from sklearn.cluster import KMeans
inertia=[]
for i in range(1,11):
km=KMeans(n_clusters=i,init='k-means++',max_iter=300,n_init=10,random_state=100)
km.fit(x1)
inertia.append(km.inertia_)
plt.figure(1,figsize=(12,6))
plt.plot(range(1,11),inertia)
plt.title('The Elbow Method',fontsize=20)
plt.xlabel('Number of Clusters')
plt.ylabel('inertia')
plt.show()```

```km = KMeans(n_clusters = 4, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 100)
y_means = km.fit_predict(x1)
plt.figure(1 , figsize = (12 , 6) )
plt.scatter(x1[y_means == 0, 0], x1[y_means == 0, 1], s = 200, c = 'salmon')
plt.scatter(x1[y_means == 1, 0], x1[y_means == 1, 1], s = 200, c = 'yellowgreen')
plt.scatter(x1[y_means == 2, 0], x1[y_means == 2, 1], s = 200, c = 'cornflowerblue')
plt.scatter(x1[y_means == 3, 0], x1[y_means == 3, 1], s = 200, c = 'magenta')
plt.scatter(km.cluster_centers_[:,0], km.cluster_centers_[:, 1], s = 100, c = 'black' , label = 'centeroid')
plt.ylabel('Spending Score (1-100)') , plt.xlabel('Age')
plt.legend()
plt.show()```

#### 刺 选题指导, 项目分享：

https://blog.csdn.net/Mr_DC_IT/article/details/126460477