Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +85 -50
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,50 +1,85 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
st.
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import math
|
| 5 |
+
|
| 6 |
+
# ページ設定
|
| 7 |
+
st.set_page_config(page_title="モンテカルロで円周率を推定", page_icon="🔵", layout="wide")
|
| 8 |
+
|
| 9 |
+
# タイトルと説明
|
| 10 |
+
st.title("🔵 モンテカルロ法で円周率を推定")
|
| 11 |
+
st.markdown("""
|
| 12 |
+
このアプリは、ランダムに点を打って円周率(π)を推定します!
|
| 13 |
+
- 円(半径1)と外接正方形(辺の長さ2)にランダムに点を打ちます。
|
| 14 |
+
- 円の中に入る点の割合からπを近似します。
|
| 15 |
+
- スライダーで点の数を調整して、結果の変化を観察してみてください!
|
| 16 |
+
""")
|
| 17 |
+
|
| 18 |
+
# サイドバーで設定
|
| 19 |
+
st.sidebar.header("設定")
|
| 20 |
+
num_points = st.sidebar.slider("点の数", min_value=100, max_value=100000, value=1000, step=100)
|
| 21 |
+
point_color = st.sidebar.color_picker("点の色", "#FF4B4B")
|
| 22 |
+
show_points = st.sidebar.checkbox("点をプロットする", value=True)
|
| 23 |
+
|
| 24 |
+
# セッション状態で計算トリガーを管理
|
| 25 |
+
if 'run_calc' not in st.session_state:
|
| 26 |
+
st.session_state.run_calc = False
|
| 27 |
+
|
| 28 |
+
# 計算ボタン
|
| 29 |
+
if st.button("計算を実行!"):
|
| 30 |
+
st.session_state.run_calc = True
|
| 31 |
+
|
| 32 |
+
# モンテカルロ計算とプロット
|
| 33 |
+
if st.session_state.run_calc:
|
| 34 |
+
# ランダムに点を生成
|
| 35 |
+
x = np.random.uniform(-1, 1, num_points)
|
| 36 |
+
y = np.random.uniform(-1, 1, num_points)
|
| 37 |
+
|
| 38 |
+
# 円の中か判定(x^2 + y^2 <= 1)
|
| 39 |
+
distances = x**2 + y**2
|
| 40 |
+
inside_circle = distances <= 1
|
| 41 |
+
|
| 42 |
+
# 円周率の推定
|
| 43 |
+
pi_estimate = 4 * np.sum(inside_circle) / num_points
|
| 44 |
+
|
| 45 |
+
# 結果表示
|
| 46 |
+
st.subheader("結果")
|
| 47 |
+
col1, col2 = st.columns(2)
|
| 48 |
+
with col1:
|
| 49 |
+
st.metric("推定された円周率", f"{pi_estimate:.6f}")
|
| 50 |
+
st.metric("実際の円周率との誤差", f"{abs(pi_estimate - math.pi):.6f}")
|
| 51 |
+
with col2:
|
| 52 |
+
st.write(f"**点の数**: {num_points}")
|
| 53 |
+
st.write(f"**円内の点**: {np.sum(inside_circle)}")
|
| 54 |
+
st.write(f"**円外の点**: {num_points - np.sum(inside_circle)}")
|
| 55 |
+
|
| 56 |
+
# プロット
|
| 57 |
+
st.subheader("プロット")
|
| 58 |
+
fig, ax = plt.subplots(figsize=(6, 6))
|
| 59 |
+
|
| 60 |
+
# 円を描画
|
| 61 |
+
circle = plt.Circle((0, 0), 1, edgecolor='blue', facecolor='none', linewidth=2)
|
| 62 |
+
ax.add_patch(circle)
|
| 63 |
+
|
| 64 |
+
# 点をプロット(オプションで非表示)
|
| 65 |
+
if show_points:
|
| 66 |
+
ax.scatter(x[inside_circle], y[inside_circle], c=point_color, s=1, alpha=0.5, label="円内")
|
| 67 |
+
ax.scatter(x[~inside_circle], y[~inside_circle], c="gray", s=1, alpha=0.5, label="円外")
|
| 68 |
+
|
| 69 |
+
# グラフ設定
|
| 70 |
+
ax.set_xlim(-1.5, 1.5)
|
| 71 |
+
ax.set_ylim(-1.5, 1.5)
|
| 72 |
+
ax.set_aspect('equal')
|
| 73 |
+
ax.grid(True, linestyle='--', alpha=0.7)
|
| 74 |
+
ax.legend()
|
| 75 |
+
ax.set_title(f"モンテカルロ法 (点の数: {num_points})")
|
| 76 |
+
|
| 77 |
+
# Streamlitにプロットを表示
|
| 78 |
+
st.pyplot(fig)
|
| 79 |
+
|
| 80 |
+
# 注意書き
|
| 81 |
+
st.markdown("*注*: 点が多いほど推定値は正確になりますが、計算とプロットに時間がかかる場合があります。")
|
| 82 |
+
|
| 83 |
+
# フッター
|
| 84 |
+
st.markdown("---")
|
| 85 |
+
st.markdown("Created with 💙 by Streamlit & Hugging Face Spaces")
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
streamlit==1.24.0
|
| 2 |
-
|
|
|
|
|
|
| 1 |
streamlit==1.24.0
|
| 2 |
+
numpy==1.26.0
|
| 3 |
+
matplotlib==3.7.1
|