数据挖掘目标(客户价值分析)

2023-12-13 10:26:33
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

In?[2]:

data=pd.read_csv(r'../教师文件/air_data.csv')

In?[3]:

data.head()

Out[3]:

Start_timeEnd_timeFareCityAgeFlight_countAvg_discountFlight_mileage
02011/08/182014/03/315860.0.35.0100.97312912560
12011/01/132014/03/315561.0佛山35.0120.57590621223
22012/08/152014/03/311089.0北京33.090.63502519246
32012/10/172014/03/319626.0绍兴县53.070.86857114070
42011/09/042014/03/314473.0上海34.0130.70341917373

In?[4]:

data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 15000 entries, 0 to 14999
Data columns (total 8 columns):
 #   Column          Non-Null Count  Dtype  
---  ------          --------------  -----  
 0   Start_time      15000 non-null  object 
 1   End_time        15000 non-null  object 
 2   Fare            14989 non-null  float64
 3   City            14490 non-null  object 
 4   Age             14907 non-null  float64
 5   Flight_count    15000 non-null  int64  
 6   Avg_discount    15000 non-null  float64
 7   Flight_mileage  15000 non-null  int64  
dtypes: float64(3), int64(2), object(3)
memory usage: 937.6+ KB

In?[5]:

data.describe()

Out[5]:

FareAgeFlight_countAvg_discountFlight_mileage
count14989.00000014907.00000015000.00000015000.00000015000.000000
mean3761.74381242.5695319.0576000.72839112395.706800
std2720.2065799.8073853.9463380.1635503588.357291
min0.00000016.0000002.0000000.1360174040.000000
25%1709.00000035.0000006.0000000.6255259747.000000
50%3580.00000041.0000008.0000000.71332211986.500000
75%5452.00000048.00000011.0000000.80384014654.000000
max36602.000000110.00000047.0000001.50000050758.000000

In?[6]:

data=data[data.Fare.notnull()]

In?[7]:

data=data[data.Fare!=0]

In?[8]:

for index,item in data.iterrows():
    s_year,s_month=item['Start_time'].split('/')[:2]
    e_year,e_month=item['End_time'].split('/')[:2]
    data.loc[index,'Months']=(int(e_year)-int(s_year))*12+(int(e_month)-int(s_month))
data=data.drop(['Start_time','End_time'],axis=1)

In?[9]:

data.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 13279 entries, 0 to 14998
Data columns (total 7 columns):
 #   Column          Non-Null Count  Dtype  
---  ------          --------------  -----  
 0   Fare            13279 non-null  float64
 1   City            12809 non-null  object 
 2   Age             13199 non-null  float64
 3   Flight_count    13279 non-null  int64  
 4   Avg_discount    13279 non-null  float64
 5   Flight_mileage  13279 non-null  int64  
 6   Months          13279 non-null  float64
dtypes: float64(4), int64(2), object(1)
memory usage: 1.3+ MB

In?[10]:

data=data.drop(['City'],axis=1)
data=(data-data.mean(axis=0))/data.std(axis=0)

In?[11]:

data.head()

Out[11]:

FareAgeFlight_countAvg_discountFlight_mileageMonths
00.643204-0.7819590.1917521.5394250.019051-0.616333
10.524036-0.7819590.700041-0.9356252.427818-0.357005
2-1.258303-0.985351-0.062393-0.5672611.878109-1.060895
32.1441621.048561-0.5706810.8879390.438910-1.134989
40.090408-0.8836550.954185-0.1411051.357317-0.653379

In?[12]:

plt.figure(figsize=(10,10))
plt.title("Pearson Correlation of Features",y=1.05,size=15)
sns.heatmap(data.astype(float).corr(),linewidths=0.1,vmax=1,square=True,cmap=plt.cm.viridis,linecolor='white',annot=True)

Out[12]:

<AxesSubplot:title={'center':'Pearson Correlation of Features'}>

In?[13]:

data=data.drop(['Fare','Age'],axis=1)

In?[14]:

from sklearn.cluster import KMeans

In?[15]:

kmeans=KMeans(n_clusters=3).fit(data)

In?[16]:

kmeans.cluster_centers_

Out[16]:

array([[-0.56475974,  0.54131875, -0.70701626, -0.56628176],
       [-0.06513412, -0.03376272, -0.10437466,  1.24214471],
       [ 0.75090493, -0.63663316,  0.95977635, -0.37662422]])

In?[17]:

kmeans.labels_

Out[17]:

array([0, 2, 2, ..., 0, 0, 0])

In?[18]:

from collections import defaultdict

In?[28]:

label_dict=defaultdict(int)

In?[29]:

for label in kmeans.labels_:
    label_dict[label] += 1

In?[30]:

label_dict

Out[30]:

defaultdict(int, {0: 5287, 2: 4287, 1: 3705})

In?[31]:

kmeans.cluster_centers_

Out[31]:

array([[-0.56475974,  0.54131875, -0.70701626, -0.56628176],
       [-0.06513412, -0.03376272, -0.10437466,  1.24214471],
       [ 0.75090493, -0.63663316,  0.95977635, -0.37662422]])

文章来源:https://blog.csdn.net/LiYao1103/article/details/134941402
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。