天天看點

ML之分類預測之ElasticNet:利用ElasticNet回歸對二分類資料集建構二分類器(DIY交叉驗證+分類的兩種度量PK)

輸出結果

ML之分類預測之ElasticNet:利用ElasticNet回歸對二分類資料集建構二分類器(DIY交叉驗證+分類的兩種度量PK)
ML之分類預測之ElasticNet:利用ElasticNet回歸對二分類資料集建構二分類器(DIY交叉驗證+分類的兩種度量PK)
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])

#第156處采樣

TP = tpr[156] * P

FP = fpr[156] * N

print('156:Threshold Value =   ', thresh[156])

繼續閱讀