0
0

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.

決定木を使ってScikit-learn Boston Housing Datasetの回帰をしてみる

Last updated at Posted at 2021-04-27

Boston Housing Datasetをロードする。


from sklearn import preprocessing
import pandas as pd
import numpy as np 

from sklearn.datasets import load_boston

boston = load_boston()
X,y = boston.data, boston.target

プロットしてみる。。。


import matplotlib.pyplot as plt

plt.figure(figsize=(24, 8), dpi=50)
plt.subplot(211)
plt.plot(X)
plt.subplot(212)
plt.plot(y)

今回は、下のyの系列に対して回帰をする。。

ダウンロード (27).png

決定木回帰のモジュールをインポートする。。


import numpy as np
from sklearn.tree import DecisionTreeRegressor
import matplotlib.pyplot as plt

【参考】
1次元配列をreshape(-1, 1)とすると、その配列を要素とする2次元1列の配列となる。


a = np.arange(4)
c = a.reshape(-1, 1)
print(c)
 
[[0]
 [1]
 [2]
 [3]]

系列yを整形する。。。


z=np.array(y).reshape(-1, 1)
print(z[0:5])
print(len(z))

回帰をする時系列のx列にあたるものを作る。。。


x = list(range(0, 506, 1))
w=np.array(x).reshape(-1, 1)

print(w[0:5])

決定木で回帰をする。。。


X_test = np.arange(0.0, 506, 1)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)

プロットをする。。


plt.figure(figsize=(24, 8), dpi=50)
plt.scatter(w, y, s=15, edgecolor="black", c="darkorange", label="data")
plt.plot(X_test, y_1, color="cornflowerblue",label="max_depth=2", linewidth=4)
plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=4)
plt.xlabel("data")
plt.ylabel("target")
plt.title("Decision Tree Regression")
plt.show()

ダウンロード (28).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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?