欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
R語(yǔ)言邏輯回歸、ROC曲線(xiàn)和十折交叉驗證

自己整理編寫(xiě)的邏輯回歸模板,作為學(xué)習筆記記錄分享。數據集用的是14個(gè)自變量Xi,一個(gè)因變量Y的australian數據集。


1. 測試集和訓練集3、7分組

  1. australian <- read.csv("australian.csv",as.is = T,sep=",",header=TRUE)  
  2. #讀取行數  
  3. N = length(australian$Y)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  4. #ind=1的是0.7概率出現的行,ind=2是0.3概率出現的行  
  5. ind=sample(2,N,replace=TRUE,prob=c(0.7,0.3))  
  6. #生成訓練集(這里訓練集和測試集隨機設置為原數據集的70%,30%)  
  7. aus_train <- australian[ind==1,]  
  8. #生成測試集  
  9. aus_test <- australian[ind==2,]  

2.生成模型,結果導出

  1. #生成logis模型,用glm函數  
  2. #用訓練集數據生成logis模型,用glm函數  
  3. #family:每一種響應分布(指數分布族)允許各種關(guān)聯(lián)函數將均值和線(xiàn)性預測器關(guān)聯(lián)起來(lái)。常用的family:binomal(link='logit')--響應變量服從二項分布,連接函數為logit,即logistic回歸  
  4. pre <- glm(Y ~.,family=binomial(link = "logit"),data = aus_train)  
  5. summary(pre)  
  6.   
  7. #測試集的真實(shí)值  
  8. real <- aus_test$Y  
  9. #predict函數可以獲得模型的預測值。這里預測所需的模型對象為pre,預測對象newdata為測試集,預測所需類(lèi)型type選擇response,對響應變量的區間進(jìn)行調整  
  10. predict. <- predict.glm(pre,type='response',newdata=aus_test)  
  11. #按照預測值為1的概率,>0.5的返回1,其余返回0  
  12. predict =ifelse(predict.>0.5,1,0)  
  13. #數據中加入預測值一列  
  14. aus_test$predict = predict  
  15. #導出結果為csv格式  
  16. #write.csv(aus_test,"aus_test.csv")  

3.模型檢驗

  1. ##模型檢驗  
  2. res <- data.frame(real,predict)  
  3. #訓練數據的行數,也就是樣本數量  
  4. n = nrow(aus_train)        
  5. #計算Cox-Snell擬合優(yōu)度  
  6. R2 <- 1-exp((pre$deviance-pre$null.deviance)/n)      
  7. cat("Cox-Snell R2=",R2,"\n")  
  8. #計算Nagelkerke擬合優(yōu)度,我們在最后輸出這個(gè)擬合優(yōu)度值  
  9. R2<-R2/(1-exp((-pre$null.deviance)/n))    
  10. cat("Nagelkerke R2=",R2,"\n")  
  11. ##模型的其他指標  
  12. #residuals(pre)     #殘差  
  13. #coefficients(pre)  #系數,線(xiàn)性模型的截距項和每個(gè)自變量的斜率,由此得出線(xiàn)性方程表達式?;蛘邔?xiě)為coef(pre)  
  14. #anova(pre)         #方差  

4.準確率和精度

  1. true_value=aus_test[,15]  
  2. predict_value=aus_test[,16]  
  3. #計算模型精確度  
  4. error = predict_value-true_value  
  5. accuracy = (nrow(aus_test)-sum(abs(error)))/nrow(aus_test) #精確度--判斷正確的數量占總數的比例  
  6. #計算Precision,Recall和F-measure  
  7. #一般來(lái)說(shuō),Precision就是檢索出來(lái)的條目(比如:文檔、網(wǎng)頁(yè)等)有多少是準確的,Recall就是所有準確的條目有多少被檢索出來(lái)了  
  8. #和混淆矩陣結合,Precision計算的是所有被檢索到的item(TP+FP)中,"應該被檢索到的item(TP)”占的比例;Recall計算的是所有檢索到的item(TP)占所有"應該被檢索到的item(TP+FN)"的比例。  
  9. precision=sum(true_value & predict_value)/sum(predict_value)  #真實(shí)值預測值全為1 / 預測值全為1 --- 提取出的正確信息條數/提取出的信息條數  
  10. recall=sum(predict_value & true_value)/sum(true_value)  #真實(shí)值預測值全為1 / 真實(shí)值全為1 --- 提取出的正確信息條數 /樣本中的信息條數  
  11. #P和R指標有時(shí)候會(huì )出現的矛盾的情況,這樣就需要綜合考慮他們,最常見(jiàn)的方法就是F-Measure(又稱(chēng)為F-Score)  
  12. F_measure=2*precision*recall/(precision+recall)    #F-Measure是Precision和Recall加權調和平均,是一個(gè)綜合評價(jià)指標  
  13. #輸出以上各結果  
  14. print(accuracy)  
  15. print(precision)  
  16. print(recall)  
  17. print(F_measure)  
  18. #混淆矩陣,顯示結果依次為T(mén)P、FN、FP、TN  
  19. table(true_value,predict_value)           

5.ROC曲線(xiàn)的幾個(gè)方法

  1. #ROC曲線(xiàn)  
  2. # 方法1  
  3. #install.packages("ROCR")    
  4. library(ROCR)       
  5. pred <- prediction(predict.,true_value)   #預測值(0.5二分類(lèi)之前的預測值)和真實(shí)值     
  6. performance(pred,'auc')@y.values        #AUC值  
  7. perf <- performance(pred,'tpr','fpr')  
  8. plot(perf)  
  9. #方法2  
  10. #install.packages("pROC")  
  11. library(pROC)  
  12. modelroc <- roc(true_value,predict.)  
  13. plot(modelroc, print.auc=TRUE, auc.polygon=TRUE,legacy.axes=TRUE, grid=c(0.1, 0.2),  
  14.      grid.col=c("green", "red"), max.auc.polygon=TRUE,  
  15.      auc.polygon.col="skyblue", print.thres=TRUE)        #畫(huà)出ROC曲線(xiàn),標出坐標,并標出AUC的值  
  16. #方法3,按ROC定義  
  17. TPR=rep(0,1000)  
  18. FPR=rep(0,1000)  
  19. p=predict.  
  20. for(i in 1:1000)  
  21.   {   
  22.   p0=i/1000;  
  23.   ypred<-1*(p>p0)    
  24.   TPR[i]=sum(ypred*true_value)/sum(true_value)    
  25.   FPR[i]=sum(ypred*(1-true_value))/sum(1-true_value)  
  26.   }  
  27. plot(FPR,TPR,type="l",col=2)  
  28. points(c(0,1),c(0,1),type="l",lty=2)  

6.更換測試集和訓練集的選取方式,采用十折交叉驗證

  1. australian <- read.csv("australian.csv",as.is = T,sep=",",header=TRUE)  
  2. #將australian數據分成隨機十等分  
  3. #install.packages("caret")  
  4. #固定folds函數的分組  
  5. set.seed(7)  
  6. require(caret)  
  7. folds <- createFolds(y=australian$Y,k=10)  
  8.   
  9. #構建for循環(huán),得10次交叉驗證的測試集精確度、訓練集精確度  
  10.   
  11. max=0  
  12. num=0  
  13.   
  14. for(i in 1:10){  
  15.     
  16.   fold_test <- australian[folds[[i]],]   #取folds[[i]]作為測試集  
  17.   fold_train <- australian[-folds[[i]],]   # 剩下的數據作為訓練集  
  18.     
  19.   print("***組號***")  
  20.     
  21.   fold_pre <- glm(Y ~.,family=binomial(link='logit'),data=fold_train)  
  22.   fold_predict <- predict(fold_pre,type='response',newdata=fold_test)  
  23.   fold_predict =ifelse(fold_predict>0.5,1,0)  
  24.   fold_test$predict = fold_predict  
  25.   fold_error = fold_test[,16]-fold_test[,15]  
  26.   fold_accuracy = (nrow(fold_test)-sum(abs(fold_error)))/nrow(fold_test)   
  27.   print(i)  
  28.   print("***測試集精確度***")  
  29.   print(fold_accuracy)  
  30.   print("***訓練集精確度***")  
  31.   fold_predict2 <- predict(fold_pre,type='response',newdata=fold_train)  
  32.   fold_predict2 =ifelse(fold_predict2>0.5,1,0)  
  33.   fold_train$predict = fold_predict2  
  34.   fold_error2 = fold_train[,16]-fold_train[,15]  
  35.   fold_accuracy2 = (nrow(fold_train)-sum(abs(fold_error2)))/nrow(fold_train)   
  36.   print(fold_accuracy2)  
  37.     
  38.     
  39.   if(fold_accuracy>max)  
  40.     {  
  41.     max=fold_accuracy    
  42.     num=i  
  43.     }  
  44.     
  45. }  
  46.   
  47. print(max)  
  48. print(num)  
  49.   
  50. ##結果可以看到,精確度accuracy最大的一次為max,取folds[[num]]作為測試集,其余作為訓練集。  

7.得到十折交叉驗證的精確度,結果導出

  1. #十折里測試集最大精確度的結果  
  2. testi <- australian[folds[[num]],]  
  3. traini <- australian[-folds[[num]],]   # 剩下的folds作為訓練集  
  4. prei <- glm(Y ~.,family=binomial(link='logit'),data=traini)  
  5. predicti <- predict.glm(prei,type='response',newdata=testi)  
  6. predicti =ifelse(predicti>0.5,1,0)  
  7. testi$predict = predicti  
  8. #write.csv(testi,"ausfold_test.csv")  
  9. errori = testi[,16]-testi[,15]  
  10. accuracyi = (nrow(testi)-sum(abs(errori)))/nrow(testi)   
  11.   
  12. #十折里訓練集的精確度  
  13. predicti2 <- predict.glm(prei,type='response',newdata=traini)  
  14. predicti2 =ifelse(predicti2>0.5,1,0)  
  15. traini$predict = predicti2  
  16. errori2 = traini[,16]-traini[,15]  
  17. accuracyi2 = (nrow(traini)-sum(abs(errori2)))/nrow(traini)   
  18.   
  19. #測試集精確度、取第i組、訓練集精確  
  20. accuracyi;num;accuracyi2  
  21. #write.csv(traini,"ausfold_train.csv")  





本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
布爾值運算
用Python實(shí)現機器學(xué)習算法—— K 最近鄰算法
黑馬程序員機器學(xué)習Day3學(xué)習筆記
Python實(shí)現機器學(xué)習一(實(shí)現一元線(xiàn)性回歸)
在 Python 中使用深度學(xué)習 LSTM 模型預測股票價(jià)格
老年尿失禁的病因和治療_王建業(yè)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久