import pandas as pd datafile='C:/Users/Lenore/Desktop/data/air_data.csv' resultfile='C:/Users/Lenore/Desktop/data/explore.csv' data=pd.read_csv(datafile,encoding='utf-8') explore=data.describe(percentiles=[],include='all').T explore['null']=len(data)-explore['count'] explore=explore[['null','max','min']] explore.columns=[u'空值数',u'最大值',u'最小值'] explore.to_csv(resultfile)
import matplotlib.pyplot as plt from datetime import datetime ffp = data['FFP_DATE'].apply(lambda x:datetime.strptime(x, '%Y/%m/%d')) ffp_year = ffp.map(lambda x : x.year) #绘制各年份会员入会人数直方图 fig = plt.figure(figsize=(8, 5)) #设置画布大小 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False plt.hist(ffp_year, bins='auto', color='#0504aa') plt.xlabel('年份') plt.ylabel('入会人数') plt.title('各年份会员入会人数_3042') plt.show() plt.close() #提取会员不同性别人数 male = pd.value_counts(data['GENDER'])['男'] female = pd.value_counts(data['GENDER'])['女'] #绘制会员性别比例饼图 fig = plt.figure(figsize=(7, 4)) #设置画布大小 plt.pie([male, female], labels=['男', '女'], colors=['lightskyblue', 'lightcoral'], autopct='%1.1f%%') plt.title('会员性别比例_3042') plt.show() plt.close() #提取不同级别会员的人数 lv_four = pd.value_counts(data['FFP_TIER'])[4] lv_five = pd.value_counts(data['FFP_TIER'])[5] lv_six = pd.value_counts(data['FFP_TIER'])[6] #绘制会员各级别人数条形图 fig = plt.figure(figsize=(8,5)) #设置画布大小 plt.bar(range(3), [lv_four, lv_five, lv_six], width=0.4, alpha=0.8, color='skyblue') #left:x轴的位置序列,一般采用arange函数产生一个序列; #height:y轴的数值序列,也就是柱形图的高度,一般就是我们需要展示的数据; #alpha:透明度 #width:为柱形图的宽度,一般这是为0.8即可; #color或facecolor:柱形图填充的颜色; plt.xticks([index for index in range(3)], ['4', '5', '6']) plt.xlabel('会员等级') plt.ylabel('会员人数') plt.title('会员各级别人数_3042') plt.show() plt.close() #提取会员年龄 age = data['AGE'].dropna() age = age.astype('int64') #绘制会员年龄分布箱型图 fig = plt.figure(figsize=(5,10)) plt.boxplot(age, patch_artist=True, labels=['会员年龄'], boxprops={'facecolor': 'lightblue'}) #设置填充颜色 plt.title('会员年龄分布箱型图_3042') #显示y坐标轴的底线 plt.grid(axis='y') plt.show() plt.close()
lte = data['LAST_TO_END'] fc = data['FLIGHT_COUNT'] skc = data['SEG_KM_SUM'] #绘制最后乘机至结束时长箱型图 fig = plt.figure(figsize=(5, 8)) plt.boxplot(lte, patch_artist=True, labels=['时长'], boxprops={'facecolor': 'lightblue'}) plt.title('会员最后乘机至结束时长分布箱型图_3042') plt.grid(axis='y') plt.show() plt.close() #绘制客户飞行次数箱型图 fig = plt.figure(figsize=(5, 8)) plt.boxplot(fc, patch_artist=True, labels=['飞行次数'], boxprops={'facecolor': 'lightblue'}) plt.title('会员飞行次数分布箱型图_3042') plt.grid(axis='y') plt.show() plt.close() #绘制客户总飞行公里数箱型图 fig = plt.figure(figsize=(5, 10)) plt.boxplot(skc, patch_artist=True, labels=['总飞行公里数'], boxprops={'facecolor': 'lightblue'}) plt.title('客户总飞行公里数箱型图_3042') plt.grid(axis='y') plt.show() plt.close()
#积分信息类别 #提取会员积分兑换次数 ec = data['EXCHANGE_COUNT'] #绘制会员兑换积分次数直方图 fig = plt.figure(figsize=(8, 5)) plt.hist(ec, bins=5, color='#0504aa') plt.xlabel('兑换次数') plt.ylabel('会员人数') plt.title('会员兑换积分次数分布直方图_3042') plt.show() plt.close() #提取会员总累计积分 ps = data['Points_Sum'] #绘制会员总累计积分箱型图 fig = plt.figure(figsize=(5, 8)) plt.boxplot(ps, patch_artist=True, labels=['总累计积分'], boxprops={'facecolor': 'lightblue'}) plt.title('客户总累计积分箱型图_3042') plt.grid(axis='y') plt.show() plt.close()
#提取属性并合并为新数据集 data_corr = data[['FFP_TIER', 'FLIGHT_COUNT', 'LAST_TO_END', 'SEG_KM_SUM', 'EXCHANGE_COUNT', 'Points_Sum']] age1 = data['AGE'].fillna(0) data_corr['AGE'] = age1.astype('int64') data_corr['ffp_year'] = ffp_year #计算相关性矩阵 dt_corr = data_corr.corr(method='pearson') print('相关性矩阵_3042:\n', dt_corr) #绘制热力图 import seaborn as sns plt.subplots(figsize=(10, 10)) sns.heatmap(dt_corr, annot=True, vmax=1, square=True, cmap='Blues') plt.title('热力图_3042') plt.show() plt.close
import numpy as np import pandas as pd datafile = 'C:/Users/Lenore/Desktop/data/air_data.csv' #原始数据路径 cleanedfile = 'C:/Users/Lenore/Desktop/data/data_cleaned.csv' #数据清洗后的保存路径 airline_data = pd.read_csv(datafile, encoding='utf-8') print('原始数据的形状_3042:', airline_data.shape) #去除票价为空的记录 airline_notnull = airline_data.loc[airline_data['SUM_YR_1'].notnull() & airline_data['SUM_YR_2'].notnull(),:] print('删除缺失记录后数据的形状为_3042:', airline_notnull.shape) #只保留票价非零的,或者平均折扣率不为0且与总飞行公里数大于0的记录 index1 = airline_notnull['SUM_YR_1'] != 0 index2 = airline_notnull['SUM_YR_2'] != 0 index3 = (airline_notnull['SEG_KM_SUM'] > 0) & (airline_notnull['avg_discount'] != 0) index4 = airline_notnull['AGE'] > 100 #去除年龄大于100的记录 airline = airline_notnull[(index1 | index2) & index3 & -index4] print('经过清洗后的数据的形状为_3042:', airline.shape) airline.to_csv(cleanedfile) #保存清洗后的数据
import pandas as pd import numpy as np #读取清洗后的数据 cleanedfile = 'C:/Users/Lenore/Desktop/data/data_cleaned.csv' #数据清洗后的保存路径 airline = pd.read_csv(cleanedfile, encoding='utf-8') #选取需求属性 airline_selection = airline[['LOAD_TIME','FFP_DATE','LAST_TO_END','FLIGHT_COUNT','SEG_KM_SUM','avg_discount']] print('筛选的属性前5行为_3042:') airline_selection.head()
#构造属性 from datetime import datetime airline_selection['L1'] = pd.to_datetime(airline_selection['LOAD_TIME']) - pd.to_datetime(airline_selection['FFP_DATE']) L = [] for i in airline_selection['L1']: a = int(str(i)[:4])/30 L.append(a) airline_selection['L'] = L airline_selection.drop('L1', axis=1, inplace =True) # 删除中间变量 airline_selection.drop(airline_selection.columns[:2], axis=1, inplace =True) # 去掉不需要的u'LOAD_TIME', u'FFP_DATE' airline_selection.rename(columns={'LAST_TO_END':'R','FLIGHT_COUNT':'F','SEG_KM_SUM':'M','avg_discount':'C'},inplace=True) airline_selection.head() #查看5个指标的取值范围 def f(x): return pd.Series([x.min(),x.max()], index=['min','max']) d = airline_selection.apply(f) # 5个指标的取值范围数据差异较大,为了消除数量级数据带来的影响,需要对数据进行标准化处理 from sklearn.preprocessing import StandardScaler data = StandardScaler().fit_transform(airline_selection) data = pd.DataFrame(data) data.columns = ['Z' + i for i in airline_selection.columns] data =data.iloc[:,[4,0,1,2,3]] # 列进行排序 np.savez('C:/Users/Lenore/Desktop/data/airline_scale.npz',data) data.head()
import pandas as pd import numpy as np from sklearn.cluster import KMeans #读取标准化后的数据 airline_scale = np.load('C:/Users/Lenore/Desktop/data/airline_scale.npz')['arr_0'] k = 5 #确定聚类中心数 #构建模型,随机种子设为123 kmeans_model=KMeans(n_clusters=k,random_state=123) fit_kmeans = kmeans_model.fit(airline_scale) #模型训练 #查看聚类结果 kmeans_cc = kmeans_model.cluster_centers_ #聚类中心 print('各聚类中心为:\n', kmeans_cc) kmeans_labels = kmeans_model.labels_ #样本的类别标签 print('各样本的类别标签为:\n', kmeans_labels) r1 = pd.Series(kmeans_model.labels_).value_counts() #统计不同类别样本的树木 print('最终每个类别的数目为:\n', r1) #输出聚类分群的结果 cluster_center = pd.DataFrame(kmeans_model.cluster_centers_, columns=['ZL', 'ZR', 'ZF', 'ZM', 'ZC']) #将聚类中心放在数据框中 cluster_center.index = pd.DataFrame(kmeans_model.labels_).drop_duplicates().iloc[:, 0] #将样本类别作为数据框索引 print(cluster_center)
%matplotlib inline import matplotlib.pyplot as plt # 客户分群雷达图 labels = ['ZL','ZR','ZF','ZM','ZC'] legen = ['客户群' + str(i + 1) for i in cluster_center.index] # 客户群命名 lstype = ['-','--',(0, (3, 5, 1, 5, 1, 5)),':','-.'] kinds = list(cluster_center.iloc[:, 0]) # 由于雷达图要保证数据闭合,因此再添加L列,并转换为np.ndarray cluster_center = pd.concat([cluster_center, cluster_center[['ZL']]], axis=1) centers = np.array(cluster_center.iloc[:, 0:]) # 分割圆周长,并让其闭合 n = len(labels) angle = np.linspace(0, 2 * np.pi, n, endpoint=False) angle = np.concatenate((angle, [angle[0]])) # 绘图 fig = plt.figure(figsize=(8,6)) ax = fig.add_subplot(111, polar=True) # 以极坐标的形式绘制图形 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 # 画线 for i in range(len(kinds)): ax.plot(angle, centers[i], linestyle=lstype[i], linewidth=2, label=kinds[i]) # 添加属性标签 plt.title('客户特征分析雷达图_3042') plt.legend(legen) plt.show() plt.close
标签:数据分析,plt,pd,实践,selection,airline,第七章,3042,data From: https://www.cnblogs.com/lnxlaila/p/17209147.html