Github代码
检测数据集中的每个子项是否属于同一分类:
If so return 类标签;
Else
寻找划分数据集的最好特征
划分数据集
创建分支节点
for每个划分的子集
调用createBranch函数并增加返回结果到分支节点中
return 分支节点
H = \sum_{i=1}^np(x_i)log_2p(x_i)
公式参考
\sigma(z) = \frac{1}{1+e^{-z}}
z = w_0x_0+w_1x_1+w_2x_2+...+w_nx_n
z = w^Tx
def gradAscent(dataMatIn, classLabels):
dataMatrix = np.mat(dataMatI #convert to NumPy matrix
labelMat = np.mat(classLabels).transpose()
m,n = np.shape(dataMatrix)
alpha = 0.001
maxCycles = 500
weights = np.ones((n,1))
for k in range(maxCycles):
h = sigmoid(dataMatrix*weights)
error = (labelMat - h)
weights = weights + alpha * dataMatrix.transpose()* error
return weights
def stocGradAscent0(dataMatrix, classLabels):
m,n = np.shape(dataMatrix)
alpha = 0.01
weights = np.ones(n)
for i in range(m):
h = sigmoid(sum(dataMatrix[i]*weights))
error = classLabels[i] - h
weights = weights + alpha * error * dataMatrix[i]
return weights
def stocGradAscent1(dataMatrix, classLabels, numIter=150):
dataMatrix = np.array(dataMatrix)
m,n = np.shape(dataMatrix)
weights = np.ones(n)
for j in range(numIter):
dataIndex = range(m)
dataIndex = list(dataIndex)
for i in range(m):
alpha = 2/(1.0+j+i)+0.0001
randIndex = int(np.random.uniform(0,len(dataIndex)))
h = sigmoid(sum(dataMatrix[randIndex]*weights))
error = classLabels[randIndex] - h
weights = weights + alpha * error * dataMatrix[randIndex]
del(dataIndex[randIndex])
return weights
w^TX + b = \sum_{i=1}^m\alpha_ix^Tx^{(i)} + b (4.1)
\frac{|w^TA + b|}{||w||}
minf(x)
s.t.g_i(x) \leq 0 (j=1,2,3...J)
L(x,\lambda,v) = f(x) + \sum_{j=1}^{J}\lambda_j[g_j(x)+v_j^2]
def smoSimple(dataMatIn, classLabels, C, toler, maxIter):
dataMatrix = np.mat(dataMatIn); labelMat = np.mat(classLabels).transpose()
b = 0; m,n = np.shape(dataMatrix)
alphas = np.mat(np.zeros((m,1)))
iter = 0
while (iter < maxIter):
alphaPairsChanged = 0
for i in range(m):
fXi = float(np.multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[i,:].T)) + b
Ei = fXi - float(labelMat[i])#if checks if an example violates KKT conditions
if ((labelMat[i]*Ei < -toler) and (alphas[i] < C)) or ((labelMat[i]*Ei > toler) and (alphas[i] > 0)):
j = selectJrand(i,m)
fXj = float(np.multiply(alphas,labelMat).T*(dataMatrix*dataMatrix[j,:].T)) + b
Ej = fXj - float(labelMat[j])
alphaIold = alphas[i].copy(); alphaJold = alphas[j].copy();
if (labelMat[i] != labelMat[j]):
L = max(0, alphas[j] - alphas[i])
H = min(C, C + alphas[j] - alphas[i])
else:
L = max(0, alphas[j] + alphas[i] - C)
H = min(C, alphas[j] + alphas[i])
if L==H: print("L==H"); continue
eta = 2.0 * dataMatrix[i,:]*dataMatrix[j,:].T - dataMatrix[i,:]*dataMatrix[i,:].T - dataMatrix[j,:]*dataMatrix[j,:].T
if eta >= 0: print("eta>=0"); continue
alphas[j] -= labelMat[j]*(Ei - Ej)/eta
alphas[j] = clipAlpha(alphas[j],H,L)
if (abs(alphas[j] - alphaJold) < 0.00001): print("j not moving enough"); continue
alphas[i] += labelMat[j]*labelMat[i]*(alphaJold - alphas[j])#update i by the same amount as j
#the update is in the oppostie direction
b1 = b - Ei- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[i,:].T - labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[i,:]*dataMatrix[j,:].T
b2 = b - Ej- labelMat[i]*(alphas[i]-alphaIold)*dataMatrix[i,:]*dataMatrix[j,:].T - labelMat[j]*(alphas[j]-alphaJold)*dataMatrix[j,:]*dataMatrix[j,:].T
if (0 < alphas[i]) and (C > alphas[i]): b = b1
elif (0 < alphas[j]) and (C > alphas[j]): b = b2
else: b = (b1 + b2)/2.0
alphaPairsChanged += 1
print("iter: %d i:%d, pairs changed %d" % (iter,i,alphaPairsChanged))
if (alphaPairsChanged == 0): iter += 1
else: iter = 0
print("iteration number: %d" % iter)
return b,alphas
def calcWs(alphas,dataArr,classLabels):
X = np.mat(dataArr); labelMat = np.mat(classLabels).transpose()
m,n = np.shape(X)
w = np.zeros((n,1))
for i in range(m):
w += np.multiply(alphas[i]*labelMat[i],X[i,:].T)
return w
f(x) = sign(\sum_{i=1}^N\alpha_i^*y_iK(x,x_i) + b^*)
f(x) = sign(w^*X + b^*) = sign(\sum_{i=1}^N\alpha_i^*y_i(xx_i) + b)
\epsilon = \frac{the \quad number \quad of \quad error \quad samples}{the \quad number \quad of \quad all \quad samples}
\alpha = \frac{1}{2}ln(\frac{1-\epsilon}{\epsilon})
D_i^{(t+1)} = \frac{D_i^{t}e^{\pm\alpha}}{Sum(D)}
3.例子 单层决策树
# 单层决策树:树桩弱分类器
def stumpClassify(dataMatrix, dimen, threshVal, threshIneq): #输入:样本特征矩阵,指定特征,阈值,是否反转类别
retArray = ones((shape(dataMatrix)[0], 1))
if threshIneq == 'lt':
retArray[dataMatrix[:, dimen] <= threshVal] = -1.0
else:
retArray[dataMatrix[:, dimen] > threshVal] = -1.0
return retArray # 返回类别向量
# 遍历分类函数所有可能的输入 找到基于D的最佳单层决策树
def buildStump(dataArr, classLabels, D): # 输入:样本特征矩阵,样本标签,样本权重
dataMatrix = mat(dataArr);
labelMat = mat(classLabels).T
m, n = shape(dataMatrix)
numSteps = 10.0; # 用于遍历特征的阈值
bestStump = {}; # 记录最佳单层树桩的信息
bestClasEst = mat(zeros((m, 1)))
minError = inf # 记录最小错误率
for i in range(n): # 遍历所有特征
rangeMin = dataMatrix[:, i].min();
rangeMax = dataMatrix[:, i].max();
stepSize = (rangeMax - rangeMin) / numSteps
for j in range(-1, int(numSteps) + 1): # 遍历所有阈值
for inequal in ['lt', 'gt']: # 反转类别
threshVal = (rangeMin + float(j) * stepSize)
predictedVals = stumpClassify(dataMatrix, i, threshVal,inequal) # 指定特征,阈值,类别判定 后 树桩进行分类
errArr = mat(ones((m, 1)))
errArr[predictedVals == labelMat] = 0
weightedError = D.T * errArr # 计算加权错误率
#print "split: dim %d, thresh %.2f, thresh ineqal: %s, the weighted error is %.3f" % (i, threshVal, inequal, weightedError)
if weightedError < minError: # 更新最佳树桩配置信息
minError = weightedError
bestClasEst = predictedVals.copy()
bestStump['dim'] = i
bestStump['thresh'] = threshVal
bestStump['ineq'] = inequal
return bestStump, minError, bestClasEst# 返回决策树,最小错误率,及其预测类别
---------------------
作者:xiaxzhou
来源:CSDN
原文:https://blog.csdn.net/xiaxzhou/article/details/72872270
版权声明:本文为博主原创文章,转载请附上博文链接!
/ | 预测+1 | 预测-1 |
---|---|---|
真实+1 | 真正例(TP) | 违反例(FN) |
真实-1 | 伪正例(FP) | 真反例(TN) |
- 正确率 = TP/(TP+FP),预测正例样本中真正正例的比例
- 召回率 = TP/(TP+FN),预测正例样本的真实正例占所有真实正例的比例