forked from matloff/ArtOfML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllCodeOrdered.R
505 lines (466 loc) · 12.2 KB
/
AllCodeOrdered.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# this is all the code from the book, ordered by the sequence of chapters
# in the book
# PrologueReg.tex:
data(day1)
head(day1)
nrow(day1)
day1 <- day1[,c(8,10:13,16)]
head(day1)
knnout <- qeKNN(day1,'tot',k=5) # fit the k-NN model
today <- data.frame(workingday=1, temp=12.8, atemp=11.8, hum=0.23,
windspeed=5)
predict(knnout,today) # predict new case
anotherday <- today
anotherday$windspeed <- 18
predict(knnout,anotherday)
predict(knnout,rbind(today,anotherday))
data(mlb)
head(mlb)
w <- mlb[,c(4:6)] # extract height, weight and age
z <- qeKNN(w,'Weight') # fit k-NN model
predict(z,data.frame(Height=72,Age=24))
knnout1 <- qeKNN(mlb[,3:6],'Weight',25) # extract Position, Height, Age
predict(knnout1,data.frame(Height=72,Age=24,Position='Catcher'))
sqrt((70/39.37-72/39.37)^2 + (12*24-12*30)^2)
ht <- mlb$Height
ht <- (ht - mean(ht)) / sd(ht)
mlb$Height <- ht
x <- data.frame(u=3:5,v=c(12,5,13))
x
mmscale(x)
kno <- qeKNN(day1,'tot',1)
datapoint3X <- day1[3,-6] # remote Y value
predict(kno,datapoint3X)
day1[3,6]
knnout <- qeKNN(day1,'tot',5)
knnout$testAcc
meanTot <- mean(day1$tot)
meanTot
mean(abs(day1$tot - meanTot))
qeKNN(day1,'tot',10)$testAcc
qeKNN(day1,'tot',25)$testAcc
qeKNN(day1,'tot',10)$testAcc
qeKNN(day1,'tot',10)$testAcc
set.seed(9999)
qeKNN(day1,'tot',10)$testAcc
qeKNN(day1,'tot',10)$testAcc
set.seed(9999) # try it again
qeKNN(day1,'tot',10)$testAcc
qeKNN(day1,'tot',10)$testAcc
plot(day1$tot,type='l') # plotting type is 'l', lines between points
day1[,c(8,10:13,16)]
data(day1)
day2 <- day1[,c(1,8,10:13,16)]
kno <- qeKNN(day2,'tot',k=5) # re-run k-NN on the new data
kno$testAcc
head(day1)
table(day1$weathersit)
w <- c(102,140,0,129,0)
w == 0
w[w == 0] <- NA # set the TRUE elements of w to NAs
w
library(regtools)
args(kNN)
x <- mlb[,c(4,6)] # height, age
y <- mlb[,5] # weight
newx <- c(72,24) # ht, age of new player to be predicted re weight
kmax <- 25 # k
knnout <- kNN(x,y,newx,kmax)
knnout$regests # predicted value
kno <- qeKNN(mlb[,4:6],'Weight',k=25,holdout=NULL)
predict(kno,data.frame(Height=72,Age=24))
# PrologueClass.tex:
vert <- read.table('column_3C.dat',header=FALSE,stringsAsFactors=TRUE)
head(vert)
table(vert$V7)
class(vert$V7)
levels(vert$V7)
telco <- read.csv('WA_Fn-UseC_-Telco-Customer-Churn.csv',header=TRUE)
head(telco)
names(telco)
class(telco$Churn)
telco <- read.csv('WA_Fn-UseC_-Telco-Customer-Churn.csv',header=TRUE,
stringsAsFactors=TRUE)
class(telco$Churn)
tc <- telco[,-1]
sum(is.na(tc))
ccIdxs <- which(complete.cases(tc))
tc <- tc[ccIdxs,]
tc <- na.exclude(tc)
nrow(tc)
set.seed(9999)
knnout <- qeKNN(tc,'Churn',75)
newCase <- tc[8,]
newCase$gender <- 'Male'
newCase$SeniorCitizen <- 1
newCase <- newCase[,-20]
newCase <- subset(newCase,select=-Churn)
predict(knnout,newCase)
knnout$testAcc
length(levels(telco$customerID))
vert <- read.table('column_3C.dat',header=FALSE,stringsAsFactors=TRUE)
head(vert)
nrow(vert)
set.seed(999)
## kout <- qeKNN(vert,'V7',5)
kout <- qeKNN(vert,'V7',50)
z <- vert[1,-7] # exclude Y
z$V2 <- 25
predict(kout,z)
kout$testAcc
mean(telco$Churn == 'Yes')
lco$Churn == 'Yes'
table(vert$V7) / nrow(vert)
kout$baseAcc
data(day1)
day1 <- day1[,c(8,10:13,16)] # extract the desired features
set.seed(9999)
knnout <- qeKNN(day1,'tot',k=5) # fit the k-NN model
knnout$testAcc
knnout$baseAcc
kout$confusion
ma <- read.csv('KaggleV2-May-2016.csv',header=TRUE,
stringsAsFactors=TRUE)
# load('MLBook_ma_data')
names(ma)
nrow(ma)
length(unique(ma$PatientId))
ma <- ma[,-c(1,2,4,5,7)]
names(ma)
table(ma$No.show)
kout <- qeKNN(ma,'No.show',25)
names(kout)
names(kout$holdoutPreds)
predY <- kout$holdoutPreds$predClasses
table(predY)
preds <- predict(kout,ma[,-9]) # exclude "Y", column 9
table(preds$probs[,2])
sum(preds$probs[,2] > 0.25)
w <- qeROC(tc,knnout,'Churn','Yes')
w$auc
head(w$thresholds,20)
head(w$specificities,20)
head(w$sensitivities,20)
qeROC(vert,kout,'V7','DH')
qeROC(vert,kout,'V7','NO')
qeROC(vert,kout,'V7','SL')
# BiasVarOver.tex:
data(pef)
head(pef)
dim(pef)
z <- qeKNN(pef,'wageinc',k=10)
z$testAcc
z$baseAcc
# DimReduce.tex:
yr <- read.csv('YearPredictionMSD.txt',header=FALSE)
library(data.table)
yr <- fread('YearPredictionMSD.txt')
yr <- as.data.frame(yr)
dim(yr)
system.time(kNN(yr[,-1],yr[,1],yr[1,-1],25))
pcout <- prcomp(yr[,-1])
rt <- pcout$rotation
dim(rt)
rt[1:10,1:10]
names(yr)
pcout$rotation <- rt[,1:20]
pcX <- predict(pcout,yr[,-1])
dim(pcX)
w <- yr[8,-1]
w[1] <- 32.6
newW <- predict(pcout,w)
newW
pcout$sdev^2
u <- c(12,5,13)
cumsum(u) # 12+5 = 17, 12+5+13=30
cumsum(u) / length(u) # convert to proportions
pcsds <- pcout$sdev^2
cumsum(pcsds) / sum(pcsds)
z$testAcc
w <- yr[8,-1]
w[1] <- 32.6
predict(z,w)
qeFOCI(pef,'wageinc')
yr1 <- yr[sample(1:nrow(yr),50000),]
system.time(z <- qeFOCI(yr1,'V1',numCores=2,parPlat='locThreads'))
z
# CART.tex:
w <- airq[1,-1]
w[2] <- 8.8
w
predict(dtout,w)
plot(dtout)
dtout$termNodeMembers[['5']]
data(yell10k)
head(yell10k)
length(unique(yell10k$PULocationID))
length(unique(yell10k$DOLocationID))
143*205
dtout <- qeDT(yell10k,'tripTime')
dtout$testAccAA
dtout$baseAcc
dtout
dtout$termNodeCounts
library(data.table)
cvr <- fread('covtype.data.gz')
cvr[1,]
cvr$V55 <- as.factor(cvr$V55)
table(cvr$V55)
cvr50k <- cvr[sample(1:nrow(cvr),50000),]
dto <- qeDT(cvr50k,'V55')
dto$testAcc
dto$baseAcc
dto$confusion
dto$nNodes
dto$nTermNodes
sort(airq$Wind)
args(qeDT)
# BagBoost.tex:
rfout <- qeRF(vert,'V7',holdout=NULL)
z <- vert[1,-7]
z$V2 <- 18
predict(rfout,z)
predict(kout,z)
dim(afrsoil)
names(afrsoil)
set.seed(9999)
rfo <- qeRF(afrsoil[,c(1:3578,3597)],'pH',holdout=500)
rfo$testAcc
rfo$baseAcc
range(afrsoil$pH)
predict(rfo,afrsoil[88,1:3594])
ds <- read.csv('dataset.csv',stringsAsFactors=TRUE)
names(ds)
ds <- ds[,-1]
head(ds)
dim(ds)
set.seed(9999)
gbout <- qeGBoost(ds,'MOS',nTree=750,holdout=NULL)
ds3 <- ds[3,-8]
ds3[,3] <- 1500
ds3[,4] <- 62
predict(gbout,ds3)
gbm.perf(gbout$gbmOuts)
predict(gbout,ds3,newNTree=382)
mean(abs(preds - ds[,8]))
set.seed(9999)
gbout <- qeGBoost(vert,'V7')
predict(gbout,vert[12,-7])
# FineTuning.tex:
set.seed(9999)
ftout <- qeFT(data=pef,yName='wageinc',qeftn='qeKNN',
pars=list(k=5:25),nTst=1000,nXval=5)
ftout
ftout <- qeFT(data=pef,yName='occ',qeftn='qeKNN',pars=list(k=1:25),
nTst=1000,nXval=5)
ftout
head(phoneme)
dim(phoneme)
z <- qeFT(phoneme,'lbl','qeDT',list(alpha=c(0.01,0.05,0.10,0.25,0.50,1),
50,1000,5,showProgress=T)
z
# LinGenLin.tex:
data(mlb)
hw <- mlb[,2:3]
ht71 <- which(hw$Height == 71)
mean(hw$Weight[ht71])
meanWts <- tapply(hw$Weight,hw$Height,mean)
meanWts
meanWts['70']
plot(names(meanWts),meanWts)
lmout <- lm(Weight ~ .,data=hw)
lmout
predict(lmout,data.frame(Height=71))
qelout <- qeLin(hw,'Weight',holdout=NULL)
qelout$coef
predict(qelout,data.frame(Height=71))
qelout <- qeLin(mlb[,4:6],'Weight',holdout=NULL)
predict(qelout,data.frame(Height=71,Age = 28))
Abb[1,]$house_rules
Abb[1,]$monthly_price
table(Abb$square_feet)
head(Abb)
linout <- qeLin(Abb[,-c(4,5)],'monthly_price',holdout=NULL)
linout$coef
class(linout)
linout <- qeLin(Abb[,-c(4,5)],'monthly_price')
data(mlb)
mlb[1,]
set.seed(9999)
glout <- qeLogit(tc,'Churn',holdout=NULL,yesYVal='Yes')
names(tc)
newx <- tc[333,-20] # exclude Y
newx
newx$gender <- 'Female'
predict(glout,newx)
data(falldetection)
fd <- falldeteciton
head(fd)
fd$ACTIVITY <- as.factor(fd$ACTIVITY) # was integer, need factor for qe*
set.seed(9999)
fd$ACTIVITY <- as.factor(fd$ACTIVITY)
fdout <- qeLogit(fd,'ACTIVITY')
fdout$testAcc
fdout$baseAcc
table(fd$ACTIVITY)
data(day1)
e1 <- day1[,c(4,10,12,13,16)]
e2 <- day1[,c(4,10,12,13,16,6,7)] # add holiday, weekday columns
names(e1)
names(e2)
set.seed(9999)
e1out <- qeLin(e1,'tot')
e2out <- qeLin(e2,'tot')
newx1 <- e1[3,-5] # exclude tot
newx2 <- e2[3,-5] # exclude tot
predict(e1out,newx1)
predict(e2out,newx2)
stdErrPred(e1out,newx1)
stdErrPred(e2out,newx2)
data(day1)
day1tottemp <- day1[,c(10,16)] # just tot, temp
head(day1tottemp)
day1tottemp$tempSqr <- day1tottemp$temp^2
head(day1tottemp)
day1tottemp <- day1[,c(10,16)]
qepout <- qePolyLin(day1tottemp,'tot',deg=2,holdout=NULL)
names(qepout)
qepout$bh
predict(qepout,data.frame(temp=12))
set.seed(9999)
qePolyLin(day1tottemp,'tot',deg=2,holdout=100)$testAcc
set.seed(9999) # to get the same holdout set
qeLin(day1tottemp,'tot',holdout=100)$testAcc
data(pef)
set.seed(9999)
qeLogit(pef,'occ')$testAcc
set.seed(9999)
qePolyLog(pef,'occ',2)$testAcc
table(mlb$Height)
qeCompare(vert,'V7',c('qeLogit','qePolyLog','qeKNN','qeRF','qeGBoost'),100)
# LASSO.tex:
yellout <- qeLASSO(yell10k,'tripTime')
yellout$testAcc
yellout$baseAcc
Abb$square_feet <- NULL
Abb$weekly_price <- NULL
Abb <- na.exclude(Abb)
z <- qeLASSO(Abb,'monthly_price',holdout=NULL)
plot(z)
x18 <- Abb[18,-4]
x18[4] <- 360
x18[8] <- 92
predict(z,x18)
z$coefs
z$whenEntered
afrsoil1 <- afrsoil[,c(1:3578,3597)]
z <- qeLASSO(afrsoil1,'pH',holdout=NULL)
z$nzero
z$lambda
# SVM.tex:
data(forest500)
qeFOCI(forest500,'V55')$sel
f500 <- forest500[,c(1,6,55)]
head(f500)
table(f500$V55)
f500$V55 <- regtools::toSubFactor(f500$V55,list('1'))
head(f500)
plot(f500[,1:2],pch=ifelse(f500[,3] == '1',0,3))
w <- qeLogit(f500,'V55',holdout=NULL,yesYVal='1')
cf <- coef(w$glmOuts[[1]])
abline(a=-cf[1]/cf[3], b=-cf[2]/cf[3])
head(iris)
j2 <- iris[,c(2,4,5)] # sep. width, pet. width species
head(j2)
j2$Species <- toSubFactor(j2$Species,'setosa')
j2[c(7,77),]
plot(j2[,1:2],pch=3*as.numeric(j2[,3]))
plot(iris[,c(1,3)],pch=as.numeric(iris$Species))
set.seed(9999)
z <- matrix(rnorm(500),ncol=2)
plus1 <- z[z[,1]^2 + z[,2]^2 > 4,] # outer ring, class +1
minus1 <- z[z[,1]^2 + z[,2]^2 < 2,] # inner disk, class -1
plus1 <- cbind(plus1,+1) # add in Y column
minus1 <- cbind(minus1,-1) # add in Y column
head(plus1) # take a look
head(minus1)
pm1 <- rbind(plus1,minus1) # combine into one dataset
plot(pm1[,1:2],pch=pm1[,3]+2) # gives us pluses and circles
pm2 <- pm1[,1:2]^2 # replace each X value by its square
pm2 <- cbind(pm2,pm1[,3]) # tack on Y to the new datasets
plot(pm2[,1:2],pch=pm2[,3]+2)
z <- qeSVM(f500,'V55',holdout=NULL)
newx <- f500[8,1:2]
newx
newx[2] <- 2888
predict(z,newx)
gpout <- polyreg::getPoly(forest500,2) # input with n = 500, p = 54
# NeuralNets.tex:
z <- vert[1,-7] # exclude "Y", which we are predicting
nnout <- qeNeural(vert,'V7',holdout=NULL)
z$V2 <- 18
predict(nnout,z)
pars <- list(hidden=c('5,5','25,25','100,100','100,0.2,100,0.2',
learnRate=c(0.0001,0.0005,0.001,0.005))
ftout <- qeFT(fd,'ACTIVITY','qeNeural',pars=pars,nTst=250,nXval=25)
ftout$outdf
qeNeural(fd,'ACTIVITY')$baseAcc # any qe* function could be called
# FineTuning.tex:
set.seed(9999)
ftout <- qeFT(data=pef,yName='wageinc',qeftn='qeKNN',
pars=list(k=5:25),nTst=1000,nXval=5)
ftout
ftout <- qeFT(data=pef,yName='occ',qeftn='qeKNN',pars=list(k=1:25),
nTst=1000,nXval=5)
ftout
head(phoneme)
dim(phoneme)
z <- qeFT(phoneme,'lbl','qeDT',list(alpha=c(0.01,0.05,0.10,0.25,0.50,1),
50,1000,5,showProgress=T)
z
# TimeSeries.tex:
x <- c(5,12,13,8,88,6)
w <- TStoX(x,2)
wd <- as.data.frame(w)
wd
eus <- EuStockMarkets # built-in R dataset
tsout <- qeTS(5,eus,'qeKNN',opts=list(k=10)) # use k-NN with k = 10
data(weatherTS)
head(weatherTS)
ptot <- weatherTS$PRECTOT
z <- qeTS(2,ptot,'qeRF',holdout=NULL)
length(ptot)
predict(z,ptot[4016:4017])
qeTS(1,ptot,"qeKNN")$testAcc
replicMeans(1000,'qeTS(1,ptot,"qeKNN")$testAcc')
mean(abs(ptot - mean(ptot)))
replicMeans(1000,'qeTS(1,ptot,"qeKNN")$testAcc')
replicMeans(1000,'qeTS(2,ptot,"qeKNN")$testAcc')
replicMeans(1000,'qeTS(3,ptot,"qeKNN")$testAcc')
replicMeans(1000,'qeTS(4,ptot,"qeKNN")$testAcc')
replicMeans(1000,'qeTS(5,ptot,"qeKNN")$testAcc')
replicMeasn(1000,'qeTS(6,ptot,"qeKNN")$testAcc')
replicMean(1000,'qeTS(7,ptot,"qeKNN")$testAcc')
replicMeasn(1000,'qeTS(8,ptot,"qeKNN")$testAcc')
replicMeans(1000,'qeTS(9,ptot,"qeKNN")$testAcc')
replicMeans(1000,'qeTS(10,ptot,"qeKNN")$testAcc')
replicMeans(1000,'qeTS(3,ptot,"qeLin")$testAcc')
replicMeans(1000,'qeTS(3,ptot,"qePolyLin")$testAcc')
replicMeans(1000,'qeTS(3,ptot,"qeRF")$testAcc')
data(quizzes)
str(quizzes)
quizzes[8,1]
quizzes[8,2]
z <- qeText(quizzes,qeName='qeRF')
predict(z,quizzes[8,1])
library(textdata)
ag <- dataset_ag_news()
agdf <- as.data.frame(ag) # qe-series functions require data frames
agdf[,1] <- as.factor(agdf[,1]) # qe requires a factor Y
dim(ag)
agdf[28,] # for example
smallSet <- sample(1:nrow(agdf),10000)
agdfSmall <- agdf[smallSet,]
w <- qeText(agdfSmall[,c(1,3)],'class',qeName='qeSVM')
w$testAcc
w$baseAcc