ML之分类预测之ElasticNet:利用ElasticNet回归对二分类数据集构建二分类器(DIY交叉验证+分类的两种度量PK)

ML之分类预测之ElasticNet:利用ElasticNet回归对二分类数据集构建二分类器(DIY交叉验证+分类的两种度量PK)


输出结果

设计思路

核心代码

#(4)交叉验证
for ixval in range(nxval):
    idxTest = [a for a in range(nrow) if a%nxval == ixval%nxval]
    idxTrain = [a for a in range(nrow) if a%nxval != ixval%nxval]

    xTrain = numpy.array([xNormalized[r] for r in idxTrain])
    xTest = numpy.array([xNormalized[r] for r in idxTest])
    labelTrain = numpy.array([labelNormalized[r] for r in idxTrain])
    labelTest = numpy.array([labelNormalized[r] for r in idxTest])
    alphas, coefs, _ = enet_path(xTrain, labelTrain,l1_ratio=0.8, fit_intercept=False, return_models=False)

    if ixval == 0:
        pred = numpy.dot(xTest, coefs)
        yOut = labelTest
    else:
        #accumulate predictions累积预测
        yTemp = numpy.array(yOut)
        yOut = numpy.concatenate((yTemp, labelTest), axis=0)

        #accumulate predictions
        predTemp = numpy.array(pred)
        pred = numpy.concatenate((predTemp, numpy.dot(xTest, coefs)), axis = 0)

#三处采样

P = len(idxPos)    #P = Positive cases
N = nrow - P       #N = Negative cases

#第52处采样
TP = tpr[52] * P   #TP = True positives = tpr * P
FN = P - TP        #FN = False negatives = P - TP
FP = fpr[52] * N   #FP = False positives = fpr * N
TN = N - FP        #TN = True negatives = N - FP
print('52:Threshold Value =   ', thresh[52])
print('TP = ', TP, 'FP = ', FP)
print('FN = ', FN, 'TN = ', TN)

#第104处采样
TP = tpr[104] * P
FN = P - TP
FP = fpr[104] * N
TN = N - FP
print('104:Threshold Value =   ', thresh[104])
print('TP = ', TP, 'FP = ', FP)
print('FN = ', FN, 'TN = ', TN)

#第156处采样
TP = tpr[156] * P
FN = P - TP
FP = fpr[156] * N
TN = N - FP
print('156:Threshold Value =   ', thresh[156])
print('TP = ', TP, 'FP = ', FP)
print('FN = ', FN, 'TN = ', TN)

(0)

相关推荐