import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
# データの読み込み
df = pd.read_csv("race_data.csv", encoding="shift-jis")
# データの前処理
# 過去10レースの成績を数値化
for i in range(1, 11):
df[f"result_{i}"] = df[f"result_{i}"].apply(lambda x: 1 if "1" in str(x) else 0)
# 入力データと教師データを分割
X = df.drop(["result", "horse_name"], axis=1).values
y = df["result"].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# モデルの構築
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(64, activation="relu", input_shape=(20,)),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(1, activation="sigmoid")
])
# モデルのコンパイル
model.compile(optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"])
# 学習
model.fit(X_train, y_train, epochs=100, validation_data=(X_test, y_test))
# 予測
y_pred = model.predict(X_test)
y_pred = np.where(y_pred >= 0.5, 1, 0)
accuracy = np.mean(y_pred == y_test)
# 精度の評価
print(f"Accuracy: {accuracy}")
|