import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# データの準備(例)
data = {
'hand': [['1m', '2m', '3m', '4m', '5m'], ['1p', '2p', '3p', '4p', '5p']],
'discard': ['6m', '7p'],
'label': [1, 0] # 1: 良い打牌, 0: 悪い打牌
}
# データフレームの作成
df = pd.DataFrame(data)
# 特徴量の抽出(例)
def extract_features(hand, discard):
# 簡単な特徴量の例
features = {
'hand_length': len(hand),
'discard_value': ord(discard[0]) # 簡単な例として、打牌の値をASCIIコードで表現
}
return features
# 特徴量の抽出
X = df.apply(lambda row: extract_features(row['hand'], row['discard']), axis=1)
X = pd.DataFrame(X.tolist())
# ラベルの抽出
y = df['label']
# データの分割
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# モデルのトレーニング
model = RandomForestClassifier()
model.fit(X_train, y_train)
# モデルの評価
y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
# 新しい手牌と打牌の評価
new_hand = ['1m', '2m', '3m', '4m', '5m']
new_discard = '6m'
new_features = extract_features(new_hand, new_discard)
new_features_df = pd.DataFrame([new_features])
prediction = model.predict(new_features_df)
print("Prediction:", prediction)