特定の座標(x,y)の点が赤(ラベル値0)、青(ラベル値1)のどちらに分類されるのかを求めたいです。
問題:(x,y)=(5.5,4.0)の点は赤、青のどちらに分類されるか?
すみません。初めて質問させていただきます。
#必要なライブラリの読み込み。 %matplotlib inline import matplotlib.pyplot as plt import numpy as np #分類対象となる平面内に分布した点を生成。 L = 10 # 領域の一辺の長さ N = 50 # 点の数 B = L/2 # 市松模様の境界値 np.random.seed(1234) X = np.random.rand(N, 2) * L y = [] for i in range(N): x_, y_ = X[i, :] if x_ < B and y_ < B or x_ > B and y_ > B: y.append(1) else: y.append(0) colors = ['blue' if yi == 1 else 'red' for yi in y] plt.scatter(X[:, 0], X[:, 1], c=colors) #決定木モデルにより上のデータを学習し、決定木を可視化。 from sklearn.tree import DecisionTreeClassifier from sklearn.tree import plot_tree model = DecisionTreeClassifier(random_state=20) model.fit(X, y) plt.figure(figsize=(12, 6)) plot_tree(model, class_names=["red", "blue"], feature_names=['x', 'y'], impurity=False, filled=True, fontsize=16);
どのようなルール(コード)で分類するのか分からないと回答得られないかと思います。
分類対象となる平面内に分布した点を生成。
L = 10 # 領域の一辺の長さ
N = 50 # 点の数
B = L/2 # 市松模様の境界値
np.random.seed(1234)
X = np.random.rand(N, 2) * L
y = []
for i in range(N):
x_, y_ = X[i, :]
if x_ < B and y_ < B or x_ > B and y_ > B:
y.append(1)
else:
y.append(0)
colors = ['blue' if yi == 1 else 'red' for yi in y]
plt.scatter(X[:, 0], X[:, 1], c=colors)
決定木モデルにより上のデータを学習し、決定木を可視化。
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import plot_tree
model = DecisionTreeClassifier(random_state=20)
model.fit(X, y)
plt.figure(figsize=(12, 6))
plot_tree(model, class_names=["red", "blue"], feature_names=['x', 'y'],
impurity=False, filled=True, fontsize=16);
説明が不十分でした。このようなコードを書きました。
コードはこの欄ではなく質問本文に、インデントが消えないようにコードプロックで囲んで記載ください。
すみません。慣れていないものでして
回答1件
あなたの回答
tips
プレビュー