File size: 1,609 Bytes
5cbc1e9 |
1 2 3 4 5 6 7 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 51 52 53 54 55 |
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from math import sqrt
# Load the data
games = pd.read_csv("./input/games.csv")
turns = pd.read_csv("./input/turns.csv")
train = pd.read_csv("./input/train.csv")
# Merge the datasets on game_id
merged_data = pd.merge(train, games, on="game_id")
merged_data = pd.merge(
merged_data,
turns.groupby("game_id").agg({"points": "sum"}).reset_index(),
on="game_id",
)
# Prepare the features and target variable
X = merged_data[["game_duration_seconds", "winner", "points"]]
y = merged_data["rating"]
# Split the data into training and validation sets
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize the model
model = RandomForestRegressor(n_estimators=100, random_state=42)
# Train the model
model.fit(X_train, y_train)
# Predict on the validation set
y_pred = model.predict(X_val)
# Calculate the RMSE
rmse = sqrt(mean_squared_error(y_val, y_pred))
print(f"Validation RMSE: {rmse}")
# Prepare the test set
test = pd.read_csv("./input/test.csv")
test_merged = pd.merge(test, games, on="game_id")
test_merged = pd.merge(
test_merged,
turns.groupby("game_id").agg({"points": "sum"}).reset_index(),
on="game_id",
)
X_test = test_merged[["game_duration_seconds", "winner", "points"]]
# Predict on the test set
test["rating"] = model.predict(X_test)
# Save the predictions to a CSV file
test[["game_id", "rating"]].to_csv("./working/submission.csv", index=False)
|