LoginSignup
0
0

More than 3 years have passed since last update.

Kaggle House Prices ③ ~ 予測・提出 ~

Last updated at Posted at 2020-10-09

以下で作成したモデルを使用してテストデータの予測、及び submission ファイルの提出を行います。
Kaggle House Prices ② ~ モデル作成 ~

ライブラリの読み込み

import numpy as np
from sklearn.externals import joblib

データの読み込み

def load_x_test() -> pd.DataFrame:
    """事前に作成したテストデータの特徴量を読み込む

    :return: テストデータの特徴量
    """
    return joblib.load('test_x.pkl')

def load_model(i_fold):
    """事前に作成したモデルを読み込む

    :return: 対象foldのモデル
    """
    return joblib.load(f'model-{i_fold}.pkl')

def load_pred_test():
    """事前に作成したテストデータの予測結果を読み込む

    :return: テストデータの予測結果
    """
    return joblib.load('pred-test.pkl')

テストデータの予測を実行

# クロスバリデーションで学習した各foldのモデルの平均により、テストデータの予測を行う
test_x = load_x_test()
preds = []
n_fold = 4

# 各foldのモデルで予測を行う
for i_fold in range(n_fold):
    print(f'start prediction fold:{i_fold}')
    model = load_model(i_fold)
    pred = model.predict(test_x)
    preds.append(pred)
    print(f'end prediction fold:{i_fold}')

# 予測の平均値を取得する
pred_avg = np.mean(preds, axis=0)

# 予測結果の保存
joblib.dump(pred_avg, 'pred-test.pkl')

提出用の submission ファイルを作成

pred = load_pred_test()
print(len(pred))
print(load_x_test())
submission = pd.DataFrame(pd.read_csv('test.csv')['Id'])
submission['SalePrice'] = np.exp(pred)
submission.to_csv(
    'submission.csv',
    index=False
)

提出結果

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0