知识点补充
证明:
函数可以看成n 个优化函数之和
2:CART 分类算法
3:CART 回归算法
决策树算法代码实践
实验一:sklearn自带鸢尾花数据决策树分类
基本流程:
1:数据导入 from sklearn.data import load_iris
2:数据预处理 3类花4中特征 特征两两一组分开
3:每一组做一次并画出分类结果图,对某一组画出决策树分支图
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier,plot_tree
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
if __name__=='__main__':
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
iris_feature='花萼长度','花萼宽度','花瓣长度','花瓣宽度'
path='..\\..\\..\\..\\..\\iris.data'
data=pd.read_csv(path,header=None)
x=data[list(range(4))]
y=LabelEncoder().fit_transform(data[4])
x_train_pre,x_test_pre,y_train,y_test = train_test_split(x,y,train_size=0.7,random_state=0)
plt.figure(figsize=(6,6),facecolor='w')
for Indexpair,pair in enumerate([[0,1],[0,2],[0,2],[1,2],[1,3],[2,3]]):
x_train = x_train_pre[pair]
x_test = x_test_pre[pair]
model=DecisionTreeClassifier(max_depth=6,min_samples_leaf=3,criterion='entropy')
model=model.fit(x_train,y_train)
# 画图
N,M=500,500
x1_min,x2_min=x_train.min()
x1_max, x2_max = x_train.max()
t1=np.linspace(x1_min,x1_max,N)
t2 = np.linspace(x2_min, x2_max, N)
# 生成网格采样点
x1,x2=np.meshgrid(t1,t2)