1. 程式人生 > >對比分析

對比分析

4.5 比較 一個 ply nan pre har itl 隨著

‘‘‘
【課程1.3】  對比分析

對比分析 → 兩個互相聯系的指標進行比較

絕對數比較(相減) / 相對數比較(相除)
結構分析、比例分析、空間比較分析、動態對比分析

‘‘‘
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
% matplotlib inline
# 1、絕對數比較 → 相減
# 相互對比的指標在量級上不能差別過大
# (1)折線圖比較
# (2)多系列柱狀圖比較

data = pd.DataFrame(np.random.rand(30
,2)*1000, columns = [A_sale,B_sale], index = pd.period_range(20170601,20170630)) print(data.head()) # 創建數據 → 30天內A/B產品的日銷售額 data.plot(kind=line, style = --., alpha = 0.8, figsize = (10,3), title = AB產品銷量對比-折線圖) # 折線圖比較 data.plot(kind
= bar, width = 0.8, alpha = 0.8, figsize = (10,3), title = AB產品銷量對比-柱狀圖) # 多系列柱狀圖比較

  輸出:

                A_sale      B_sale
2017-06-01  334.812619  778.500279
2017-06-02  921.431743  970.734046
2017-06-03  785.444137  481.548456
2017-06-04  359.790330  259.824537
2017-06-05 657.224681 332.818389

技術分享圖片

# 1、絕對數比較 → 相減
# (3)柱狀圖堆疊圖+差值折線圖比較

fig3 = plt.figure(figsize=(10,6))
plt.subplots_adjust(hspace=0.3)
# 創建子圖及間隔設置

ax1 = fig3.add_subplot(2,1,1)  
x = range(len(data))
y1 = data[A_sale]
y2 = -data[B_sale]
plt.bar(x,y1,width = 1,facecolor = yellowgreen)
plt.bar(x,y2,width = 1,facecolor = lightskyblue)
plt.title(AB產品銷量對比-堆疊圖)
plt.grid()
plt.xticks(range(0,30,6))
ax1.set_xticklabels(data.index[::6])
# 創建堆疊圖

ax2 = fig3.add_subplot(2,1,2)  
y3 = data[A_sale]-data[B_sale]
plt.plot(x,y3,--go)
plt.axhline(0,hold=None,color=r,linestyle="--",alpha=0.8)  # 添加y軸參考線
plt.grid()
plt.title(AB產品銷量對比-差值折線)
plt.xticks(range(0,30,6))
ax2.set_xticklabels(data.index[::6])
# 創建差值折線圖

  輸出:

[<matplotlib.text.Text at 0x9faa9b0>,
 <matplotlib.text.Text at 0x9fa6e48>,
 <matplotlib.text.Text at 0x9fed0f0>,
 <matplotlib.text.Text at 0x9fedb38>,
 <matplotlib.text.Text at 0x9ff15c0>]

技術分享圖片

  

# 2、相對數比較 → 相除
# 有聯系的指標綜合計算後的對比,數值為相對數
# 結構分析、比例分析、空間比較分析、動態對比分析、計劃完成度分析
# (1)結構分析
# 在分組基礎上,各組總量指標與總體的總量指標對比,計算出各組數量在總量中所占比重
# 反映總體的內部結構

data = pd.DataFrame({A_sale:np.random.rand(30)*1000,
                    B_sale:np.random.rand(30)*200},
                   index = pd.period_range(20170601,20170630))
print(data.head())
print(------)
# 創建數據 → 30天內A/B產品的日銷售額
# A/B產品銷售額量級不同

data[A_per] = data[A_sale] / data[A_sale].sum()
data[B_per] = data[B_sale] / data[B_sale].sum()
# 計算出每天的營收占比

data[A_per%] = data[A_per].apply(lambda x: %.2f%% % (x*100))
data[B_per%] = data[B_per].apply(lambda x: %.2f%% % (x*100))
# 轉換為百分數
print(data.head())

fig,axes = plt.subplots(2,1,figsize = (10,6),sharex=True)
data[[A_sale,B_sale]].plot(kind=line,style = --.,alpha = 0.8,ax=axes[0])
axes[0].legend(loc = upper right)
data[[A_per,B_per]].plot(kind=line,style = --.,alpha = 0.8,ax=axes[1])
axes[1].legend(loc = upper right)
# 絕對值對比較難看出結構性變化,通過看銷售額占比來看售賣情況的對比

# 同時可以反應“強度” → 兩個性質不同但有一定聯系的總量指標對比,用來說明“強度”、“密度”、“普遍程度”
# 例如:國內生產總值“元/人”,人口密度“人/平方公裏”

  輸出:

                A_sale      B_sale
2017-06-01  688.134982    6.759655
2017-06-02  310.911156  141.978290
2017-06-03  227.496397  123.595400
2017-06-04  905.453084   64.534911
2017-06-05  374.572618  147.550005
------
                A_sale      B_sale     A_per     B_per A_per% B_per%
2017-06-01  688.134982    6.759655  0.045606  0.002227  4.56%  0.22%
2017-06-02  310.911156  141.978290  0.020606  0.046780  2.06%  4.68%
2017-06-03  227.496397  123.595400  0.015077  0.040723  1.51%  4.07%
2017-06-04  905.453084   64.534911  0.060009  0.021263  6.00%  2.13%
2017-06-05  374.572618  147.550005  0.024825  0.048616  2.48%  4.86%

 技術分享圖片

# 2、相對數比較 → 相除
# (2)比例分析
# 在分組的基礎上,將總體不同部分的指標數值進行對比,其相對指標一般稱為“比例相對數”
# 比例相對數 = 總體中某一部分數值 / 總體中另一部分數值 → “基本建設投資額中工業、農業、教育投資的比例”、“男女比例”...

data = pd.DataFrame({consumption:np.random.rand(12)*1000 + 2000,
                    salary:np.random.rand(12)*500 + 5000},
                   index = pd.period_range(2017/1,2017/12,freq = M))
print(data.head())
print(------)
# 創建數據 → 某人一年內的消費、工資薪水情況
# 消費按照2000-3000/月隨機,工資按照5000-5500/月隨機

data[c_s] = data[consumption] / data[salary]
print(data.head())
# 比例相對數 → 消費收入比

data[c_s].plot.area(color = green,alpha = 0.5,ylim = [0.3,0.6],figsize=(8,3),grid=True)
# 創建面積圖表達

  輸出:

         consumption       salary
2017-01  2300.613040  5349.939624
2017-02  2256.167470  5477.291974
2017-03  2356.130582  5366.495609
2017-04  2680.961342  5203.749452
2017-05  2612.676360  5395.189285
------
         consumption       salary       c_s
2017-01  2300.613040  5349.939624  0.430026
2017-02  2256.167470  5477.291974  0.411913
2017-03  2356.130582  5366.495609  0.439045
2017-04  2680.961342  5203.749452  0.515198
2017-05  2612.676360  5395.189285  0.484260

技術分享圖片

# 2、相對數比較 → 相除
# (3)空間比較分析(橫向對比分析)
# 同類現象在同一時間不同空間的指標數值進行對比,反應同類現象在不同空間上的差異程度和現象發展不平衡的狀況
# 空間比較相對數 = 甲空間某一現象的數值 / 乙空間同類現象的數值
# 一個很現實的例子 → 絕對數來看,我國多經濟總量世界第一,但從人均水平來看是另一回事

data = pd.DataFrame({A:np.random.rand(30)*5000,
                    B:np.random.rand(30)*2000,
                    C:np.random.rand(30)*10000,
                    D:np.random.rand(30)*800},
                   index = pd.period_range(20170601,20170630))
print(data.head())
print(------)
# 創建數據 → 30天內A/B/C/D四個產品的銷售情況
# 不同產品的銷售量級不同

data.sum().plot(kind = bar,color = [r,g,b,k], alpha = 0.8, grid = True)
for i,j in zip(range(4),data.sum()):
    plt.text(i-0.25,j+2000,%.2f % j, color = k)
# 通過柱狀圖做橫向比較 → 4個產品的銷售額總量

data[:10].plot(kind = bar,color = [r,g,b,k], alpha = 0.8, grid = True, figsize = (12,4),width = 0.8)
# 多系列柱狀圖,橫向比較前十天4個產品的銷售額

# 關於同比與環比
# 同比 → 產品A在2015.3和2016.3的比較(相鄰時間段的同一時間點)
# 環比 → 產品A在2015.3和2015.4的比較(相鄰時間段的比較)
# 如何界定“相鄰時間段”與“時間點”,決定了是同比還是環比

  輸出:

                     A            B            C           D
2017-06-01  2850.211921  1647.138351  5276.354493  529.747107
2017-06-02  3222.680792   768.466753  6941.803099  284.760211
2017-06-03   727.193796  1732.612257  3311.883561  332.427362
2017-06-04  2582.599603   444.224737  1829.401025  744.340597
2017-06-05  1756.352061  1731.221585  7583.832278  768.792895

技術分享圖片

# 2、相對數比較 → 相除
# (4)動態對比分析(縱向對比分析)
# 同一現象在不同時間上的指標數值進行對比,反應現象的數量隨著時間推移而發展變動的程度及趨勢
# 最基本方法,計算動態相對數 → 發展速度
# 動態相對數(發展速度) = 某一現象的報告期數值 / 同一現象的基期數值
# 基期:用來比較的基礎時期
# 報告期:所要研究的時期,又稱計算期

data = pd.DataFrame({A:np.random.rand(30)*2000+1000},
                   index = pd.period_range(20170601,20170630))
print(data.head())
print(------)
# 創建數據 → 30天內A產品的銷售情況

data[base] = 1000  # 假設基期銷售額為1000,後面每一天都為計算期
data[l_growth] = data[A] - data[base]  # 累計增長量 = 報告期水平 - 固定基期水平
data[z_growth] = data[A] - data.shift(1)[A]  # 逐期增長量 = 報告期水平 - 報告期前一期水平
data[data.isnull()] = 0  # 替換缺失值

data[[l_growth,z_growth]].plot(figsize = (10,4),style = --.,alpha = 0.8)  
plt.axhline(0,hold=None,color=r,linestyle="--",alpha=0.8)  # 添加y軸參考線
plt.legend(loc = lower left)
plt.grid()
# 通過折線圖查看增長量情況

data[lspeed] = data[l_growth] / data[base]  # 定基增長速度
data[zspeed] = data[z_growth] / data.shift(1)[A]  # 環比增長速度
data[[lspeed,zspeed]].plot(figsize = (10,4),style = --.,alpha = 0.8)  
plt.axhline(0,hold=None,color=r,linestyle="--",alpha=0.8)  # 添加y軸參考線
plt.grid()
print(data.head())
print(------)
# 通過折線圖查看發展速度

  輸出:

                     A
2017-06-01  2604.901536
2017-06-02  2387.955402
2017-06-03  1968.693059
2017-06-04  2313.807035
2017-06-05  1441.483332
------
                      A  base     l_growth    z_growth    lspeed    zspeed
2017-06-01  2604.901536  1000  1604.901536    0.000000  1.604902       NaN
2017-06-02  2387.955402  1000  1387.955402 -216.946134  1.387955 -0.083284
2017-06-03  1968.693059  1000   968.693059 -419.262343  0.968693 -0.175574
2017-06-04  2313.807035  1000  1313.807035  345.113976  1.313807  0.175301
2017-06-05  1441.483332  1000   441.483332 -872.323703  0.441483 -0.377008
------

技術分享圖片

對比分析