Last active
August 29, 2015 13:56
-
-
Save firaswehbe/8921590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm(list=ls()) | |
# Manual way to create a grid from x in [-10,10] | |
# and y in [-5,5] | |
a<-matrix(rep(seq(-10,10,2),11)) | |
a<-cbind(a,rep(c(-5:5),each=11)) | |
# Assign classes in a linearly separable way | |
myclass = (a[,1]+a[,2])<5 | |
# myn is for picking the p-norm when we normalize | |
# So for myn == 2, we're doing Euclidian norm. | |
# See Section 1.5 in Doug's notes and excercise #7 | |
myn = 2 | |
# mymags is the p-norm | |
mymags = (abs(a[,1])^myn + abs(a[,2])^myn)^(1/myn) | |
# Divide by the norm -- verify that the length of each | |
# row vector in the a matrix is == 1 | |
b = a / mymags | |
# Here we are scaling each feature independantly by | |
# dividing each columns by it's standard deviation | |
# centered around the mean (standardizing) | |
c<-a | |
c[,1]<-(a[,1]-mean(a[,1]))/sd(a[,1]) | |
c[,2]<-(a[,2]-mean(a[,2]))/sd(a[,2]) | |
# In matrix 'd', normalizing after scaling. | |
mymags2 = (abs(c[,1])^myn + abs(c[,2])^myn)^(1/myn) | |
d = c / mymags2 | |
# PDF divice with 2 x 1 figures and 1 inch outer margin | |
pdf("normalizationeffect.pdf",width=8.5,height=11) | |
par(mfrow=c(2,1),omi=c(1,1,1,1)) | |
plot(a,col=ifelse(myclass,'red','green'),pch=16) | |
title("No normalization") | |
plot(b,col=ifelse(myclass,'red','green'),pch=16) | |
title(paste("Normalization ( p =",myn,")")) | |
# Note what happens when we call plot next. It jumps | |
# to the next figure which is outside the 2x1 layout. | |
# This is a pdf device so it starts a new page | |
plot(c,col=ifelse(myclass,'red','green'),pch=16) | |
title("Scaled / Standardized features ( mean at the center, sd = 1 )") | |
plot(d,col=ifelse(myclass,'red','green'),pch=16) | |
title("Normalization after scaling") | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment