Beispiel #1
0
                                                    test_size=0.3,
                                                    random_state=1,
                                                    stratify=y)

# ジニ不純度を指標とする決定気のインスタンスを生成
tree_model = DecisionTreeClassifier(criterion='gini',
                                    max_depth=4,
                                    random_state=1)

# 決定木のモデルを訓練データに適合させる
tree_model.fit(X_train, y_train)
X_combined = np.vstack((X_train, X_test))
y_combined = np.hstack((y_train, y_test))

plot_decision_regions2(X_combined,
                       y_combined,
                       classifier=tree_model,
                       test_idx=range(105, 150))
plt.xlabel('petal length [cm]')
plt.ylabel('petal width [cm]')
plt.legend(loc='upper left')
plt.tight_layout()
plt.show()

# 訓練後の決定木モデルの可視化
tree.plot_tree(tree_model)
dot_data = export_graphviz(tree_model,
                           filled=True,
                           rounded=True,
                           class_names=['Setosa', 'Versicolor', 'Virginica'],
                           feature_names=['petal length', 'petal width'],
                           out_file=None)
Beispiel #2
0
sc = StandardScaler()
sc.fit(X_train)
# 平均と標準偏差を用いて標準化
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)
# 訓練データとテストデータの結合
X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined = np.hstack((y_train, y_test))


# ロジスティック回帰のインスタンスを生成
lr = LogisticRegression(C=100.0, random_state=1, solver='lbfgs', multi_class='ovr')
lr.fit(X_train_std, y_train)

# 決定領域をプロット
plot_decision_regions2(X_combined_std, y_combined, classifier=lr,
    test_idx=range(105, 150))
plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized]')
plt.legend(loc='upper left')
plt.tight_layout()
plt.show()


# テストデータセットのデータ点の所属確率
pre = lr.predict_proba(X_test_std[:3, :])
print('所属確率:', pre)
print('確率の和:', pre.sum(axis=1))
# クラスラベルの予測値の取得
print('所属ラベル:', pre.argmax(axis=1))
print('所属ラベル:', lr.predict(X_test_std[:3, :]))
# 単一データ点のクラスラベル予測