|
import pandas as pd |
|
from sklearn.model_selection import train_test_split, GridSearchCV |
|
from sklearn.svm import SVR |
|
from sklearn.metrics import median_absolute_error |
|
from sklearn.preprocessing import StandardScaler, PolynomialFeatures |
|
|
|
|
|
train_data = pd.read_csv("./input/train.csv") |
|
|
|
|
|
X = train_data.drop(["id", "Hardness"], axis=1) |
|
y = train_data["Hardness"] |
|
|
|
|
|
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42) |
|
|
|
|
|
poly = PolynomialFeatures(degree=2, interaction_only=True, include_bias=False) |
|
X_train_poly = poly.fit_transform(X_train) |
|
X_val_poly = poly.transform(X_val) |
|
|
|
|
|
scaler = StandardScaler() |
|
X_train_scaled = scaler.fit_transform(X_train_poly) |
|
X_val_scaled = scaler.transform(X_val_poly) |
|
|
|
|
|
svr = SVR(kernel="rbf") |
|
|
|
|
|
param_grid = { |
|
"C": [0.1, 0.5, 1, 1.5, 2, 2.5, 3], |
|
"gamma": [0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3], |
|
"epsilon": [0.001, 0.005, 0.01, 0.015, 0.02, 0.025, 0.03], |
|
} |
|
|
|
|
|
grid_search = GridSearchCV( |
|
svr, param_grid, cv=5, scoring="neg_median_absolute_error", verbose=1, n_jobs=-1 |
|
) |
|
|
|
|
|
grid_search.fit(X_train_scaled, y_train) |
|
|
|
|
|
print(f"Best parameters: {grid_search.best_params_}") |
|
|
|
|
|
best_model = grid_search.best_estimator_ |
|
predictions = best_model.predict(X_val_scaled) |
|
|
|
|
|
medae = median_absolute_error(y_val, predictions) |
|
print(f"Median Absolute Error: {medae}") |
|
|
|
|
|
test_data = pd.read_csv("./input/test.csv") |
|
X_test = test_data.drop(["id"], axis=1) |
|
X_test_poly = poly.transform(X_test) |
|
X_test_scaled = scaler.transform(X_test_poly) |
|
test_predictions = best_model.predict(X_test_scaled) |
|
submission = pd.DataFrame({"id": test_data["id"], "Hardness": test_predictions}) |
|
submission.to_csv("./working/submission.csv", index=False) |
|
|