公開日:2019-08-15
最終更新日:2019-08-26
最終更新日:2019-08-26
skl03-0:準備
次のcurry.csvは第1章で用いたBobのカレーに対する評価履歴データである.以下のデータをdataディレクトリに配置したうえで,次のコードを実行しよう.
curry.csv:カレーに対する評価履歴データ(データIDid,辛さspicy {0-100},とろみthickness {0-100},評価値rating {0=嫌い, 1=どちらでもない, 2=好き})
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
>>> import numpy as np >>> import pandas as pd >>> >>> # データの読込み >>> curry = pd.read_csv('data/curry.csv', index_col=0) >>> >>> feature_names = np.array(curry.columns[:-1]) >>> target_names = ['dislike', 'neutral', 'like'] >>> >>> curry_X = np.array(curry[feature_names]) >>> curry_y = np.array(curry['rating']) >>> >>> # 全データをランダムに学習データ80%とテストデータ20%に分割 >>> n = len(curry_X) >>> m = int(n * 0.2) >>> np.random.seed(0) >>> indices = np.random.permutation(n) >>> curry_X_train = curry_X[indices[:-m]] >>> curry_y_train = curry_y[indices[:-m]] >>> curry_X_test = curry_X[indices[-m:]] >>> curry_y_test = curry_y[indices[-m:]] |
skl03-1:
LogisticRegressionのインポートLogisticRegressionをインポートしよう.
難易度:★
| ミッション | 説明 |
|---|---|
| 1 | LogisticRegressionをインポートする. |
skl03-2:
LogisticRegressionオブジェクトの生成LogisticRegressionオブジェクトを生成しよう.ここで,パラメタはsolver='lbfgs', C=1e5, multi_class='multinomial'とする.
難易度:★★
| ミッション | 説明 |
|---|---|
| 1 | LogisticRegression()コンストラクタを呼び出す. |
| 2 | solverパラメタを指定する. |
| 3 | Cパラメタを指定する. |
| 4 | multi_classパラメタを指定する. |
| 5 | 生成したLogisticRegressionオブジェクトをlogとする. |
skl03-3:学習
学習データを基にlogにより学習しよう.
難易度:★★
| ミッション | 説明 |
|---|---|
| 1 | LogisticRegression.fit()メソッドを使う. |
skl03-4:予測
logによりテストデータに対してラベルを予測しよう.
難易度:★★
| ミッション | 説明 |
|---|---|
| 1 | LogisticRegression.predict()メソッドを使う. |
skl03-5:予測精度の取得
logのテストデータに対する予測精度を取得しよう.
難易度:★
| ミッション | 説明 |
|---|---|
| 1 | LogisticRegression.score()メソッドを使う. |
skl03-6:学習モデルの可視化
次のコードはlogによる学習モデルを可視化するものである.次のコードをskl03_plt.pyというファイル名で保存し,python3コマンドで実行しよう.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap from sklearn.linear_model import LogisticRegression # data curry = pd.read_csv('data/curry.csv', index_col=0) feature_names = np.array(curry.columns[:-1]) target_names = ['dislike', 'neutral', 'like'] curry_X = np.array(curry[feature_names]) curry_y = np.array(curry['rating']) # train log = LogisticRegression(solver='lbfgs', C=1e5, multi_class='multinomial') log.fit(curry_X, curry_y) # plot cmap_light = ListedColormap(['#CCCCFF', '#CCFFCC', '#FFCCCC']) cmap_dark = ListedColormap(['#8888FF', '#88FF88', '#FF8888']) x_min = 0 x_max = 100 y_min = 0 y_max = 100 xx, yy = np.mgrid[x_min:x_max:200j, y_min:y_max:200j] Z = log.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.pcolormesh(xx, yy, Z, cmap=cmap_light) plt.scatter(curry_X[:, 0], curry_X[:, 1], c=curry_y, cmap=cmap_dark, edgecolors='k') plt.title("curry") plt.xlabel('spicy') plt.ylabel('thickness') plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.show() |
難易度:★
| ミッション | 説明 |
|---|---|
| 1 | python3コマンドでskl03_plt.pyを実行する. |