sugitora
Add Car Purchase Simulator files
d9161b8
import gradio as gr
import pandas as pd
import numpy as np
# データの読み込み
data = pd.read_csv("20250221_Car_LogitData.csv")
# 結果を格納するデータフレーム
results_df = pd.DataFrame(columns=["Car_Type", "Condition", "Interest_Rate", "Purchase_Probability", "Expected_Purchase_Amount"])
# ロジット関数の定義
def logistic(x, beta_0, beta_1):
return 1 / (1 + np.exp(-(beta_0 + beta_1 * x)))
# 分析関数
def perform_analysis(car_type, avg_price, interest_rate, cut_rate):
global results_df # 既存の結果に追加するためにグローバル変数を利用
# 入力値の制限チェック
if not (1 <= car_type <= 10):
return "Error: Car_Type must be between 1 and 10."
if interest_rate < 0 or cut_rate < 0:
return "Error: Interest rates must be non-negative."
# CSVから切片と係数を取得
beta_0 = data.iloc[int(car_type) - 1, 9] # 10列目(0-indexなので9)
beta_1 = data.iloc[int(car_type) - 1, 10] # 11列目(0-indexなので10)
# 現在の購入確率と期待購入額
purchase_prob_current = logistic(interest_rate, beta_0, beta_1)
expected_purchase_current = purchase_prob_current * avg_price
# 金利を引き下げた場合の計算(0%未満にならないように制限)
new_interest_rate = max(0, interest_rate - cut_rate)
purchase_prob_new = logistic(new_interest_rate, beta_0, beta_1)
expected_purchase_new = purchase_prob_new * avg_price
# 結果を保存
new_result = pd.DataFrame({
"Car_Type": [car_type, car_type],
"Condition": ["Current", f"After Cut ({cut_rate}%)"],
"Interest_Rate": [interest_rate, new_interest_rate],
"Purchase_Probability": [purchase_prob_current, purchase_prob_new],
"Expected_Purchase_Amount": [expected_purchase_current, expected_purchase_new]
})
results_df = pd.concat([results_df, new_result], ignore_index=True)
return new_result
# CSVダウンロード関数
def download_results():
csv_path = "Car_Purchase_Results.csv"
results_df.to_csv(csv_path, index=False)
return csv_path
# Gradio インターフェースの作成
with gr.Blocks() as demo:
gr.Markdown("### 🚗 車種ごとの金利と購入確率の分析ツール(CSVデータからβ0とβ1を自動取得)")
with gr.Row():
car_type = gr.Slider(label="Car Type (1-10)", minimum=1, maximum=10, step=1, value=1)
avg_price = gr.Number(label="Average Purchase Price", value=2000000)
interest_rate = gr.Number(label="Current Interest Rate (%)", value=3.9, minimum=0)
cut_rate = gr.Number(label="Interest Rate Cut (%)", value=0.5, minimum=0)
output = gr.Dataframe(label="Analysis Result")
analyze_button = gr.Button("Run Analysis")
analyze_button.click(
perform_analysis,
inputs=[car_type, avg_price, interest_rate, cut_rate],
outputs=output
)
download_button = gr.Button("Download Results as CSV")
download_file = gr.File()
download_button.click(download_results, outputs=download_file)
# 実行
demo.launch()