SlideShare a Scribd company logo
#part1
pisaTrain = read.csv("pisa2009train.csv")
pisaTest = read.csv("pisa2009test.csv")
str(pisaTrain)
summary(pisaTrain)
pairs(pisaTrain[,18:24])
#average readinScore per gender
tapply(pisaTrain$readingScore, pisaTrain$male, mean)
#missing variables
summary(pisaTrain)
pisaTrain = na.omit(pisaTrain)
pisaTest = na.omit(pisaTest)
summary(pisaTrain)
summary(pisaTest)
plot(pisaTest$raceeth)
str(pisaTest$raceeth)
pisaTrain$raceeth = relevel(pisaTrain$raceeth, "White")
pisaTest$raceeth = relevel(pisaTest$raceeth, "White")
lmScore = lm(readingScore~., data=pisaTrain)
summary(lmScore)
SSE = sum(lmScore$residuals^2)
RMSE =sqrt(mean(lmScore$residuals^2))
predTest = predict(lmScore, newdata=pisaTest)
summary(predTest)
rmse=sqrt(mean((predTest-pisaTest$readingScore)^2))
SSE=sum((predTest-pisaTest$readingScore)^2)
baseline = mean(pisaTrain$readingScore)
SST=sum((baseline-pisaTest$readingScore)^2)
r2=1-SSE/SST
plot(lmScore)
hist(pisaTrain$readingScore)
#part2
Elantra = read.csv("elantra.csv")
fix(Elantra)
pairs(Elantra)
cor(Elantra)
library(car)
fit <- lm(ElantraSales~., data=Elantra)
vif(fit)
# Global test of model assumptions
install.packages("gvlma")
library(gvlma)
gvmodel <- gvlma(fit)
summary(gvmodel)
ElantraTrain = subset(Elantra, Year <= 2012)
ElantraTest = subset(Elantra, Year > 2012)
ElantraLM = lm(ElantraSales ~ Unemployment + Queries + CPI_energy + CPI_all, data=ElantraTrain)
summary(ElantraLM)
ElantraLM = lm(ElantraSales ~ Unemployment + Queries + CPI_energy + CPI_all + Month,
data=ElantraTrain)
summary(ElantraLM)
ElantraTrain$MonthFactor = as.factor(ElantraTrain$Month)
ElantraTest$MonthFactor = as.factor(ElantraTest$Month)
ElantraLM = lm(ElantraSales ~ Unemployment + Queries + CPI_energy + CPI_all + MonthFactor,
data=ElantraTrain)
summary(ElantraLM)
cor(ElantraTrain[c("Unemployment","Month","Queries","CPI_energy","CPI_all")])
PredictTest = predict(ElantraLM, newdata=ElantraTest)
SSE = sum((PredictTest - ElantraTest$ElantraSales)^2)
SST = sum((mean(ElantraTrain$ElantraSales) - ElantraTest$ElantraSales)^2)
#part 3
mod1=lm(medv~lstat*age,data = Boston)
summary(mod1)
mod2=lm(medv~lstat+age+lstat*age,data = Boston)
summary(mod2)
mod3=lm(medv~lstat:age,data = Boston)
summary(mod3)
#part4 Nonlinear Relationship
install.packages("ILSR")
library(ISLR)
?Auto
pairs(Auto)
attach(Auto)
plot(horsepower,mpg)
summary(lm(mpg~horsepower))
par(mfrow=c(2,2))
plot(lm(mpg~horsepower))
summary(lm(mpg~horsepower+I(horsepower^2)))
plot(lm(mpg~horsepower+I(horsepower^2)))
summary(lm(mpg~poly(horsepower,7)))
plot(lm(mpg~poly(horsepower,7)))
#cross validation
install.packages("boot")
library(boot)
set.seed(2017)
cv.error.10=rep(0,10)
for (i in 1:10){
glm.fit=glm(mpg~poly(horsepower,i),data=Auto)
cv.error.10[i]=cv.glm(Auto,glm.fit,K=10)$delta[1]
}
cv.error.10
par(mfrow=c(1,1))
plot(seq(1:10),cv.error.10,type = "l")
#logit
qual=read.csv("quality.csv")
fix(qual)
summary(qual)
str(qual)
pairs(qual)
hist(qual$PoorCare)
table(qual$PoorCare)
install.packages("caTools")
library(caTools)
split=sample.split(qual$PoorCare,SplitRatio = 0.8)
split
qTrain=subset(qual,split==TRUE)
qTest=subset(qual,split==FALSE)
plot(qTrain$OfficeVisits,qTrain$Narcotics,col=qTrain$PoorCare+1)
logitmodel=glm(PoorCare ~OfficeVisits+Narcotics,data=qTrain,family=binomial)
summary(logitmodel)
logitmodel1=glm(PoorCare ~.,data=qTrain,family=binomial)
summary(logitmodel1)
predTrain=predict(logitmodel,type = "response")
hist(predTrain)
tapply(predTrain,qTrain$PoorCare,mean)
t(table(qTrain$PoorCare,predTrain>0.5))
t(table(qTrain$PoorCare,predTrain>0.1))
t(table(qTrain$PoorCare,predTrain>0.07))
install.packages("ROCR")
library(ROCR)
ROCpredict=prediction(predTrain,qTrain$PoorCare)
ROCperf=performance(ROCpredict,"tpr","fpr")
plot(ROCperf)
plot(ROCperf,print.cutoffs.at=c(0,25,0.5,0.75))
auc.perf = performance(ROCpredict, measure = "auc")
auc.perf@y.values
plot(qTrain$OfficeVisits,qTrain$Narcotics,col=qTrain$PoorCare+1)
#build decision boundary
coefs = coef(logitmodel)
x = c(0,40)
y = c((-1/coefs[3]) * (coefs[2] * x + coefs[1]))
lines(x, y, col="black", lwd=2)
predTst = predict(logitmodel,type="response",newdata = qTest)
t(table(qTest$PoorCare,predTst>0.5))
#Framingham
Fdata = read.csv("framingham.csv")
str(Fdata)
library(caTools)
set.seed(2018)
split=sample.split(Fdata$TenYearCHD,SplitRatio = 0.8)
Ftrain=subset(Fdata,split==TRUE)
Ftest=subset(Fdata,split==FALSE)
table(Ftrain$TenYearCHD)
table(Ftest$TenYearCHD)
frLogitMod=glm(TenYearCHD~.,data=Ftrain)
summary(frLogitMod)
predTst = predict(frLogitMod,type="response",newdata = Ftest)
t(table(Ftest$TenYearCHD,predTst>0.5 ))
#roc for test
ROCpredict=prediction(predTst,Ftest$TenYearCHD)
ROCperf=performance(ROCpredict,"tpr","fpr")
plot(ROCperf)
auc.perf = performance(ROCpredict, measure = "auc")
auc.perf@y.values
#loans
Dloans=read.csv("loans.csv")
fix(Dloans)
table(Dloans$credit.policy)
str(Dloans)
#identify Na variable
summary(Dloans)
missing = subset(Dloans, is.na(log.annual.inc) | is.na(days.with.cr.line) | is.na(revol.util) |
is.na(inq.last.6mths) | is.na(delinq.2yrs) | is.na(pub.rec))
nrow(missing)
library(caTools)
set.seed(2018)
split=sample.split(Dloans$not.fully.paid,SplitRatio = 0.8)
Dtrain=subset(Dloans,split==TRUE)
Dtest=subset(Dloans,split==FALSE)
#imputation
install.packages("mice")
library(mice)
#train imputation
vars.for.imputation = setdiff(names(Dtrain), "not.fully.paid")
imputed = complete(mice(Dtrain[vars.for.imputation]))
Dtrain[vars.for.imputation] = imputed
summary(Dtrain)
#test imputation
vars.for.imputation = setdiff(names(Dtest), "not.fully.paid")
imputed = complete(mice(Dtest[vars.for.imputation]))
Dtest[vars.for.imputation] = imputed
summary(Dtest)
#prediction
mod1 = glm(not.fully.paid~., data=Dtrain, family="binomial")
summary(mod1)
#prediction
Dtest$predicted.risk = predict(mod1, newdata=Dtest, type="response")
t(table(Dtest$not.fully.paid, Dtest$predicted.risk > 0.5))
#ROC
library(ROCR)
pred = prediction(Dtest$predicted.risk, Dtest$not.fully.paid)
as.numeric(performance(pred, "auc")@y.values)
ROCperf=performance(pred,"tpr","fpr")
plot(ROCperf)

More Related Content

Similar to מערכות לומדות: תרגילי כיתה 4 ו-5

Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
James Titcumb
 
Climbing the Abstract Syntax Tree (Forum PHP 2017)
Climbing the Abstract Syntax Tree (Forum PHP 2017)Climbing the Abstract Syntax Tree (Forum PHP 2017)
Climbing the Abstract Syntax Tree (Forum PHP 2017)
James Titcumb
 
Rcommands-for those who interested in R.
Rcommands-for those who interested in R.Rcommands-for those who interested in R.
Rcommands-for those who interested in R.
Dr. Volkan OBAN
 
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
James Titcumb
 
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
James Titcumb
 
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
James Titcumb
 
Climbing the Abstract Syntax Tree (PHP UK 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)Climbing the Abstract Syntax Tree (PHP UK 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)
James Titcumb
 
Climbing the Abstract Syntax Tree (PHP Russia 2019)
Climbing the Abstract Syntax Tree (PHP Russia 2019)Climbing the Abstract Syntax Tree (PHP Russia 2019)
Climbing the Abstract Syntax Tree (PHP Russia 2019)
James Titcumb
 
Ch2
Ch2Ch2
CL metaprogramming
CL metaprogrammingCL metaprogramming
CL metaprogramming
dudarev
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Sway Wang
 
Climbing the Abstract Syntax Tree (php[world] 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)Climbing the Abstract Syntax Tree (php[world] 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)
James Titcumb
 
Phylogenetics in R
Phylogenetics in RPhylogenetics in R
Phylogenetics in R
schamber
 
Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
Andrew Shitov
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and Go
Eleanor McHugh
 
PHP - Introduction to String Handling
PHP -  Introduction to  String Handling PHP -  Introduction to  String Handling
PHP - Introduction to String Handling
Vibrant Technologies & Computers
 

Similar to מערכות לומדות: תרגילי כיתה 4 ו-5 (16)

Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
 
Climbing the Abstract Syntax Tree (Forum PHP 2017)
Climbing the Abstract Syntax Tree (Forum PHP 2017)Climbing the Abstract Syntax Tree (Forum PHP 2017)
Climbing the Abstract Syntax Tree (Forum PHP 2017)
 
Rcommands-for those who interested in R.
Rcommands-for those who interested in R.Rcommands-for those who interested in R.
Rcommands-for those who interested in R.
 
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
 
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)
 
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
 
Climbing the Abstract Syntax Tree (PHP UK 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)Climbing the Abstract Syntax Tree (PHP UK 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)
 
Climbing the Abstract Syntax Tree (PHP Russia 2019)
Climbing the Abstract Syntax Tree (PHP Russia 2019)Climbing the Abstract Syntax Tree (PHP Russia 2019)
Climbing the Abstract Syntax Tree (PHP Russia 2019)
 
Ch2
Ch2Ch2
Ch2
 
CL metaprogramming
CL metaprogrammingCL metaprogramming
CL metaprogramming
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Climbing the Abstract Syntax Tree (php[world] 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)Climbing the Abstract Syntax Tree (php[world] 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)
 
Phylogenetics in R
Phylogenetics in RPhylogenetics in R
Phylogenetics in R
 
Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and Go
 
PHP - Introduction to String Handling
PHP -  Introduction to  String Handling PHP -  Introduction to  String Handling
PHP - Introduction to String Handling
 

More from Igor Kleiner

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

Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
dot55audits
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 

Recently uploaded (20)

Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 

מערכות לומדות: תרגילי כיתה 4 ו-5

  • 1. #part1 pisaTrain = read.csv("pisa2009train.csv") pisaTest = read.csv("pisa2009test.csv") str(pisaTrain) summary(pisaTrain) pairs(pisaTrain[,18:24]) #average readinScore per gender tapply(pisaTrain$readingScore, pisaTrain$male, mean) #missing variables summary(pisaTrain) pisaTrain = na.omit(pisaTrain) pisaTest = na.omit(pisaTest) summary(pisaTrain) summary(pisaTest) plot(pisaTest$raceeth) str(pisaTest$raceeth) pisaTrain$raceeth = relevel(pisaTrain$raceeth, "White") pisaTest$raceeth = relevel(pisaTest$raceeth, "White") lmScore = lm(readingScore~., data=pisaTrain) summary(lmScore) SSE = sum(lmScore$residuals^2)
  • 2. RMSE =sqrt(mean(lmScore$residuals^2)) predTest = predict(lmScore, newdata=pisaTest) summary(predTest) rmse=sqrt(mean((predTest-pisaTest$readingScore)^2)) SSE=sum((predTest-pisaTest$readingScore)^2) baseline = mean(pisaTrain$readingScore) SST=sum((baseline-pisaTest$readingScore)^2) r2=1-SSE/SST plot(lmScore) hist(pisaTrain$readingScore) #part2 Elantra = read.csv("elantra.csv") fix(Elantra) pairs(Elantra) cor(Elantra) library(car) fit <- lm(ElantraSales~., data=Elantra) vif(fit) # Global test of model assumptions install.packages("gvlma") library(gvlma) gvmodel <- gvlma(fit) summary(gvmodel)
  • 3. ElantraTrain = subset(Elantra, Year <= 2012) ElantraTest = subset(Elantra, Year > 2012) ElantraLM = lm(ElantraSales ~ Unemployment + Queries + CPI_energy + CPI_all, data=ElantraTrain) summary(ElantraLM) ElantraLM = lm(ElantraSales ~ Unemployment + Queries + CPI_energy + CPI_all + Month, data=ElantraTrain) summary(ElantraLM) ElantraTrain$MonthFactor = as.factor(ElantraTrain$Month) ElantraTest$MonthFactor = as.factor(ElantraTest$Month) ElantraLM = lm(ElantraSales ~ Unemployment + Queries + CPI_energy + CPI_all + MonthFactor, data=ElantraTrain) summary(ElantraLM) cor(ElantraTrain[c("Unemployment","Month","Queries","CPI_energy","CPI_all")]) PredictTest = predict(ElantraLM, newdata=ElantraTest) SSE = sum((PredictTest - ElantraTest$ElantraSales)^2) SST = sum((mean(ElantraTrain$ElantraSales) - ElantraTest$ElantraSales)^2) #part 3 mod1=lm(medv~lstat*age,data = Boston) summary(mod1) mod2=lm(medv~lstat+age+lstat*age,data = Boston) summary(mod2)
  • 4. mod3=lm(medv~lstat:age,data = Boston) summary(mod3) #part4 Nonlinear Relationship install.packages("ILSR") library(ISLR) ?Auto pairs(Auto) attach(Auto) plot(horsepower,mpg) summary(lm(mpg~horsepower)) par(mfrow=c(2,2)) plot(lm(mpg~horsepower)) summary(lm(mpg~horsepower+I(horsepower^2))) plot(lm(mpg~horsepower+I(horsepower^2))) summary(lm(mpg~poly(horsepower,7))) plot(lm(mpg~poly(horsepower,7))) #cross validation install.packages("boot") library(boot) set.seed(2017)
  • 5. cv.error.10=rep(0,10) for (i in 1:10){ glm.fit=glm(mpg~poly(horsepower,i),data=Auto) cv.error.10[i]=cv.glm(Auto,glm.fit,K=10)$delta[1] } cv.error.10 par(mfrow=c(1,1)) plot(seq(1:10),cv.error.10,type = "l") #logit qual=read.csv("quality.csv") fix(qual) summary(qual) str(qual) pairs(qual) hist(qual$PoorCare) table(qual$PoorCare) install.packages("caTools") library(caTools) split=sample.split(qual$PoorCare,SplitRatio = 0.8) split qTrain=subset(qual,split==TRUE) qTest=subset(qual,split==FALSE)
  • 6. plot(qTrain$OfficeVisits,qTrain$Narcotics,col=qTrain$PoorCare+1) logitmodel=glm(PoorCare ~OfficeVisits+Narcotics,data=qTrain,family=binomial) summary(logitmodel) logitmodel1=glm(PoorCare ~.,data=qTrain,family=binomial) summary(logitmodel1) predTrain=predict(logitmodel,type = "response") hist(predTrain) tapply(predTrain,qTrain$PoorCare,mean) t(table(qTrain$PoorCare,predTrain>0.5)) t(table(qTrain$PoorCare,predTrain>0.1)) t(table(qTrain$PoorCare,predTrain>0.07)) install.packages("ROCR") library(ROCR) ROCpredict=prediction(predTrain,qTrain$PoorCare) ROCperf=performance(ROCpredict,"tpr","fpr") plot(ROCperf) plot(ROCperf,print.cutoffs.at=c(0,25,0.5,0.75)) auc.perf = performance(ROCpredict, measure = "auc") auc.perf@y.values plot(qTrain$OfficeVisits,qTrain$Narcotics,col=qTrain$PoorCare+1) #build decision boundary
  • 7. coefs = coef(logitmodel) x = c(0,40) y = c((-1/coefs[3]) * (coefs[2] * x + coefs[1])) lines(x, y, col="black", lwd=2) predTst = predict(logitmodel,type="response",newdata = qTest) t(table(qTest$PoorCare,predTst>0.5)) #Framingham Fdata = read.csv("framingham.csv") str(Fdata) library(caTools) set.seed(2018) split=sample.split(Fdata$TenYearCHD,SplitRatio = 0.8) Ftrain=subset(Fdata,split==TRUE) Ftest=subset(Fdata,split==FALSE) table(Ftrain$TenYearCHD) table(Ftest$TenYearCHD) frLogitMod=glm(TenYearCHD~.,data=Ftrain) summary(frLogitMod) predTst = predict(frLogitMod,type="response",newdata = Ftest) t(table(Ftest$TenYearCHD,predTst>0.5 )) #roc for test ROCpredict=prediction(predTst,Ftest$TenYearCHD)
  • 8. ROCperf=performance(ROCpredict,"tpr","fpr") plot(ROCperf) auc.perf = performance(ROCpredict, measure = "auc") auc.perf@y.values #loans Dloans=read.csv("loans.csv") fix(Dloans) table(Dloans$credit.policy) str(Dloans) #identify Na variable summary(Dloans) missing = subset(Dloans, is.na(log.annual.inc) | is.na(days.with.cr.line) | is.na(revol.util) | is.na(inq.last.6mths) | is.na(delinq.2yrs) | is.na(pub.rec)) nrow(missing) library(caTools) set.seed(2018) split=sample.split(Dloans$not.fully.paid,SplitRatio = 0.8) Dtrain=subset(Dloans,split==TRUE) Dtest=subset(Dloans,split==FALSE) #imputation install.packages("mice") library(mice)
  • 9. #train imputation vars.for.imputation = setdiff(names(Dtrain), "not.fully.paid") imputed = complete(mice(Dtrain[vars.for.imputation])) Dtrain[vars.for.imputation] = imputed summary(Dtrain) #test imputation vars.for.imputation = setdiff(names(Dtest), "not.fully.paid") imputed = complete(mice(Dtest[vars.for.imputation])) Dtest[vars.for.imputation] = imputed summary(Dtest) #prediction mod1 = glm(not.fully.paid~., data=Dtrain, family="binomial") summary(mod1) #prediction Dtest$predicted.risk = predict(mod1, newdata=Dtest, type="response") t(table(Dtest$not.fully.paid, Dtest$predicted.risk > 0.5)) #ROC library(ROCR) pred = prediction(Dtest$predicted.risk, Dtest$not.fully.paid) as.numeric(performance(pred, "auc")@y.values) ROCperf=performance(pred,"tpr","fpr") plot(ROCperf)