アクセス解析データと
記事は、
結果を
必要な ライブラリの インストール
Mac を
!pip install --upgrade pip !pip install pandas !pip install google2pandas !pip install oauth2client !pip install bokeh !pip install scipy
Google Analytics データの 取得
google2pandas
を
過去にgoogle2pandas
の
from google2pandas import * view_id = '103185238' query = { 'reportRequests': [{ 'viewId' : view_id, 'dateRanges': [{ 'startDate' : '365daysAgo', 'endDate' : 'today'}], 'dimensions' : [ {'name' : 'ga:yearMonth'} ], 'metrics' : [ {'expression' : 'ga:sessions'} ] }] } conn = GoogleAnalyticsQueryV4(secrets='./ga_client.json') df = conn.execute_query(query) # 出力 df['sessions'] = df['sessions'].astype(int) df.sort_values('yearMonth', ascending=False)
yearMonth | sessions | |
---|---|---|
12 | 201902 | 11961 |
11 | 201901 | 17213 |
10 | 201812 | 17098 |
9 | 201811 | 16829 |
8 | 201810 | 16629 |
7 | 201809 | 15633 |
6 | 201808 | 15996 |
5 | 201807 | 15379 |
4 | 201806 | 15615 |
3 | 201805 | 14163 |
2 | 201804 | 11105 |
1 | 201803 | 10444 |
0 | 201802 | 3264 |
Dataframe.plot で 棒グラフを 出力
Pandas の
from scipy import stats import scipy import numpy as np # 信頼区間を求める interval = scipy.stats.poisson.interval(alpha=0.95, mu=df['sessions'], loc=0) # 誤差範囲の計算 yerr は棒グラフの点からの距離になるので、df['sessions'].values との絶対値の差を求める。 interval = np.abs(interval - df['sessions'].values) # kind='bar' で棒グラフを指定、capsize=20 でエラーバーの横線を出力する、 df.plot(figsize=(16, 8), kind='bar', x='yearMonth', y='sessions', yerr=interval,capsize=20)
<matplotlib.axes._subplots.AxesSubplot at 0x138969860>
from bokeh.models import ColumnDataSource, Whisker from bokeh.plotting import figure, show from bokeh.io import output_notebook, show from scipy import stats import scipy import numpy as np # jyupter notebook に描画する output_notebook() # x軸が数値項目ではないので、x_range を指定しておく p = figure(x_range=df['yearMonth'].values, x_axis_label='yearMonth', y_axis_label='sessions', plot_width=900, plot_height=275) # 棒グラフを描画 p.vbar(x=df['yearMonth'].values, width=0.5, bottom=0, top=df['sessions'], color="firebrick",fill_alpha=0.4) # 信頼区間を求める interval = scipy.stats.poisson.interval(alpha=0.95, mu=df['sessions'], loc=0) # エラーバーの描画 source_error = ColumnDataSource(data=dict(base=df['yearMonth'].values, lower=interval[0], upper=interval[1])) p.add_layout( Whisker(source=source_error, base="base", upper="upper", lower="lower") ) show(p)
補足 scipy.stats.poisson.interval
の 計算結果に ついて
python - scipy.stats.poisson.interval - what confidence interval - Cross Validated に、
scipy.stats.poisson.interval
の
R のpoisson.test
と
厳密に
エラーバー 付き棒グラフから 読み取れる 情報
エラーバー
- 基本的に
セッション数は 増加傾向に ある。 - 201804-201805、
201809-201810 の 期間では 有意な 変化が あった。 - 201802、
201902 は データが 中途半端に なっていて、 うまく 描画できていない。
記事の
感覚的には
参考
記事作成中に
- CVR、
新規率、 直帰率と 「誤差の 範囲」(信頼区間 付き折れ線グラフ) - 廿TT - python - Poisson confidence interval with numpy - Stack Overflow
- python - scipy.stats.poisson.interval - what confidence interval - Cross Validated
- Error Bars | plotly
- データ分析に
おける 統計的ゆらぎの 計算方法 (インターネット広告/デジマ向け) - Qiita - エラーバーと
SDと SE - Note of Pediatric Surgery - 二項分布から
Poisson分布の 導出と ベイズの 定理 - Qiita - python - Compute a confidence interval from sample data - Stack Overflow
- ポアソン分布の
信頼区間 – Seto’s Home Page - Styling Visual Attributes — Bokeh 1.0.4 documentation
以上です。
コメント