python聚类分析顾客_Python用K-means聚类算法进行客户分群的实现

本文介绍了如何使用Python中的K-means算法对超市顾客数据进行聚类分析,通过数据可视化和手肘法确定最优聚类数,最终得出基于年龄、收入和消费分数的客户群体划分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、背景

1.项目描述

你拥有一个超市(Supermarket Mall)。通过会员卡,你用有一些关于你的客户的基本数据,如客户ID,年龄,性别,年收入和消费分数。

消费分数是根据客户行为和购买数据等定义的参数分配给客户的。

问题陈述:你拥有这个商场。想要了解怎么样的顾客可以很容易地聚集在一起(目标顾客),以便可以给营销团队以灵感并相应地计划策略。

2.数据描述

字段名

描述

CustomerID

客户编号

Gender

性别

Age

年龄

Annual Income (k$)

年收入,单位为千美元

Spending Score (1-100)

消费分数,范围在1~100

二、相关模块

import numpy as np

import pandas as pd

from pandas import plotting

import matplotlib.pyplot as plt

import seaborn as sns

import plotly.graph_objs as go

import plotly.offline as py

from sklearn.cluster import KMeans

import warnings

warnings.filterwarnings('ignore')

三、数据可视化

1.数据读取

io = '.../Mall_Customers.csv'

df = pd.DataFrame(pd.read_csv(io))

# 修改列名

df.rename(columns={'Annual Income (k$)': 'Annual Income', 'Spending Score (1-100)': 'Spending Score'}, inplace=True)

print(df.head())

print(df.describe())

print(df.shape)

print(df.count())

print(df.dtypes)

输出如下。

CustomerID Gender Age Annual Income Spending Score

0 1 Male 19 15 39

1 2 Male 21 15 81

2 3 Female 20 16 6

3 4 Female 23 16 77

4 5 Female 31 17 40

-----------------------------------------------------------------

CustomerID Age Annual Income Spending Score

count 200.000000 200.000000 200.000000 200.000000

mean 100.500000 38.850000 60.560000 50.200000

std 57.879185 13.969007 26.264721 25.823522

min 1.000000 18.000000 15.000000 1.000000

25% 50.750000 28.750000 41.500000 34.750000

50% 100.500000 36.000000 61.500000 50.000000

75% 150.250000 49.000000 78.000000 73.000000

max 200.000000 70.000000 137.000000 99.000000

-----------------------------------------------------------------

(200, 5)

CustomerID 200

Gender 200

Age 200

Annual Income 200

Spending Score 200

dtype: int64

-----------------------------------------------------------------

CustomerID int64

Gender object

Age int64

Annual Income int64

Spending Score int64

dtype: object

2.数据可视化

2.1 平行坐标图

平行坐标图(Parallel coordinates plot)用于多元数据的可视化,将高维数据的各个属性(变量)用一系列相互平行的坐标轴表示, 纵向是属性值,横向是属性类别。

若在某个属性上相同颜色折线较为集中,不同颜色有一定的间距,则说明该属性对于预标签类别判定有较大的帮助。

若某个属性上线

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值