from sklearn.tree import DecisionTreeClassifier as Ditri
X,z = datasets.make_blobs(n_samples=100,n_features=2,centers=7,cluster_std=1.8,random_state=3)
dt = Ditri()
dt.fit(X,z)
nmesh = 200
mx,my = np.meshgrid(np.linspace(X[:,0].min()-1,X[:,0].max()+1,nmesh),np.linspace(X[:,1].min()-1,X[:,1].max()+1,nmesh))
mX = np.stack([mx.ravel(),my.ravel()],1)
mz = dt.predict(mX).reshape(nmesh,nmesh)
plt.figure(figsize=[4,4])
plt.axes([0.1,0.06,0.88,0.88],aspect=1)
plt.scatter(X[:,0],X[:,1],c=z,edgecolor='k',cmap='rainbow',vmin=0,vmax=6)
plt.contourf(mx,my,mz,alpha=0.7,cmap='rainbow',zorder=0,vmin=0,vmax=6)
plt.show()
print(dt)
DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort=False, random_state=None,
splitter='best')
for i in range(1,8):
dt = Ditri(max_depth=i)
dt.fit(X,z)
nmesh = 200
mx,my = np.meshgrid(np.linspace(X[:,0].min()-1,X[:,0].max()+1,nmesh),np.linspace(X[:,1].min()-1,X[:,1].max()+1,nmesh))
mX = np.stack([mx.ravel(),my.ravel()],1)
mz = dt.predict(mX).reshape(nmesh,nmesh)
plt.figure(figsize=[4,4])
plt.axes([0.1,0.06,0.88,0.88],aspect=1)
plt.scatter(X[:,0],X[:,1],c=z,edgecolor='k',cmap='rainbow',vmin=0,vmax=6)
plt.contourf(mx,my,mz,alpha=0.7,cmap='rainbow',zorder=0,vmin=0,vmax=6)
plt.title(u'ความลึก %d'%i,family='Tahoma')
plt.savefig('dt%02d.png'%i)
plt.close()
np.random.seed(6)
X,z = datasets.make_blobs(n_samples=100,n_features=6,centers=3,cluster_std=2.2)
dt = Ditri()
dt.fit(X,z)
fi = dt.feature_importances_
print(fi) # ได้ [ 0. 0. 0. 0.5049505 0.05657709 0.43847242]
ia = fi.argsort()
plt.scatter(X[:,ia[-1]],X[:,ia[-2]],c=z,edgecolor='#aa8844',cmap='winter')
plt.show()
plt.scatter(X[:,ia[0]],X[:,ia[1]],c=z,edgecolor='#aa8844',cmap='winter')
plt.show()
ติดตามอัปเดตของบล็อกได้ที่แฟนเพจ