ใน ep นี้ เราจะมาวิเคราะห์ Customer Segment สอนทฤษฎี Machine Learning การแบ่งกลุ่มลูกค้า Customer Segmentation หรือ Market Segmentation หลักการ สร้าง Segment Profile กลุ่มลูกค้า, Customer Profile ด้วย K-Means Clustering ซึ่งเป็น Machine Learning แบบ Unsupervised Learning เป็น Algorithm ที่เรียบง่าย และนิยมใช้ใงาน Customer Relationship Management, Business Model Canvas
Customer Segmentation คืออะไร

Customer Segmentation หรือ Marketing Segmentation คือ การแบ่งกลุ่มลูกค้าเป็นกลุ่มย่อย ที่สมาชิกมีคุณลักษณะคล้าย ๆ กัน Customer Segmentation เป็นเครื่องมีอสำคัญ ที่นักการตลาดใช้ในการระบุความต้องการของกลุ่มลูกค้า ที่ถูกมองข้ามไป ยังไม่ได้รับการตอบสนอง การใช้ข้อมูลเหล่านี้ทำให้องค์กรธุรกิจสามารถเอาชนะคู่แข่ง ด้วยการพัฒนาสินค้า และบริการพิเศษ ที่ตอบสนองลูกค้าเฉพาะกลุ่ม

ตัวอย่างวิธีการ Customer Segmentation ยอดนิยม หลักการวิเคราะห์ ที่ธุรกิจใช้ในการแบ่งกลุ่มลูกค้า Market Segment, Customer Persona, Customer Avatar มีดังนี้
- Customer Segmentation ด้วยสถิติประชากร Demographic เช่น เพศ gender, อายุ age, ลักษณะของครอบครัว familial, กรรมพันธุ์ racial, ชาติพันธุ์ ethnic group และสถานะสมรส marital status, รายได้ income, การศึกษา education, อาชีพ occupation
- Customer Segmentation ด้วยข้อมูลภูมิศาสตร์ Geographical ที่มักแตกต่างกันใป ในแต่ละภาคธุรกิจ บางธุรกิจ อาจจะเน้นเฉพาะพื้นที่ หรือเมืองบางเมือง สำหรับธุรกิจขนาดใหญ่ อาจจะมองภาพรวมทั้งประเทศ หรือทวีป
- Customer Segmentation ด้วยลักษณะจิตวิทยา Psychographics เช่น ฐานะทางสังคม social class, ไลฟ์สไตล์ lifestyle, และ อุปนิสัย บุคลิกภาพ personality traits
- Customer Segmentation ด้วยข้อมูลพฤติกรรม Behavioral data เช่น การนิสัย พฤติกรรมการจับจ่ายใช้สอย การบริโภค spending and consumption habits, การใช้งานสินค้า และบริการ product/service usage, และประโยชน์ใช้สอยที่ได้รับ desired benefits.

เรามาเริ่มกันเลยดีกว่า
ใน ep นี้ เราจะมาวิเคราะห์ Customer Segment สอนทฤษฎี การแบ่งกลุ่มลูกค้า Customer Segmentation หรือ Market Segmentation สร้าง Segment Profile กลุ่มลูกค้า, Customer Profile ด้วย K-Means Clustering ซึ่งเป็น Unsupervised Machine Learning Algorithm ที่เรียบง่าย และนิยมใช้ใงาน Customer Relationship Management, Business Model Canvas
Customer Segmentation คืออะไร¶
Customer Segmentation หรือ Marketing Segmentation คือ การแบ่งกลุ่มลูกค้าเป็นกลุ่มย่อย ที่สมาชิกมีคุณลักษณะคล้าย ๆ กัน Customer Segmentation เป็นเครื่องมีอสำคัญ ที่นักการตลาดใช้ในการระบุความต้องการของกลุ่มลูกค้า ที่ถูกมองข้ามไป ยังไม่ได้รับการตอบสนอง การใช้ข้อมูลเหล่านี้ทำให้องค์กรธุรกิจสามารถเอาชนะคู่แข่ง ด้วยการพัฒนาสินค้า และบริการพิเศษ ที่ตอบสนองลูกค้าเฉพาะกลุ่ม
วิธีการยอดนิยม ที่ธุรกิจใช้ในการแบ่ง Segment กลุ่มลูกค้า คือ
- สถิติประชากร Demographic เช่น เพศ gender, อายุ age, ลักษณะของครอบครัว,กรรมพันธุ์ familial และสถานะสมรส marital status, รายได้ income, การศึกษา education, อาชีพ occupation.ฃ
- ข้อมูลภูมิศาสตร์ Geographical ที่มักแตกต่างกันใป ในแต่ละภาคธุรกิจ บางธุรกิจ อาจจะเน้นเฉพาะพื้นที่ หรือเมืองบางเมือง สำหรับธุรกิจขนาดใหญ่ อาจจะมองภาพรวมทั้งประเทศ หรือทวีป
- ลักษณะจิตวิทยา Psychographics เช่น ฐานะทางสังคม social class, ไลฟ์สไตล์ lifestyle, และ อุปนิสัย บุคลิกภาพ personality traits
- ข้อมูลพฤติกรรม Behavioral data เช่น การนิสัย พฤติกรรมการจับจ่ายใช้สอย การบริโภค spending and consumption habits, การใช้งานสินค้า และบริการ product/service usage, และประโยชน์ใช้สอยที่ได้รับ desired benefits.
0. Install¶
%reload_ext autoreload
%autoreload 2
%matplotlib inline
เราจะต้อง Install kaggle เพื่อ Download Dataset
# ! pip install kaggle --upgrade
# ! pip install plotly --upgrade
1. Import Library¶
Import Libray ที่เราต้องการใช้
import numpy as np
import pandas as pd
from pandas import plotting
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('fivethirtyeight')
import plotly as py
import plotly.graph_objs as go
py.offline.init_notebook_mode(connected = True)
from sklearn.cluster import KMeans
import fastai
import warnings
import os
from pathlib import Path
warnings.filterwarnings("ignore")
2. เตรียม Path สำหรับดาวน์โหลดข้อมูล¶
กำหนด path ของ Config File และ Dataset ว่าจะอยู่ใน Google Drive ถ้าเราใช้ Google Colab หรือ อยู่ใน HOME ถ้าเราใช้ VM ธรรมดา และกำหนด Environment Variable ไปยังโฟลเดอร์ที่เก็บ kaggle.json
ในกรณีใช้ Colab ให้ Mount Google Drive เพื่อดึง Config File มาจาก Google Drive ส่วนตัวของเรา เมื่อเรารัน Cell ด้านล่างจะมีลิงค์ปรากฎขึ้นมาให้เรา Login กด Approve แล้ว Copy Authorization Code มาใส่ในช่องด้านล่าง แล้วกด Enter
dataset = 'vjchoudhary7/customer-segmentation-tutorial-in-python'
# Google Colab
config_path = Path('/content/drive')
data_path_base = Path('/content/datasets/')
data_path = data_path_base/dataset
from google.colab import drive
drive.mount(str(config_path))
os.environ['KAGGLE_CONFIG_DIR'] = f"{config_path}/My Drive/.kaggle"
# # VM
# config_path = Path(os.getenv("HOME"))
# data_path = config_path/"datasets"/dataset
# data_path.mkdir(parents=True, exist_ok=True)
# os.environ['KAGGLE_CONFIG_DIR'] = f"{config_path}/.kaggle"
3. Dataset¶
ในเคสนี้ เราจะ Download ข้อมูล Dataset ที่เกี่ยวข้องทั้งหมดมาเก็บไว้ ซึ่งเป็นไฟลื CSV
Dataset เราจะดึงจาก Kaggle วิธี Download kaggle.json ให้ดูจาก ep ที่แล้ว
เมื่อได้ kaggle.json มาแล้ว ในกรณีใช้ Google Colab ให้นำมาใส่ไว้ในโฟลเดอร์ My Drive/.kaggle ใน Google Drive ของเรา เป็น My Drive/.kaggle/kaggle.json ถ้าใช้ VM ให้ใส่ใน HOME/.kaggle/
สั่งดาวน์โหลด Dataset จาก Kaggle พร้อมทั้ง unzip ไว้ใน data_path
!kaggle datasets download {dataset} -p "{data_path}" --unzip
ls ใน path ดูรายการไฟล์ที่ได้มา
!ls {data_path}
4. Data¶
4.1 Exploratory Data Analysis¶
โหลดไฟล์ CSV ขึ้นมาใส่ Pandas Dataframe แล้วลองวิเคราะห์สำรวจข้อมูล Exploratory Data Analysis มี 200 Row 5 Column
df = pd.read_csv(data_path/'Mall_Customers.csv')
df.shape
ดูข้อมูลตัวอย่าง 5 แถวแรก
df.head()
ดูสถิติข้อมูล
df.describe()
ดูชนิดข้อมูล เป็นตัวเลข ยกเว้น Gender ที่เป็น Object (String)
df.dtypes
เคสนี้ไม่มีข้อมูลขาดหาย ไม่ต้องทำ Fillna
df.isnull().sum()
4.2 Visualization¶
ดู Histogram ของทั้ง 3 Column ที่เป็นตัวเลข¶
ดูการการะจายข้อมูลของทั้ง 3 Column
plt.figure(1 , figsize = (15 , 6))
n = 0
for x in ['Age' , 'Annual Income (k$)' , 'Spending Score (1-100)']:
n += 1
plt.subplot(1 , 3 , n)
plt.subplots_adjust(hspace =0.5 , wspace = 0.5)
sns.distplot(df[x] , bins = 20)
plt.title('Distplot of {}'.format(x))
plt.show()
สัดส่วน แต่ละเพศ¶
นับจำนวนตัวอย่างข้อมูล แยก Gender
labels = ['Female', 'Male']
size = df['Gender'].value_counts()
size
explode = [0, 0.1]
plt.rcParams['figure.figsize'] = (9, 9)
plt.pie(size, explode = explode, labels = labels, shadow = True, autopct = '%.2f%%')
plt.title('Gender', fontsize = 20)
plt.axis('off')
plt.legend()
plt.show()
Andrew Curves¶
Andrew Curves เมื่อคำนึงถึง เพศ Gender
plt.rcParams['figure.figsize'] = (10, 7)
plotting.andrews_curves(df.drop("CustomerID", axis=1), "Gender")
plt.title('Andrew Curves for Gender', fontsize = 20)
plt.show()
พล็อตกราฟความสัมพันธ์ ระหว่าง Age, Annual Income, Spending Score¶
พล็อตข้อมูล และ fit ด้วย Linear Regression โดยยังไม่สนใจเพศ Gender
plt.figure(1 , figsize = (10 , 7))
n = 0
for x in ['Age' , 'Annual Income (k$)' , 'Spending Score (1-100)']:
for y in ['Age' , 'Annual Income (k$)' , 'Spending Score (1-100)']:
n += 1
plt.subplot(3 , 3 , n)
plt.subplots_adjust(hspace = 0.5 , wspace = 0.5)
sns.regplot(x = x , y = y , data = df)
plt.ylabel(y.split()[0]+' '+y.split()[1] if len(y.split()) > 1 else y )
plt.show()
Pairplot Age, Annual Income, Spending Score w.r.t Gender¶
แสดงความสัมพันธ์ Age, Annual Income, Spending Score เมื่อคำนึงถึง เพศ Gender
g = sns.pairplot(df.drop("CustomerID", axis=1), hue="Gender", height=3)
g.fig.suptitle("Pairplot for the Data", y=1.08) # y= some height>1
Scatter Plot Age vs Annual Income with respect to Gender¶
พล็อตความสัมพันธ์ของ อายุ และรายได้ต่อปี เมื่อคำนึงถึง เพศ Gender
plt.figure(1 , figsize = (10 , 6))
for gender in ['Male' , 'Female']:
plt.scatter(x = 'Age' , y = 'Annual Income (k$)' , data = df[df['Gender'] == gender] ,
s = 200 , alpha = 0.5 , label = gender)
plt.xlabel('Age'), plt.ylabel('Annual Income (k$)')
plt.title('Age vs Annual Income w.r.t Gender')
plt.legend()
plt.show()
Scatter Annual Income vs Spending Score with respect to Gender¶
พล็อตความสัมพันธ์ของ รายได้ต่อปี กับ คะแนนการใช้จ่าย เมื่อคำนึงถึง เพศ Gender
plt.figure(1 , figsize = (10 , 6))
for gender in ['Male' , 'Female']:
plt.scatter(x = 'Annual Income (k$)',y = 'Spending Score (1-100)' ,
data = df[df['Gender'] == gender] ,s = 200 , alpha = 0.5 , label = gender)
plt.xlabel('Annual Income (k$)'), plt.ylabel('Spending Score (1-100)')
plt.title('Annual Income vs Spending Score w.r.t Gender')
plt.legend()
plt.show()
Distribution of Age , Annual Income and Spending Score according to Gender¶
ดูการกระจายของ Age, Annual Income และ Spending Score แยกแต่ละเพศ Gender
plt.figure(1 , figsize = (12 , 7))
n = 0
for cols in ['Age' , 'Annual Income (k$)' , 'Spending Score (1-100)']:
n += 1
plt.subplot(1 , 3 , n)
plt.subplots_adjust(hspace = 0.5 , wspace = 0.5)
sns.violinplot(x = cols , y = 'Gender' , data = df , palette = 'vlag')
sns.swarmplot(x = cols , y = 'Gender' , data = df)
plt.ylabel('Gender' if n == 1 else '')
plt.title('Boxplots & Swarmplots' if n == 2 else '')
plt.show()