mirror of
https://github.com/apachecn/ailearning.git
synced 2026-02-13 07:15:26 +08:00
优化 7.集成方法的md文件
This commit is contained in:
@@ -155,16 +155,16 @@ def adaBoostTrainDS(dataArr, labelArr, numIt=40):
|
||||
# store Stump Params in Array
|
||||
weakClassArr.append(bestStump)
|
||||
|
||||
print "alpha=%s, classEst=%s, bestStump=%s, error=%s " % (alpha, classEst.T, bestStump, error)
|
||||
# print "alpha=%s, classEst=%s, bestStump=%s, error=%s " % (alpha, classEst.T, bestStump, error)
|
||||
# -1主要是下面求e的-alpha次方; 如果判断正确,乘积为1,否则成绩为-1,这样就可以算出分类的情况了
|
||||
expon = multiply(-1*alpha*mat(labelArr).T, classEst)
|
||||
print '\n'
|
||||
print 'labelArr=', labelArr
|
||||
print 'classEst=', classEst.T
|
||||
print '\n'
|
||||
print '乘积: ', multiply(mat(labelArr).T, classEst).T
|
||||
# print '\n'
|
||||
# print 'labelArr=', labelArr
|
||||
# print 'classEst=', classEst.T
|
||||
# print '\n'
|
||||
# print '乘积: ', multiply(mat(labelArr).T, classEst).T
|
||||
# 判断正确的,就乘以-1,否则就乘以1, 为什么? 书上的公式。
|
||||
print '(-1取反)预测值expon=', expon.T
|
||||
# print '(-1取反)预测值expon=', expon.T
|
||||
# 计算e的expon次方,然后计算得到一个综合的概率的值
|
||||
# 结果发现: 判断错误的特征,D对于的特征的权重值会变大。
|
||||
D = multiply(D, exp(expon))
|
||||
@@ -173,9 +173,9 @@ def adaBoostTrainDS(dataArr, labelArr, numIt=40):
|
||||
print '\n'
|
||||
|
||||
# 预测的分类结果值,在上一轮结果的基础上,进行加和操作
|
||||
print '当前的分类结果:', alpha*classEst.T
|
||||
# print '当前的分类结果:', alpha*classEst.T
|
||||
aggClassEst += alpha*classEst
|
||||
print "叠加后的分类结果aggClassEst: ", aggClassEst.T
|
||||
# print "叠加后的分类结果aggClassEst: ", aggClassEst.T
|
||||
# sign 判断正为1, 0为0, 负为-1,通过最终加和的权重值,判断符号。
|
||||
# 结果为:错误的样本标签集合,因为是 !=,那么结果就是0 正, 1 负
|
||||
aggErrors = multiply(sign(aggClassEst) != mat(labelArr).T, ones((m, 1)))
|
||||
|
||||
@@ -10,7 +10,7 @@ Random Forest Algorithm on Sonar Dataset
|
||||
---
|
||||
源代码网址:http://www.tuicool.com/articles/iiUfeim
|
||||
Flying_sfeng博客地址:http://blog.csdn.net/flying_sfeng/article/details/64133822
|
||||
在此表示感谢你的代码和注解, 我重新也完善了你的注解
|
||||
在此表示感谢你的代码和注解, 我重新也完善了个人注解
|
||||
'''
|
||||
from random import seed, randrange, random
|
||||
|
||||
@@ -26,7 +26,7 @@ def loadDataSet(filename):
|
||||
for featrue in line.split(','):
|
||||
# strip()返回移除字符串头尾指定的字符生成的新字符串
|
||||
str_f = featrue.strip()
|
||||
if str_f.isdigit():
|
||||
if str_f.isdigit(): # 判断是否是数字
|
||||
# 将数据集的第column列转换成float形式
|
||||
lineArr.append(float(str_f))
|
||||
else:
|
||||
@@ -46,22 +46,23 @@ def cross_validation_split(dataset, n_folds):
|
||||
dataset_split list集合,存放的是:将数据集进行抽重抽样 n_folds 份,数据可以重复重复抽取,每一次list的元素是无重复的
|
||||
"""
|
||||
dataset_split = list()
|
||||
dataset_copy = list(dataset) #复制一份dataset,防止dataset的内容改变
|
||||
dataset_copy = list(dataset) # 复制一份 dataset,防止 dataset 的内容改变
|
||||
fold_size = len(dataset) / n_folds
|
||||
for i in range(n_folds):
|
||||
fold = list() #每次循环fold清零,防止重复导入dataset_split
|
||||
while len(fold) < fold_size: #这里不能用if,if只是在第一次判断时起作用,while执行循环,直到条件不成立
|
||||
fold = list() # 每次循环 fold 清零,防止重复导入 dataset_split
|
||||
while len(fold) < fold_size: # 这里不能用 if,if 只是在第一次判断时起作用,while 执行循环,直到条件不成立
|
||||
# 有放回的随机采样,有一些样本被重复采样,从而在训练集中多次出现,有的则从未在训练集中出现,此则自助采样法。从而保证每棵决策树训练集的差异性
|
||||
index = randrange(len(dataset_copy))
|
||||
# 将对应索引index的内容从dataset_copy中导出,并将该内容从dataset_copy中删除。
|
||||
# pop()函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
|
||||
fold.append(dataset_copy.pop(index))
|
||||
# 将对应索引 index 的内容从 dataset_copy 中导出,并将该内容从 dataset_copy 中删除。
|
||||
# pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
|
||||
# fold.append(dataset_copy.pop(index)) # 无放回的方式
|
||||
fold.append(dataset_copy[index]) # 有放回的方式
|
||||
dataset_split.append(fold)
|
||||
# 由dataset分割出的n_folds个数据构成的列表,为了用于交叉验证
|
||||
return dataset_split
|
||||
|
||||
|
||||
# Split a dataset based on an attribute and an attribute value #根据特征和特征值分割数据集
|
||||
# Split a dataset based on an attribute and an attribute value # 根据特征和特征值分割数据集
|
||||
def test_split(index, value, dataset):
|
||||
left, right = list(), list()
|
||||
for row in dataset:
|
||||
@@ -73,45 +74,46 @@ def test_split(index, value, dataset):
|
||||
|
||||
|
||||
# Calculate the Gini index for a split dataset
|
||||
def gini_index(groups, class_values): #个人理解:计算代价,分类越准确,则gini越小
|
||||
def gini_index(groups, class_values): # 个人理解:计算代价,分类越准确,则 gini 越小
|
||||
gini = 0.0
|
||||
for class_value in class_values: #class_values =[0,1]
|
||||
for group in groups: #groups=(left,right)
|
||||
for class_value in class_values: # class_values = [0, 1]
|
||||
for group in groups: # groups = (left, right)
|
||||
size = len(group)
|
||||
if size == 0:
|
||||
continue
|
||||
proportion = [row[-1] for row in group].count(class_value) / float(size)
|
||||
gini += (proportion * (1.0 - proportion)) #个人理解:计算代价,分类越准确,则gini越小
|
||||
gini += (proportion * (1.0 - proportion)) # 个人理解:计算代价,分类越准确,则 gini 越小
|
||||
return gini
|
||||
|
||||
|
||||
# 找出分割数据集的最优特征,得到最优的特征index,特征值row[index],以及分割完的数据groups(left,right)
|
||||
# 找出分割数据集的最优特征,得到最优的特征 index,特征值 row[index],以及分割完的数据 groups(left, right)
|
||||
def get_split(dataset, n_features):
|
||||
class_values = list(set(row[-1] for row in dataset)) #class_values =[0,1]
|
||||
class_values = list(set(row[-1] for row in dataset)) # class_values =[0, 1]
|
||||
b_index, b_value, b_score, b_groups = 999, 999, 999, None
|
||||
features = list()
|
||||
while len(features) < n_features:
|
||||
index = randrange(len(dataset[0])-1) #往features添加n_features个特征(n_feature等于特征数的根号),特征索引从dataset中随机取
|
||||
index = randrange(len(dataset[0])-1) # 往 features 添加 n_features 个特征( n_feature 等于特征数的根号),特征索引从 dataset 中随机取
|
||||
if index not in features:
|
||||
features.append(index)
|
||||
for index in features: #在n_features个特征中选出最优的特征索引,并没有遍历所有特征,从而保证了每课决策树的差异性
|
||||
for index in features: # 在 n_features 个特征中选出最优的特征索引,并没有遍历所有特征,从而保证了每课决策树的差异性
|
||||
for row in dataset:
|
||||
groups = test_split(index, row[index], dataset) #groups=(left,right);row[index]遍历每一行index索引下的特征值作为分类值value,找出最优的分类特征和特征值
|
||||
groups = test_split(index, row[index], dataset) # groups=(left, right), row[index] 遍历每一行 index 索引下的特征值作为分类值 value, 找出最优的分类特征和特征值
|
||||
gini = gini_index(groups, class_values)
|
||||
# 左右两边的数量越一样,说明数据区分度不高,gini系数越大
|
||||
if gini < b_score:
|
||||
b_index, b_value, b_score, b_groups = index, row[index], gini, groups #最后得到最优的分类特征b_index,分类特征值b_value,分类结果b_groups。b_value为分错的代价成本。
|
||||
#print b_score
|
||||
return {'index':b_index, 'value':b_value, 'groups':b_groups}
|
||||
b_index, b_value, b_score, b_groups = index, row[index], gini, groups # 最后得到最优的分类特征 b_index,分类特征值 b_value,分类结果 b_groups。b_value 为分错的代价成本
|
||||
# print b_score
|
||||
return {'index': b_index, 'value': b_value, 'groups': b_groups}
|
||||
|
||||
|
||||
# Create a terminal node value #输出group中出现次数较多的标签
|
||||
# Create a terminal node value # 输出group中出现次数较多的标签
|
||||
def to_terminal(group):
|
||||
outcomes = [row[-1] for row in group] #max()函数中,当key参数不为空时,就以key的函数对象为判断的标准;
|
||||
return max(set(outcomes), key=outcomes.count) # 输出group中出现次数较多的标签
|
||||
outcomes = [row[-1] for row in group] # max() 函数中,当 key 参数不为空时,就以 key 的函数对象为判断的标准
|
||||
return max(set(outcomes), key=outcomes.count) # 输出 group 中出现次数较多的标签
|
||||
|
||||
|
||||
# Create child splits for a node or make terminal #创建子分割器,递归分类,直到分类结束
|
||||
def split(node, max_depth, min_size, n_features, depth): #max_depth = 10,min_size = 1,n_features = int(sqrt(len(dataset[0])-1))
|
||||
# Create child splits for a node or make terminal # 创建子分割器,递归分类,直到分类结束
|
||||
def split(node, max_depth, min_size, n_features, depth): # max_depth = 10, min_size = 1, n_features = int(sqrt((dataset[0])-1))
|
||||
left, right = node['groups']
|
||||
del(node['groups'])
|
||||
# check for a no split
|
||||
@@ -119,15 +121,15 @@ def split(node, max_depth, min_size, n_features, depth): #max_depth = 10,min_
|
||||
node['left'] = node['right'] = to_terminal(left + right)
|
||||
return
|
||||
# check for max depth
|
||||
if depth >= max_depth: #max_depth=10表示递归十次,若分类还未结束,则选取数据中分类标签较多的作为结果,使分类提前结束,防止过拟合
|
||||
if depth >= max_depth: # max_depth=10 表示递归十次,若分类还未结束,则选取数据中分类标签较多的作为结果,使分类提前结束,防止过拟合
|
||||
node['left'], node['right'] = to_terminal(left), to_terminal(right)
|
||||
return
|
||||
# process left child
|
||||
if len(left) <= min_size:
|
||||
node['left'] = to_terminal(left)
|
||||
else:
|
||||
node['left'] = get_split(left, n_features) #node['left']是一个字典,形式为{'index':b_index, 'value':b_value, 'groups':b_groups},所以node是一个多层字典
|
||||
split(node['left'], max_depth, min_size, n_features, depth+1) #递归,depth+1计算递归层数
|
||||
node['left'] = get_split(left, n_features) # node['left']是一个字典,形式为{'index':b_index, 'value':b_value, 'groups':b_groups},所以node是一个多层字典
|
||||
split(node['left'], max_depth, min_size, n_features, depth+1) # 递归,depth+1计算递归层数
|
||||
# process right child
|
||||
if len(right) <= min_size:
|
||||
node['right'] = to_terminal(right)
|
||||
@@ -149,19 +151,19 @@ def build_tree(train, max_depth, min_size, n_features):
|
||||
root 返回决策树
|
||||
"""
|
||||
|
||||
# 返回最有列和相关的信息
|
||||
# 返回最优列和相关的信息
|
||||
root = get_split(train, n_features)
|
||||
|
||||
# 对左右2变的数据 进行递归的调用,由于最优特征使用过,所以在后面进行使用的时候,就没有意义了
|
||||
# 对左右2边的数据 进行递归的调用,由于最优特征使用过,所以在后面进行使用的时候,就没有意义了
|
||||
# 例如: 性别-男女,对男使用这一特征就没任何意义了
|
||||
split(root, max_depth, min_size, n_features, 1)
|
||||
return root
|
||||
|
||||
|
||||
# Make a prediction with a decision tree
|
||||
def predict(node, row): #预测模型分类结果
|
||||
def predict(node, row): # 预测模型分类结果
|
||||
if row[node['index']] < node['value']:
|
||||
if isinstance(node['left'], dict): #isinstance是Python中的一个内建函数。是用来判断一个对象是否是一个已知的类型。
|
||||
if isinstance(node['left'], dict): # isinstance是Python中的一个内建函数。是用来判断一个对象是否是一个已知的类型。
|
||||
return predict(node['left'], row)
|
||||
else:
|
||||
return node['left']
|
||||
@@ -189,7 +191,7 @@ def bagging_predict(trees, row):
|
||||
|
||||
|
||||
# Create a random subsample from the dataset with replacement
|
||||
def subsample(dataset, ratio): #创建数据集的随机子样本
|
||||
def subsample(dataset, ratio): # 创建数据集的随机子样本
|
||||
"""random_forest(评估算法性能,返回模型得分)
|
||||
|
||||
Args:
|
||||
@@ -227,7 +229,7 @@ def random_forest(train, test, max_depth, min_size, sample_size, n_trees, n_feat
|
||||
"""
|
||||
|
||||
trees = list()
|
||||
# n_trees表示决策树的数量
|
||||
# n_trees 表示决策树的数量
|
||||
for i in range(n_trees):
|
||||
# 随机抽样的训练样本, 随机采样保证了每棵决策树训练集的差异性
|
||||
sample = subsample(train, sample_size)
|
||||
@@ -241,7 +243,7 @@ def random_forest(train, test, max_depth, min_size, sample_size, n_trees, n_feat
|
||||
|
||||
|
||||
# Calculate accuracy percentage
|
||||
def accuracy_metric(actual, predicted): #导入实际值和预测值,计算精确度
|
||||
def accuracy_metric(actual, predicted): # 导入实际值和预测值,计算精确度
|
||||
correct = 0
|
||||
for i in range(len(actual)):
|
||||
if actual[i] == predicted[i]:
|
||||
@@ -262,14 +264,14 @@ def evaluate_algorithm(dataset, algorithm, n_folds, *args):
|
||||
scores 模型得分
|
||||
"""
|
||||
|
||||
# 将数据集进行抽重抽样 n_folds 份,数据可以重复重复抽取,每一次list的元素是无重复的
|
||||
# 将数据集进行抽重抽样 n_folds 份,数据可以重复重复抽取,每一次 list 的元素是无重复的
|
||||
folds = cross_validation_split(dataset, n_folds)
|
||||
scores = list()
|
||||
# 每次循环从folds从取出一个fold作为测试集,其余作为训练集,遍历整个folds,实现交叉验证
|
||||
# 每次循环从 folds 从取出一个 fold 作为测试集,其余作为训练集,遍历整个 folds ,实现交叉验证
|
||||
for fold in folds:
|
||||
train_set = list(folds)
|
||||
train_set.remove(fold)
|
||||
# 将多个fold列表组合成一个train_set列表, 类似 union all
|
||||
# 将多个 fold 列表组合成一个 train_set 列表, 类似 union all
|
||||
"""
|
||||
In [20]: l1=[[1, 2, 'a'], [11, 22, 'b']]
|
||||
In [21]: l2=[[3, 4, 'c'], [33, 44, 'd']]
|
||||
@@ -283,11 +285,11 @@ def evaluate_algorithm(dataset, algorithm, n_folds, *args):
|
||||
"""
|
||||
train_set = sum(train_set, [])
|
||||
test_set = list()
|
||||
# fold表示从原始数据集dataset提取出来的测试集
|
||||
# fold 表示从原始数据集 dataset 提取出来的测试集
|
||||
for row in fold:
|
||||
row_copy = list(row)
|
||||
test_set.append(row_copy)
|
||||
row_copy[-1] = None
|
||||
test_set.append(row_copy)
|
||||
predicted = algorithm(train_set, test_set, *args)
|
||||
actual = [row[-1] for row in fold]
|
||||
|
||||
@@ -307,9 +309,9 @@ if __name__ == '__main__':
|
||||
max_depth = 20 # 调参(自己修改) #决策树深度不能太深,不然容易导致过拟合
|
||||
min_size = 1 # 决策树的叶子节点最少的元素数量
|
||||
sample_size = 1.0 # 做决策树时候的样本的比例
|
||||
# n_features = int(sqrt(len(dataset[0])-1))
|
||||
n_features =15 # 调参(自己修改) #准确性与多样性之间的权衡
|
||||
for n_trees in [1, 5, 10]: # 理论上树是越多越好
|
||||
# n_features = int((len(dataset[0])-1))
|
||||
n_features = 15 # 调参(自己修改) #准确性与多样性之间的权衡
|
||||
for n_trees in [1, 10, 20]: # 理论上树是越多越好
|
||||
scores = evaluate_algorithm(dataset, random_forest, n_folds, max_depth, min_size, sample_size, n_trees, n_features)
|
||||
# 每一次执行本文件时都能产生同一个随机数
|
||||
seed(1)
|
||||
|
||||
Reference in New Issue
Block a user