python-numpy

meshgrid

根據坐標向量創建坐標矩陣

1
2
3
4
5
6
7
8
============== ==== ==== ======= ===== ====================
Min Max Mean SD Class Correlation
============== ==== ==== ======= ===== ====================
sepal length: 4.3 7.9 5.84 0.83 0.7826
sepal width: 2.0 4.4 3.05 0.43 -0.4194
petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)
petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)
============== ==== ==== ======= ===== ====================

上述為iris資料庫之描述性統計資料,下面取petal length及petal width使用,分別取最小值及最大值後加減1,並進行meshgrid處理
(0, 7.9, 0.1)
(-0.9, 3.5, 0.1) –> 44個區間(-0.9, -0.8, -0.7,…..3.4)
XX即為[ 0. , 0.1, 0.2, …. 7.6, 7.7, 7.8]共44個
yy即為[-0.9, -0.9, -0.9, …. -0.9, -0.9, -0.9][-0.8, -0.8, -0.8, …. -0.8, -0.8, -0.8]…..[3.4, 3.4, 3.4, …. 3.4, 3.4, 3.4]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from itertools import product
import numpy as np
import matplotlib.pyplot as plt
from sklearn. datasets import load_iris
from sklearn import tree
iris = load_iris()
X = iris.data[:, [2, 3]]
y = iris.target
clf = tree.DecisionTreeClassifier(max_depth=2)
clf.fit(X, y)
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 #加1減1只是為了繪圖時留空白
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1),
np.arange(y_min, y_max, 0.1)) #meshgrid根據坐標向量創建坐標矩陣