利用Kmeans聚类进行用户分层分析

一、数据浅析

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import warnings
import seaborn as sns
import re
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler, MinMaxScaler
plt.style.use('ggplot')
warnings.filterwarnings('ignore')
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_csv('超市数据.csv',sep=',',encoding='gbk') # 默认utf-8编码,读取数据出错,gbk编码读取成功
data['性别'] = np.where(data['性别']=='Male',1,0)
data.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 200 entries, 0 to 199
Data columns (total 5 columns):
 #   Column   Non-Null Count  Dtype
---  ------   --------------  -----
 0   用户id     200 non-null    int64
 1   性别       200 non-null    int32
 2   年龄       200 non-null    int64
 3   年收入(k$)  200 non-null    int64
 4   消费得分     200 non-null    int64
dtypes: int32(1), int64(4)
memory usage: 7.2 KB

1.1、数据相关性分析

sns.pairplot(data.iloc[:,2:])
plt.show()

在这里插入图片描述

1.2、年龄、年收入、消费得分直方图分析

fig, ax =plt.subplots(1,3,constrained_layout=True, figsize=(12, 4))
axesSub = sns.distplot(data['年龄'],ax=ax[0
  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值