forked from stepthom/sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides_featurehashing.Rmd
166 lines (125 loc) · 4.84 KB
/
slides_featurehashing.Rmd
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
---
title: "Feature Hashing"
author: "Dr. Stephen W. Thomas, Queen's University"
output:
pdf_document:
highlight: pygments
number_sections: yes
toc: no
toc_depth: '2'
---
```{r}
library(FeatureHashing)
library(dplyr)
```
```{r}
diabetes <- read.csv("data/diabetic_data.csv", stringsAsFactors = FALSE)
# quick look at the data
str(diabetes)
# drop useless variables
diabetes <- subset(diabetes,select=-c(encounter_id, patient_nbr))
# transform all "?" to 0s
diabetes[diabetes == "?"] <- NA
# remove zero variance - ty James http://stackoverflow.com/questions/8805298/quickly-remove-zero-variance-variables-from-a-data-frame
diabetes <- diabetes[sapply(diabetes, function(x) length(levels(factor(x,exclude=NULL)))>1)]
# prep outcome variable to those readmitted under 30 days
diabetes$readmitted <- ifelse(diabetes$readmitted == "<30",1,0)
# generalize outcome name
outcomeName <- 'readmitted'
# large factors to deal with
length(unique(diabetes$diag_1))
length(unique(diabetes$diag_2))
length(unique(diabetes$diag_3))
```
```{r}
# dummy var version -------------------------------------------------------
diabetes_dummy <- diabetes
# alwasy a good excersize to see the length of data that will need to be transformed
# charcolumns <- names(diabetes_dummy[sapply(diabetes_dummy, is.character)])
# for (thecol in charcolumns)
# print(paste(thecol,length(unique(diabetes_dummy[,thecol]))))
# warning will need 2GB at least free memory
require(caret)
dmy <- dummyVars(" ~ .", data = diabetes_dummy)
diabetes_dummy <- data.frame(predict(dmy, newdata = diabetes_dummy))
# many features
dim(diabetes_dummy)
# change all NAs to 0
diabetes_dummy[is.na(diabetes_dummy)] <- 0
# split the data into training and testing data sets
set.seed(1234)
split <- sample(nrow(diabetes_dummy), floor(0.5*nrow(diabetes_dummy)))
objTrain <-diabetes_dummy[split,]
objTest <- diabetes_dummy[-split,]
predictorNames <- setdiff(names(diabetes_dummy),outcomeName)
# cv.glmnet expects a matrix
library(glmnet)
# straight matrix model not recommended - works but very slow, go with a sparse matrix
# glmnetModel <- cv.glmnet(model.matrix(~., data=objTrain[,predictorNames]), objTrain[,outcomeName],
# family = "binomial", type.measure = "auc")
glmnetModel <- cv.glmnet(sparse.model.matrix(~., data=objTrain[,predictorNames]), objTrain[,outcomeName],
family = "binomial", type.measure = "auc")
glmnetPredict <- predict(glmnetModel,sparse.model.matrix(~., data=objTest[,predictorNames]), s="lambda.min")
# dummy version score:
auc(objTest[,outcomeName], glmnetPredict)
```
```{r}
# feature hashed version -------------------------------------------------
diabetes_hash <- diabetes
predictorNames <- setdiff(names(diabetes_hash),outcomeName)
# change all NAs to 0
diabetes_hash[is.na(diabetes_hash)] <- 0
set.seed(1234)
split <- sample(nrow(diabetes_hash), floor(0.5*nrow(diabetes_hash)))
objTrain <-diabetes_hash[split,]
objTest <- diabetes_hash[-split,]
dim(objTrain)
head(objTrain)
```
```{r}
set.seed(123)
idx = which(objTrain$race == "Asian")[1:3]
idx = c(idx, which(objTrain$race == "Hispanic")[1:3])
idx = c(idx, which(objTrain$race == "Other")[1:3])
idx = c(idx, which(objTrain$race == "0")[1:3])
idx = c(idx, which(objTrain$race == "AfricanAmerican")[1:3])
idx = c(idx, which(objTrain$race == "Caucasian")[1:3])
df = objTrain[idx,]
df$readmitted = sample(c(0, 1), replace=T, size=nrow(df))
df
colnames(df)
df1 = df %>% select(race)
df1_hashed = hashed.model.matrix(~ . -1, data=df1, hash.size=2^3, transpose=FALSE, create.mapping = TRUE)
df1_all = cbind(df1, as.matrix(df1_hashed))
hash.mapping(df1_hashed)
print("")
df2 = df %>% select(race, gender)
df2_hashed = hashed.model.matrix(~ . -1, data=df2, hash.size=2^3, transpose=FALSE, create.mapping = TRUE)
df2_all = cbind(df2, as.matrix(df2_hashed))
hash.mapping(df2_hashed)
print("")
df3 = df %>% select(race, gender, age)
df3_hashed = hashed.model.matrix(~ . -1, data=df3, hash.size=2^3, transpose=FALSE, create.mapping = TRUE)
df3_all = cbind(df3, as.matrix(df3_hashed))
hash.mapping(df3_hashed)
```
```{r}
library(FeatureHashing)
objTrain_hashed = hashed.model.matrix(~., data=objTrain[,predictorNames], hash.size=2^12, transpose=FALSE)
objTrain_hashed = as(objTrain_hashed, "dgCMatrix")
objTest_hashed = hashed.model.matrix(~., data=objTest[,predictorNames], hash.size=2^12, transpose=FALSE)
objTest_hashed = as(objTest_hashed, "dgCMatrix")
str(objTrain_hashed)
dim(objTrain_hashed)
sort(objTrain_hashed[2,], decreasing=T)[1:20]
sort(objTrain_hashed[3,], decreasing=T)[1:20]
sort(objTrain_hashed[300,], decreasing=T)[1:20]
```
```{r}
library(glmnet)
glmnetModel <- cv.glmnet(objTrain_hashed, objTrain[,outcomeName],
family = "binomial", type.measure = "auc")
# hashed version score:
glmnetPredict <- predict(glmnetModel, objTest_hashed, s="lambda.min")
auc(objTest[,outcomeName], glmnetPredict)
```