SlideShare a Scribd company logo
library(ROCR)
library(rpart)
library(rpart.plot)
#census.csvdataanalysis
#http://archive.ics.uci.edu/ml/datasets/Adult
cdata=read.csv("census.csv")
head(cdata,10)
summary(cdata)
str(cdata)
pairs(cdata[1:200,],col=cdata$over50k)
hist((cdata$capitalgain))
hist(log(cdata$capitalgain))
#prepare train80% test10% and validation10% sets
set.seed(2017)
spl = sample.split(cdata$over50k,SplitRatio=0.8)
ctrain = subset(cdata,spl==TRUE)
ctest= subset(cdata,spl==FALSE)
spl = sample.split(ctest$over50k,SplitRatio=0.5)
cvalidation=subset(ctest,spl==TRUE)
ctest= subset(ctest,spl==FALSE)
#buildfirstmodel logit
#buildmodel
set.seed(2017)
clogit= glm( over50k ~ . , family="binomial",data= ctrain)
summary(clogit)
#prediction andaccuracy on test
predictTest=predict(clogit,newdata=ctest,type = "response")
t(table(ctest$over50k,predictTest>= 0.5))
(2262+484)/sum(table(ctest$over50k,predictTest>=0.5))
#most frequentprediction
table(cdata$over50k)
24283/(24283+7695)
#AUC
ROCRpred= prediction(predictTest,ctest$over50k)
as.numeric(performance(ROCRpred,"auc")@y.values)
###################################################
# summarylogitaccuracy fort=0.5 0.859 auc=0.91 #
###################################################
#decisiontree
#buildmodel
set.seed(2017)
ctree = rpart( over50k ~ . , method="class",data= ctrain,minbucket=100)
prp(ctree)
#buildprediction
predictTest=predict(ctree,newdata=ctest,type = "class")
t(table(ctest$over50k,predictTest))
(2300+405)/sum(table(ctest$over50k,predictTest))
#accuracy=0.846
#rocr auc
predictTest=predict(ctree,newdata=ctest)
predictTest=predictTest[,2]
ROCRpred= prediction(predictTest,ctest$over50k)
as.numeric(performance(ROCRpred,"auc")@y.values)
##################################################################
# summarytree minbucket=100 accuracy fort=0.5 0.846 auc=0.855 #
##################################################################
#parameterstuningdecisiontree
set.seed(2017)
cartGrid = expand.grid( .cp= seq(0.001,0.1,0.001))
fitControl =trainControl( method="cv",number= 10 )
rezCV=train( over50k~ . , data = ctrain, method= "rpart",trControl = fitControl,tuneGrid=cartGrid )
rezCV
cvmod= rpart(over50k~.,data=ctrain,method="class",cp=0.001)
prp(cvmod)
#accuracy
predictTest=predict(cvmod,newdata=ctest,type="class")
table(ctest$over50k,predictTest)
(2329+448)/sum(table(ctest$over50k,predictTest))
#accuracy 0.868
#rocr auc
predictTest=predict(cvmod,newdata=ctest)
predictTest=predictTest[,2]
ROCRpred= prediction(predictTest,ctest$over50k)
as.numeric(performance(ROCRpred,"auc")@y.values)
#auc 0.883
##################################################################
# summarydecisiontree cvaccuracy for t=0.5 0.868 auc=0.883 #
##################################################################
#random forest
set.seed(2017)
#buildmodel
crf = randomForest(over50k~ . , data = ctrain)
#predicton test
predictTest=predict(crf,newdata=ctest)
#accuracy calculation
t(table(ctest$over50k,predictTest))
(2426+214)/(sum(table(ctest$over50k,predictTest)))
#AUC calculation
predictTest=predict(crf,newdata=ctest,type="prob")
ROCRpred= prediction(predictTest[,2],ctest$over50k)
as.numeric(performance(ROCRpred,"auc")@y.values)
##################################################################
# auc=0.889 acc=0.826 #
##################################################################
#parameterstuningrandomforest
# make take a lot of time
metric<- "Accuracy"
control <- trainControl(method="cv",number=10,search="grid")
tunegrid<- expand.grid(.mtry=c(sqrt(ncol(ctrain))))
modellist<- list()
for (ntree inc(700,1000, 1200, 1400 )) {
for(mtry in c(2,3,4,5,6,7,9)) {
tunegrid= expand.grid(.mtry=mtry)
set.seed(2017)
fit<- train(over50k~.,data=ctrain[1:5000,],method="rf",metric=metric,tuneGrid=tunegrid,
trControl=control,ntree=ntree)
key<- toString(ntree*10000+mtry)
modellist[[key]]<- fit
print(c(ntree,mtry))
print(fit)
}
}
#bestrf
set.seed(2017)
crf = randomForest(over50k~ . , data = ctrain,ntree=1400,mtry=2)
predictTest=predict(crf,newdata=ctest)
t(table(ctest$over50k,predictTest))
(2427+217)/(sum(table(ctest$over50k,predictTest)))
predictTest=predict(crf,newdata=ctest,type="prob")
ROCRpred= prediction(predictTest[,2],ctest$over50k)
as.numeric(performance(ROCRpred,"auc")@y.values)
##################################################################
# auc=0.901 accuracy=0.827 #
##################################################################
#################################################################
#winnerlogit.validationset
set.seed(2017)
clogit= glm( over50k ~ . , family="binomial",data= ctrain)
summary(clogit)
#predictionandaccuracy on test
predictTest=predict(clogit,newdata=cvalidation,type ="response")
t(table(cvalidation$over50k,predictTest>= 0.5))
(2256+460)/sum(table(cvalidation$over50k,predictTest>=0.5))
#AUC
ROCRpred= prediction(predictTest,cvalidation$over50k)
as.numeric(performance(ROCRpred,"auc")@y.values)
################################
#baggingmtry=numberof features
set.seed(2017)
#buildmodel
crf = randomForest(over50k~ . , data = ctrain,mtry=12)
#predicton test
predictTest=predict(crf,newdata=ctest)
#accuracy calculation
t(table(ctest$over50k,predictTest))
(2398+214)/(sum(table(ctest$over50k,predictTest)))
#AUC calculation
predictTest=predict(crf,newdata=ctest,type="prob")
ROCRpred= prediction(predictTest[,2],ctest$over50k)
as.numeric(performance(ROCRpred,"auc")@y.values)
importance(crf)
##################################################################
# auc=0.848 acc=0.817 #
##################################################################
#####################
#boosting
install.packages("gbm")
library(gbm)
#transformdependentvariable to01 variable
ctrain$over50k=as.integer(ctrain$over50k)-1
ctest$over50k=as.integer(ctest$over50k)-1
#buildmodel
set.seed(2017)
cboost= gbm(over50k~ . , data = ctrain,distribution="bernoulli",n.trees=5000,interaction.depth=4)
#prediction
predictTest=predict(cboost,newdata=ctest,n.trees=5000,type='response')
#qualityof prediction
t(table(ctest$over50k,predictTest>0.5))
(2308+475)/(sum(table(ctest$over50k,predictTest)))
ROCRpred= prediction(predictTest,ctest$over50k)
as.numeric(performance(ROCRpred,"auc")@y.values)
##################################################################
# auc=0.922 acc=0.8700 #
##################################################################
# auc
#boosting 0.922
#logit 0.91
#tuningrand forest0.901
#random forest 0.889
#tuningtree 0.883
#decisiontree 0.855
#baging 0.848

More Related Content

Similar to מערכות לומדות תרגול 3 עצים

PyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filtersPyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filters
c.titus.brown
 
Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)
c.titus.brown
 
Internet programming lab manual
Internet programming lab manualInternet programming lab manual
Internet programming lab manual
inteldualcore
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivity
Mouli Chandira
 

Similar to מערכות לומדות תרגול 3 עצים (20)

M11 bagging loo cv
M11 bagging loo cvM11 bagging loo cv
M11 bagging loo cv
 
Advance java
Advance javaAdvance java
Advance java
 
R code
R codeR code
R code
 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions
 
PyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filtersPyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filters
 
Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)
 
Writing DSLs with Parslet - Wicked Good Ruby Conf
Writing DSLs with Parslet - Wicked Good Ruby ConfWriting DSLs with Parslet - Wicked Good Ruby Conf
Writing DSLs with Parslet - Wicked Good Ruby Conf
 
The Ring programming language version 1.10 book - Part 54 of 212
The Ring programming language version 1.10 book - Part 54 of 212The Ring programming language version 1.10 book - Part 54 of 212
The Ring programming language version 1.10 book - Part 54 of 212
 
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
 
Fcontratos
FcontratosFcontratos
Fcontratos
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
 
Internet programming lab manual
Internet programming lab manualInternet programming lab manual
Internet programming lab manual
 
The Ring programming language version 1.5.3 book - Part 54 of 184
The Ring programming language version 1.5.3 book - Part 54 of 184The Ring programming language version 1.5.3 book - Part 54 of 184
The Ring programming language version 1.5.3 book - Part 54 of 184
 
The Ring programming language version 1.5.3 book - Part 44 of 184
The Ring programming language version 1.5.3 book - Part 44 of 184The Ring programming language version 1.5.3 book - Part 44 of 184
The Ring programming language version 1.5.3 book - Part 44 of 184
 
SparkR - Play Spark Using R (20160909 HadoopCon)
SparkR - Play Spark Using R (20160909 HadoopCon)SparkR - Play Spark Using R (20160909 HadoopCon)
SparkR - Play Spark Using R (20160909 HadoopCon)
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivity
 
Java and xml
Java and xmlJava and xml
Java and xml
 
Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout
 

More from Igor Kleiner

More from Igor Kleiner (20)

Анализ данных просто и доступно - урок 1
Анализ данных просто и доступно - урок 1Анализ данных просто и доступно - урок 1
Анализ данных просто и доступно - урок 1
 
מדעי נתונים לכל אחד
מדעי נתונים לכל אחדמדעי נתונים לכל אחד
מדעי נתונים לכל אחד
 
מדע נתונים - למידה מכונות
מדע נתונים - למידה מכונותמדע נתונים - למידה מכונות
מדע נתונים - למידה מכונות
 
מבוא למדעי הנתונים שבוע 2
מבוא למדעי הנתונים שבוע 2מבוא למדעי הנתונים שבוע 2
מבוא למדעי הנתונים שבוע 2
 
מבוא למדעי הנתונים הרצאה 1
מבוא למדעי הנתונים הרצאה 1מבוא למדעי הנתונים הרצאה 1
מבוא למדעי הנתונים הרצאה 1
 
תכנות דינמי הרצאה 3
תכנות דינמי הרצאה 3תכנות דינמי הרצאה 3
תכנות דינמי הרצאה 3
 
תכנות דינמי הרצאה 4
תכנות דינמי הרצאה 4תכנות דינמי הרצאה 4
תכנות דינמי הרצאה 4
 
שאלות לתרגול עצמי
שאלות לתרגול עצמישאלות לתרגול עצמי
שאלות לתרגול עצמי
 
פתרון תרגיל 3
פתרון תרגיל 3פתרון תרגיל 3
פתרון תרגיל 3
 
מבוא לתכנות מדעי: פייתון הרצאה 13
מבוא לתכנות מדעי: פייתון הרצאה 13מבוא לתכנות מדעי: פייתון הרצאה 13
מבוא לתכנות מדעי: פייתון הרצאה 13
 
תכנות מדעי פייתון: הרצאה 12: סיבוכיות
תכנות מדעי פייתון: הרצאה 12: סיבוכיותתכנות מדעי פייתון: הרצאה 12: סיבוכיות
תכנות מדעי פייתון: הרצאה 12: סיבוכיות
 
מבוא לתכנות מדעי: פייתון: הרצאה 11: דבגינג + תכנות דינמי
מבוא לתכנות מדעי: פייתון: הרצאה 11: דבגינג + תכנות דינמימבוא לתכנות מדעי: פייתון: הרצאה 11: דבגינג + תכנות דינמי
מבוא לתכנות מדעי: פייתון: הרצאה 11: דבגינג + תכנות דינמי
 
תכנות מדעי: פייתון: הרצאה 10: : תחום הכרעה
תכנות מדעי: פייתון: הרצאה 10: : תחום הכרעהתכנות מדעי: פייתון: הרצאה 10: : תחום הכרעה
תכנות מדעי: פייתון: הרצאה 10: : תחום הכרעה
 
מבוא לתכנות מדעי: פייתון: הרצאה 9: 2017
מבוא לתכנות מדעי: פייתון: הרצאה 9: 2017מבוא לתכנות מדעי: פייתון: הרצאה 9: 2017
מבוא לתכנות מדעי: פייתון: הרצאה 9: 2017
 
תכנות מדעי: פייתון: הרצאה 8: 2017
תכנות מדעי: פייתון: הרצאה 8:  2017תכנות מדעי: פייתון: הרצאה 8:  2017
תכנות מדעי: פייתון: הרצאה 8: 2017
 
תכנות מדעי: פייתון : הרצאה 7: 2017
תכנות מדעי: פייתון : הרצאה 7: 2017תכנות מדעי: פייתון : הרצאה 7: 2017
תכנות מדעי: פייתון : הרצאה 7: 2017
 
תכנות מדעי: פייתון: הרצאה 6: קבצים, רשימות
תכנות מדעי: פייתון: הרצאה 6: קבצים, רשימותתכנות מדעי: פייתון: הרצאה 6: קבצים, רשימות
תכנות מדעי: פייתון: הרצאה 6: קבצים, רשימות
 
מבוא לתכנות מדעי: פייתון: הרצאה 5: 2017
מבוא לתכנות מדעי: פייתון: הרצאה 5: 2017מבוא לתכנות מדעי: פייתון: הרצאה 5: 2017
מבוא לתכנות מדעי: פייתון: הרצאה 5: 2017
 
מבוא לתכנות מדעי: פייתון: הרצאה 4: 2017
מבוא לתכנות מדעי: פייתון: הרצאה 4: 2017מבוא לתכנות מדעי: פייתון: הרצאה 4: 2017
מבוא לתכנות מדעי: פייתון: הרצאה 4: 2017
 
מבוא לתכנות מדעי: פייתון: הרצאה 3: לולאות
מבוא לתכנות מדעי: פייתון: הרצאה 3: לולאותמבוא לתכנות מדעי: פייתון: הרצאה 3: לולאות
מבוא לתכנות מדעי: פייתון: הרצאה 3: לולאות
 

Recently uploaded

Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
YibeltalNibretu
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Recently uploaded (20)

Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Advances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdfAdvances in production technology of Grapes.pdf
Advances in production technology of Grapes.pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 

מערכות לומדות תרגול 3 עצים