4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PythonでXGBoost

Posted at

公式

pandas型で説明変数/被説明変数、学習/テスト、データを用意する。
x_train, y_train
x_valid, y_valid

モデルを作成して予測値を計算する。

import pandas as pd
import xgboost as xgb

dtrain = xgb.DMatrix(x_train,y_train)
dvalid = xgb.DMatrix(x_valid,y_valid)

params = {
    "objective" : "reg:squarederror",
    "eval_metric" : "rmse"
}

model = xgb.train(
    params = params,
    dtrain = dtrain,
    evals = [(dtrain, "train"), (dvalid, "valid")],
    num_boost_round = 100
)

pred = model.predict(xgb.DMatrix(x_valid))

プロットする。

import matplotlib.pyplot as plt
import numpy as np

plt.scatter(y_valid, pred, alpha = 0.5)
plt.plot(np.linspace(0, 200, 400), np.linspace(0, 200, 400), "red")
plt.show()

誤差を調べる。

from sklearn.metrics import mean_squared_error

mse = mean_squared_error(y_valid, pred)
rmse = np.sqrt(mse)
print(rmse)
4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?