文章目录
1. 随机森林分类
-
决策树(
Decision Tree
)
• 是在已知各种情况发生概率的基础上,通过构成决策树来求取净现值的期望值大于等于零的概率。
• 决策树是一种树形结构,其中每个内部节点表示一个属性上的测试,每个分支代表一个测试输出,每个叶节点代表一种类别。 -
在 Python 中实现决策树
• 函数:sklearn.tree.DecisionTreeClassifier (from sklearn.tree import DecisionTreeClassifier
)
• 模型初始化:dt_model = DecisionTreeClassifier()
• 训练数据:dt_model.fit(X, y)
-
随机森林
• 随机森林指的是利用多棵树对样本进行训练并预测的一种分类器。 -
随机森林的主要优势
• 具有极好的准确率
• 能够有效地运行在大数据集上
• 能够处理具有高维特征的输入样本
• 能够评估各个特征在分类问题上的重要性(权重) -
在 Python 中实现随机森林分类
• 函数:from sklearn.ensemble import RandomForestClassifier
• 模型初始化:model_rf = RandomForestClassifier()
• 训练数据:model_rf.fit(X, y)
-
随机森林分类 python 代码:
R代码参考:R语言-分类之随机森林篇# 导入包 import pandas as pd import numpy as np from sklearn.preprocessing import StandardScaler from scipy.stats import ttest_ind, levene from sklearn.linear_model import LassoCV from sklearn.utils import shuffle from sklearn.ensemble import RandomForestClassifier # 随机森林分类器 from sklearn.model_selection import train_test_split # 训练集测试集分割 # 导入数据 xlsx_a = 'data/featureTable/aa.xlsx' xlsx_b = 'data/featureTable/bb.xlsx' data_a = pd.read_excel(xlsx_a) data_b = pd.read_excel(xlsx_b) print(data_a.shape,data_b.shape) # (212, 30) (357, 30) # t 检验特征筛选 index = [] for colName in data_a.columns[:]: if levene(data_a[colName], data_b[colName])[1] > 0.05: if ttest_ind(data_a[colName], data_b[colName])[1] < 0.05: index.append(colName) else: if ttest_ind(data_a[colName], data_b[colName],equal_var=False)[1] < 0.05: index.append(colName) print(len(index)) # 25 print(index) # ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'M', 'N', 'P', 'Q', 'R', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD'] # t 检验后数据处理 dat