ドバイデータを使ったPythonの計算例
下記の方法だと、過去のインデックス値の計算が反映されていないので不正確
売買履歴データベース
Marsa Dubai
https://scrapbox.io/files/643fdc78c60d24001b6378d8.png
info
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 606422 entries, 2004-05-05 to 2023-02-21
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 area 606422 non-null object
1 building_name_en 606422 non-null object
2 master_project_en 556734 non-null object
3 trans_group_en 606422 non-null object
4 procedure_name_en 606422 non-null object
5 rooms_en 606257 non-null object
6 procedure_area 606422 non-null float64
7 actual_worth 606382 non-null float64
8 meter_sale_price 606422 non-null float64
dtypes: float64(3), object(6)
Describe
procedure_area actual_worth meter_sale_price
count 606422.000000 6.063820e+05 6.064220e+05
mean 105.815961 1.368034e+06 1.511742e+04
std 103.559517 1.835846e+06 1.416898e+05
min 0.090000 1.000000e+00 1.000000e-02
25% 62.922500 5.548000e+05 7.696380e+03
50% 86.590000 9.308950e+05 1.059285e+04
75% 129.720000 1.604000e+06 1.538988e+04
max 35117.330000 9.986817e+07 2.181199e+07
毎日繰り返す手順
code: python
import pandas as pd
from datetime import timedelta
import statsmodels.api as sm
from sklearn.linear_model import LinearRegression
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.feature_selection import RFE
import numpy as np
import matplotlib.pyplot as plt
pd.set_option('display.max_columns', None)
import warnings
warnings.filterwarnings('ignore')
percentile_df_sale = percentile_df_sale.sort_values(by='date')
import datetime
# 過去90日前の日付を計算
end_date = percentile_df_sale.index.max()
start_date = end_date - datetime.timedelta(days=90)
# 過去90日間のデータをフィルタリング
# エリアごとに取引量を集計
transaction_count = filtered_df.groupby('area').size()
# 動的ウィンドウの期間を算出
max_window = 30
min_window = 7
# エリアごとの取引量を正規化(0から1の範囲にする)
normalized_count = (transaction_count - transaction_count.min()) / (transaction_count.max() - transaction_count.min())
# エリアごとのウィンドウ期間を決定する
dynamic_window = max_window - (max_window - min_window) * normalized_count
dynamic_window = np.round(dynamic_window).astype(int)
# エリア名を設定
area_name = "Marsa Dubai"
# エリアの減衰係数 lambda_ を設定(直近90日間の取引量の標準化の値を使う)
area = "Marsa Dubai"
lambda_ = normalized_countarea # 特定エリアのデータフレームを作成
area_df = percentile_df_sale[percentile_df_sale'area' == area_name] # 同じ日付のインデックスでグループ化し、meter_sale_priceの中央値を計算
# 中央値を新しいデータフレームに変換
area_df_median = area_df_median.to_frame()
# area_df_medianデータフレームのインデックスをリセット
area_df_median.reset_index(inplace=True)
# dateをインデックスに設定
area_df_median.set_index('date', inplace=True)
# 日付を連続するように補完
area_df_median = area_df_median.resample('D').asfreq()
# カラム meter_sale_price を補完
# ローリングウィンドウを適用して smoothed_price を計算
# 重みを計算
W_ti = 1 / lambda_
W_si = 1 - W_ti
# 最新のタイムリーデータを取得(ここではすでにデータがまーじされている前提で最新の日付の行を取得)
# 指数加重移動平均を計算
# インデックスを算出
# 直近7日間でのrollingして、最終的なインデックスにする
rolling_window = 7
グラフを表示
code: python
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# 表示する日付の範囲を指定
start_date = '2012-01-01'
end_date = '2022-02-20'
# グラフの大きさを設定
fig, ax = plt.subplots(figsize=(16, 8))
# データをプロット
ax.plot(area_df_median.index, area_df_median'rolling_index', linewidth=1, color='royalblue') # x軸のメモリを年月で表示
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_minor_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
# グラフのx軸の範囲を設定
ax.set_xlim(pd.to_datetime(start_date), pd.to_datetime(end_date))
# グラフのタイトルとラベルを設定
ax.set_title('Rolling Index (7-day window)')
ax.set_xlabel('Date')
ax.set_ylabel('Rolling Index')
# グラフを表示
plt.show()
上記の方法で、