-
大小: 1KB文件类型: .zip金币: 2下载: 1 次发布日期: 2021-04-16
- 语言: Python
- 标签: tradaboost python zip
资源简介
tradaboost的python算法实现
代码片段和文件信息
# -*- coding: UTF-8 -*-
import numpy as np
from sklearn import tree
# H 测试样本分类结果
# TrainS 原训练样本 np数组
# TrainA 辅助训练样本
# LabelS 原训练样本标签
# LabelA 辅助训练样本标签
# Test 测试样本
# N 迭代次数
def tradaboost(trans_S trans_A label_S label_A test N):
trans_data = np.concatenate((trans_A trans_S) axis=0)
trans_label = np.concatenate((label_A label_S) axis=0)
row_A = trans_A.shape[0]
row_S = trans_S.shape[0]
row_T = test.shape[0]
test_data = np.concatenate((trans_data test) axis=0)
# 初始化权重
weights_A = np.ones([row_A 1]) / row_A
weights_S = np.ones([row_S 1]) / row_S
weights = np.concatenate((weights_A weights_S) axis=0)
bata = 1 / (1 + np.sqrt(2 * np.log(row_A / N)))
# 存储每次迭代的标签和bata值?
bata_T = np.zeros([1 N])
result_label = np.ones([row_A + row_S + row_T N])
predict = np.zeros([row_T])
print ‘params initial finished.‘
trans_data = np.asarray(trans_data order=‘C‘)
trans_label = np.asarray(trans_label order=‘C‘)
test_data = np.asarray(test_data order=‘C‘)
for i in range(N):
P = calculate_P(weights trans_label)
result_label[: i] = train_classify(trans_data trans_label
test_data P)
print ‘result‘ result_label[: i] row_A row_S i result_label.shape
error_rate = calculate_error_rate(label_S result_label[row_A:row_A + row_S i]
weights[row_A:row_A + row_S :])
print ‘Error rate:‘ error_rate
if error_rate > 0.5:
error_rate = 0.5
if error_rate == 0:
N = i
break # 防止过拟合
# error_rate = 0.001
bata_T[0 i] = error_rate / (1 - error_rate)
# 调整源域样本权重
for j in range(row_S):
weights[row_A + j] = weights[row_A + j] * np.power(bata_T[0 i]
np.abs(result_label[row_A + j i] - label_S[j]))
# 调整辅域样本权重
for j in range(row_A):
weights[j] = weights[j] * np.power(bata (-np.abs(result_label[j i] - label_A[j])))
# print bata_T
for i in range(row_T):
# 跳过训练数据的标签
left = np.sum(
result_label[row_A + row_S + i int(np.ceil(N / 2)):N] * np.log(1 / bata_T[0 int(np.ceil(N / 2)):N]))
right = 0.5 * np.sum(np.log(1 / bata_T[0 int(np.ceil(N / 2)):N]))
if left >= right:
predict[i] = 1
else:
predict[i] = 0
# print left right predict[i]
return predict
def calculate_P(weights label):
total = np.sum(weights)
return np.asarray(weights / total order=‘C‘)
def train_classify(trans_data trans_label test_data P):
clf = tree.DecisionTreeClassifier(criterion=“gini“ max_features=“log2“ splitter=“random“)
clf.fit(trans_data trans_label sample_weight=P[: 0])
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3442 2020-03-31 22:21 tradaboost.py
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- 蓝奏云批量上传工具.zip
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
评论
共有 条评论