0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

streamlitで予測

Posted at

はじめに

前々回?くらいで競艇の予測モデルを作成しました。毎回データ取ってきて予測して〜を繰り返すのもめんどくさいので、streamlitで、ウェブアプリにしました。

streamlitの流れ

$ pip install streamlit

インストール

〇〇.pyを作成後

# 〇〇.py
import streamlit as st

st.title("Hello Streamlit-er 👋")
st.markdown(
    """ 
    This is a playground for you to try Streamlit and have fun. 

    **There's :rainbow[so much] you can build!**
    
    We prepared a few examples for you to get started. Just 
    click on the buttons above and discover what you can do 
    with Streamlit. 
    """
)

if st.button("Send balloons!"):
    st.balloons()

適当に公式ページのコード入れて確認しましょう

streamlit run 〇〇.py

これで起動

https://docs.streamlit.io/develop/api-reference
ここに書き方が載っています

コード

簡単にコード紹介。

import streamlit as st
import pandas as pd
import numpy as np
import requests
from bs4 import BeautifulSoup
import time
from tqdm import tqdm
import datetime
import lightgbm as lgb

# ページ設定
st.set_page_config(
    page_title="競艇予測アプリ",
    page_icon="🏁",
    layout="wide"
)
df = pd.DataFrame()

# タイトル
st.title("🏁 競艇予測アプリ")
st.markdown("---")

bst_12 = lgb.Booster(model_file="../モデル/12lgb_model.txt")
bst_456 = lgb.Booster(model_file="../モデル/456model.txt")

# メインコンテンツ

# データ取得セクション
st.header("📥 ボートレースデータ取得")

# 日付選択
dt = st.date_input("日付を選択してください", datetime.datetime.today())


# データ取得ボタン
if st.button("データを取得"):
  データ取得処理
  

# 予測
pred12 = bst_12.predict(df)
pred456 = bst_456.predict(df)
pred12 = (pred12 > 0.75).astype(int) 
pred456 = (pred456 > 0.75).astype(int) 
df['1,2位予測'] = pred12
df['4,5,6位予測'] = pred456

st.dataframe(
    df[['日','ラウンド','コース',
             "クラス_A1","クラス_A2","クラス_B1","クラス_B2",
             '1,2位予測','4,5,6位予測']],
    width=1000,  
    height=400
)
st.markdown(f"[リンク]({df['URL'][0]})")

スクリーンショット 2025-08-31 14.29.44.png

指定した日付のデータ取得して、予測結果を入れたデータを表示させることに成功しました!

最後に

これを作るのにモデルの保存・読み込み等で少し苦戦しましたが、作れました!
最近reactを使っていたので、日付変更すると再読み込みだったり色々と不便ですが、自分で使う分には大丈夫でしょう。streamlit簡単に作れていいですね!
公式ページにLLMつかったchatbotとかもあったので時間があったら作ってみようと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?