1. 程式人生 > >ML之分類預測之ElasticNet:利用ElasticNet迴歸對二分類資料集構建二分類器(DIY交叉驗證+分類的兩種度量PK)

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)