Note by Yoshikawa

Love Technology and Art

勾配ブースティング

from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import r2_score、roc_auc_score,confusion_matrix
from sklearn.metrics import 
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
clf = GradientBoostingClassifier(n_estimators=100)
clf.fit(X_train, y_train)

tkinter ファイル選択ダイアログ

# --------------------------------------------------------
#                         フォルダパス取得
# --------------------------------------------------------
def absdirpath():
    iDir = os.path.abspath(os.path.dirname(__file__))
    root = tkinter.Tk()
    root.withdraw()
    dir = tkinter.filedialog.askdirectory(initialdir=iDir)
    return dir


# --------------------------------------------------------
#                         ファイルパス取得
# --------------------------------------------------------
def absfilepath():
    root = tkinter.Tk()
    root.withdraw()
    fTyp = [("","*")]
    iDir = os.path.abspath(os.path.dirname(__file__))
    file = tkinter.filedialog.askopenfilename(filetypes = fTyp,initialdir = iDir)
    return file

pythonのsklearn

しばらくpythonのsklaernをいじってたのでまとめとく

 

sklaernは、python機械学習ができるライブラリの一つ。
ロジスティック回帰
決定木モデル
ニューラルネット

など、機械学習の色々なモデルを超手軽に構築、学習することができる。

ぶっちゃけ、アルゴリズムの中身はよくわからなくてもできちゃう。

簡単な実装例

import pandas as pd
import matplotlib.pyplot as plt

from sklearn.datasets import load_boston
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score

dataset = load_boston()
x = pd.DataFrame(dataset.data,columns=dataset.feature_names)
y = dataset.target

x_tr,x_ts,y_tr,y_ts=train_test_split(x,y,random_state=0)

est = LinearRegression() # モデルを設定
est.fit(x_tr,y_tr) # パラメータ学習

y_pre = est.predict(x_ts)
y_true = y_ts

score = r2_score(y_true,y_pre)

print('決定係数: ',score)

plt.plot(y_true,color='black')
plt.plot(y_pre,color='red')
plt.title('predict')
plt.grid()
plt.show()

plt.plot(y_true-y_pre)
plt.title('error')
plt.grid()
plt.show()

結果

f:id:yoshikawat64m:20180715174846p:plain:w500

モデルの定義や学習が一行でかける。

ドキュメントこちら
scikit-learn.org