In [1]검증용 학습·평가
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
drop_cols = ['vehicle_id', 'listing_date', 'resale_price_krw']
cat_cols = ['manufacturer', 'model_family', 'trim_level', 'fuel_type',
'transmission', 'body_type', 'region', 'seller_type', 'color']
feature_cols = [c for c in train_proc.columns if c not in drop_cols]
num_cols = [c for c in feature_cols if c not in cat_cols]
preprocessor = ColumnTransformer([
('num', StandardScaler(), num_cols),
('cat', OneHotEncoder(handle_unknown='ignore'), cat_cols),
])
model = Pipeline([('prep', preprocessor), ('lr', LinearRegression())])
X_fit, y_fit = fit_df[feature_cols], np.log1p(fit_df['resale_price_krw'])
X_valid, y_valid = valid_df[feature_cols], np.log1p(valid_df['resale_price_krw'])
model.fit(X_fit, y_fit)
pred_log = np.clip(model.predict(X_valid), 0, None)
print('Validation RMSLE:', round(np.sqrt(mean_squared_error(y_valid, pred_log)), 4))
pred_price = np.expm1(pred_log)
true_price = valid_df['resale_price_krw']
print('Validation MAE :', round(mean_absolute_error(true_price, pred_price)))
print('Validation RMSE:', round(np.sqrt(mean_squared_error(true_price, pred_price))))
print('Validation R2 :', round(r2_score(true_price, pred_price), 4))
Out[1] 출력 보기
Validation RMSLE: 0.0603 Validation MAE : 1158777 Validation RMSE: 1721547 Validation R2 : 0.9885