SlideShare une entreprise Scribd logo
1  sur  9
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

Contenu connexe

Similaire à מערכות לומדות תרגול 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
 

Similaire à מערכות לומדות תרגול 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 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
 
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
 
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
 

Plus de Igor Kleiner

Plus de 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: לולאות
 

Dernier

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Dernier (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

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