(1)原理:决策树根据样本数据集的数据特征对数据集进行划分,直到针对所有特征都划分过,或者划分的数据子集的所有数据的类别标签相同。
(2)代码实现:
#1、调用包和方法
from sklearn.datasets import load_iris
from sklearn import tree #调用树算法模型
import graphviz #调用可视化模块
import pydotplus #调用图形界面模块
from sklearn.model_selection import train_test_split #调用样本划分方法
from sklearn2pmml import sklearn2pmml, PMMLPipeline #调用生成pmml文件方法
#2、导入数据
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 40) # 划分训练集和验证集
#3、训练、验证模型
clf = tree.DecisionTreeClassifier(criterion = 'gini', splitter = 'best', max_depth = None, min_samples_split = 2, min_samples_leaf = 1, min_weight_fraction_leaf = 0.0, max_features = None, random_state = 10, max_leaf_nodes = None, min_impurity_decrease = 0.0, min_impurity_split = None, class_weight = None, presort = False)
clf.fit(X_train, y_train)
print("训练集:", clf.score(X_train, y_train)) #训练集
print("验证集:", clf.score(X_test, y_test)) #验证集
#4、生成PMML文件
pipeline = PMMLPipeline([("classifier", clf)])
pipeline.fit(X_train, y_train)
sklearn2pmml(pipeline, r"C:/Users/马青坡/Desktop/data/dt_iris.pmml")
#5、生成本地pdf文件
dot_data = tree.export_graphviz(clf) #将决策树以pdf格式可视化
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf("C:/Users/马青坡/Desktop/data/decisiontree.pdf") #文件保存到本地
(1)原理:逻辑回归是一种线性分类器,通过logistic函数,将特征映射成一个概率值,来判断输入数据的类别。
(2)代码实现:
#1、调用包和方法
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression as lr #调用逻辑回归算法模型
from sklearn.model_selection import train_test_split #调用样本划分方法
#2、导入数据
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 40) # 划分训练集和验证集
#3、训练、验证模型
clf = lr(penalty = 'l2', dual = False, tol = 0.0001, C = 1.0, fit_intercept = True, intercept_scaling = 1, class_weight = None, random_state = None, solver = 'lbfgs', max_iter = 100, multi_class = 'auto', verbose = 0, warm_start = False, n_jobs = None, l1_ratio = None)
clf.fit(X_train, y_train)
print("训练集:", clf.score(X_train, y_train)) #训练集
print("验证集:", clf.score(X_test, y_test)) #验证集
(1)原理:寻找一个能够正确划分训练数据集并且几何间隔最大的分离超平面。
(2)代码实现:
#1、调用包和方法
from sklearn.datasets import load_iris
from sklearn import svm #调用SVM算法模型
from sklearn.model_selection import train_test_split #调用样本划分方法
from sklearn2pmml import sklearn2pmml, PMMLPipeline #调用生成pmml文件方法
#2、导入数据
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 40) # 划分训练集和验证集
#3、训练、验证模型
svm = svm.SVC(C = 1.0, kernel = 'rbf', degree = 3, gamma = 'scale', coef0 = 0.0, shrinking = True, probability = False, tol = 0.001, cache_size = 200, class_weight = None, verbose = False, max_iter = -1, decision_function_shape = 'ovr', break_ties = False, random_state = None)
svm.fit(X_train, y_train)
print("训练集:", clf.score(X_train, y_train)) #训练集
print("验证集:", clf.score(X_test, y_test)) #验证集
#4、生成PMML文件
pipeline = PMMLPipeline([("classifier", svm)])
pipeline.fit(X_train, y_train)
sklearn2pmml(pipeline, r"C:/Users/马青坡/Desktop/data/svm_iris.pmml")
(1)原理:使用CART树作为弱分类器,将多个不同的决策树进行组合,利用这种组合来降低单棵决策树的可能带来的片面性和判断不准确性。
(2)代码实现:
#1、调用包和方法
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier #调用随机森林算法模型
from sklearn.model_selection import train_test_split #调用样本划分方法
from sklearn2pmml import sklearn2pmml, PMMLPipeline #调用生成pmml文件方法
#2、导入数据
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 40) # 划分训练集和验证集
#3、训练、验证模型
clf = RandomForestClassifier(n_estimators = 100, criterion = 'gini', max_depth = None, min_samples_split = 2, min_samples_leaf = 1, min_weight_fraction_leaf = 0.0, max_features = 'auto', max_leaf_nodes = None, min_impurity_decrease = 0.0, min_impurity_split = None, bootstrap = True, oob_score = False, n_jobs = None, random_state = None, verbose = 0, warm_start = False, class_weight = None, ccp_alpha = 0.0, max_samples = None)
clf.fit(X_train, y_train)
print("训练集:", clf.score(X_train, y_train)) #训练集
print("验证集:", clf.score(X_test, y_test)) #验证集
#4、生成PMML文件
pipeline = PMMLPipeline([("classifier", clf)])
pipeline.fit(X_train, y_train)
sklearn2pmml(pipeline, r"C:/Users/马青坡/Desktop/data/rf_iris.pmml")
(1)原理:AdaBoost算法中,前一个基本分类器分错的样本会得到加强,加权后的全体样本再次被用来训练下一个基本分类器。同时,在每一轮中加入一个新的弱分类器,直到达到某个预定的足够小的错误率或达到预先指定的最大迭代次数。
(2)代码实现:
#1、调用包和方法
from sklearn.datasets import load_iris
from sklearn.ensemble import AdaBoostClassifier #调用AdaBoost算法模型
from sklearn.model_selection import train_test_split #调用样本划分方法
from sklearn2pmml import sklearn2pmml, PMMLPipeline #调用生成pmml文件方法
#2、导入数据
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 40) # 划分训练集和验证集
#3、训练、验证模型
clf = AdaBoostClassifier(base_estimator = None, n_estimators = 50, learning_rate = 1.0, algorithm = 'SAMME.R', random_state = None)
clf.fit(X_train, y_train)
print("训练集:", clf.score(X_train, y_train)) #训练集
print("验证集:", clf.score(X_test, y_test)) #验证集
#4、生成PMML文件
pipeline = PMMLPipeline([("classifier", clf)])
pipeline.fit(X_train, y_train)
sklearn2pmml(pipeline, r"C:/Users/马青坡/Desktop/data/ada_iris.pmml")
(1)原理:GBDT是每次建立单个分类器时,是在之前建立的模型的损失函数的梯度下降方向。GBDT的核心在于每一棵树学的是之前所有树结论和的残差,残差就是真实值与预测值的差值,所以为了得到残差,GBDT中的树全部是回归树,之所以不用分类树,是因为分类的结果相减是没有意义的。
(2)代码实现:
#1、调用包和方法
from sklearn.datasets import load_iris
from sklearn.ensemble import GradientBoostingClassifier #调用GBDT算法模型
from sklearn.model_selection import train_test_split #调用样本划分方法
from sklearn2pmml import sklearn2pmml, PMMLPipeline #调用生成pmml文件方法
#2、导入数据
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 40) # 划分训练集和验证集
#3、训练、验证模型
clf = GradientBoostingClassifier(loss = 'deviance', learning_rate = 0.1, n_estimators = 100, subsample = 1.0, criterion = 'friedman_mse', min_samples_split = 2, min_samples_leaf = 1, min_weight_fraction_leaf = 0.0, max_depth = 3, min_impurity_decrease = 0.0, min_impurity_split = None, init = None, random_state = None, max_features = None, verbose = 0, max_leaf_nodes = None, warm_start = False, presort = 'deprecated', validation_fraction = 0.1, n_iter_no_change = None, tol = 0.0001, ccp_alpha = 0.0)
clf.fit(X_train, y_train)
print("训练集:", clf.score(X_train, y_train)) #训练集
print("验证集:", clf.score(X_test, y_test)) #验证集
#4、生成PMML文件
pipeline = PMMLPipeline([("classifier", clf)])
pipeline.fit(X_train, y_train)
sklearn2pmml(pipeline, r"C:/Users/马青坡/Desktop/data/gbdt_iris.pmml")
(1)原理:XGBoost的原理与GBDT基本相同,但XGB是在GBDT基础上的优化。相比而言,XGB主要有2点优化:①XGB支持并行,速度快;②损失函数加入了正则项,防止过拟合。
(2)代码实现:
#1、调用包和方法
from sklearn.datasets import load_iris
from xgboost import XGBClassifier #调用xgb算法模型
from sklearn.model_selection import train_test_split #调用样本划分方法
from sklearn2pmml import sklearn2pmml, PMMLPipeline #调用生成pmml文件方法
#2、导入数据
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 40) # 划分训练集和验证集
#3、训练、验证模型
clf = XGBClassifier(max_depth=3, learning_rate=0.1, n_estimators=100, verbosity=1, silent=None, objective='binary:logistic', booster='gbtree', n_jobs=1, nthread=None, gamma=0, min_child_weight=1, max_delta_step=0, subsample=1, colsample_bytree=1, colsample_bylevel=1, colsample_bynode=1, reg_alpha=0, reg_lambda=1, scale_pos_weight=1, base_score=0.5, random_state=0, seed=None, missing=None)
clf.fit(X_train, y_train)
print("训练集:", clf.score(X_train, y_train)) #训练集
print("验证集:", clf.score(X_test, y_test)) #验证集
#4、生成PMML文件
pipeline = PMMLPipeline([("classifier", clf)])
pipeline.fit(X_train, y_train)
sklearn2pmml(pipeline, r"C:/Users/马青坡/Desktop/data/xgb_iris.pmml")
(1)原理:LightGBM是在XGB基础上的优化,主要优化点在于速度更快、占用内存更小、精确度更高、支持类别变量。
(2)代码实现:
#1、调用包和方法
from sklearn.datasets import load_iris
import pandas as pd
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import make_classification
import lightgbm as lgb #调用lgbm算法模型
from sklearn.model_selection import train_test_split #调用样本划分方法
from sklearn2pmml import sklearn2pmml, PMMLPipeline #调用生成pmml文件方法
#2、导入数据
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 40) # 划分训练集和验证集
#3、训练、验证模型
gbm = lgb.LGBMRegressor(boosting_type = 'gbdt', num_leaves = 31, max_depth = -1, learning_rate = 0.05, n_estimators = 100, subsample_for_bin = 200000, objective = 'regression', class_weight = None, min_split_gain = 0.0, min_child_weight = 0.001, min_child_samples = 20, subsample = 1.0, subsample_freq = 0, colsample_bytree = 1.0, reg_alpha = 0.0, reg_lambda = 0.0, random_state = None, n_jobs = -1, silent = True, importance_type = 'split')
gbm.fit(X_train, y_train,eval_set = [(X_test, y_test)], eval_metric = 'l1', early_stopping_rounds = 10)
gbm.fit(X_train, y_train)
#测试集预测
y_pred = gbm.predict(X_test, num_iteration = gbm.best_iteration_)
##模型评估
print("The rmse of prediction is: ", mean_squared_error(y_test, y_pred) ** 0.5)
#特征重要性
print("Feature importances: ", list(gbm.feature_importances_))
#网格搜索法调参
estimator = lgb.LGBMRegressor(num_leaves = 31)
param_grid = {"learning_rate": [0.01, 0.1, 1], "n_estimators": [20, 40]}
gbm = GridSearchCV(estimator, param_grid)
#训练模型
gbm.fit(X_train, y_train)
#输出最优参数
print("Best parameters found by grid search are: ", gbm.best_params_)
#训练集、验证集评分
print("训练集:", gbm.score(X_train, y_train)) #训练集
print("验证集:", gbm.score(X_test, y_test)) #验证集
#4、生成PMML文件
pipeline = PMMLPipeline([("classifier", gbm)])
pipeline.fit(X_train, y_train)
sklearn2pmml(pipeline, r"C:/Users/马青坡/Desktop/data/gbm_iris.pmml")